I’m trying to make a discord bot, but I get the error from the title. This is my code:
import discord
import os
from dotenv import load_dotenv
from discord.ext import commands
import logging
class DiscordBot:
def __init__(self) -> None:
self.intents = discord.Intents.all()
self.intents.messages = True
self.intents.message_content = True
self.intents.guilds = True
self.intents.members = True
self.intents.presences = True
self.bot = commands.Bot(command_prefix='$', intents=self.intents)
async def on_ready(self):
print("Bot is ready.")
@commands.command()
async def hello(self, ctx):
await ctx.channel.send('Hello!')
print("HELLO!")
if __name__ == '__main__':
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN_KEY')
logging.basicConfig(level=logging.DEBUG)
bot = DiscordBot()
bot.bot.add_command(bot.hello)
bot.bot.run(TOKEN)
Here is a complete error traceback:
ERROR:discord.ext.commands.bot:Ignoring exception in command hello
Traceback (most recent call last):
File "/home/dimo/programming/discord-bot/mybot-env/lib/python3.12/site-packages/discord/ext/commands/core.py", line 235, in wrapped
ret = await coro(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
TypeError: DiscordBot.hello() missing 1 required positional argument: 'ctx'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/dimo/programming/discord-bot/mybot-env/lib/python3.12/site-packages/discord/ext/commands/bot.py", line 1350, in invoke
await ctx.command.invoke(ctx)
File "/home/dimo/programming/discord-bot/mybot-env/lib/python3.12/site-packages/discord/ext/commands/core.py", line 1029, in invoke
await injected(*ctx.args, **ctx.kwargs) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/dimo/programming/discord-bot/mybot-env/lib/python3.12/site-packages/discord/ext/commands/core.py", line 244, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: DiscordBot.hello() missing 1 required positional argument: 'ctx'
Why is this happening, and how do I fix it?
aserient is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
The scope is off. You’re adding the command but it has self
because it’s in the class. When you use the decorator, you can’t have self
because then the first positional argument will be the class itself instead of the ctx
.
The following (I tested it) works:
@commands.command()
async def hello(ctx):
await ctx.channel.send('Hello')
print("HELLO!")
class DiscordBot:
def __init__(self) -> None:
self.intents = discord.Intents.all()
self.intents.messages = True
self.intents.message_content = True
self.intents.guilds = True
self.intents.members = True
self.intents.presences = True
self.bot = commands.Bot(command_prefix='$', intents=self.intents)
self.bot.add_command(hello)
Also, why do you have a bunch of intents enabled if self.intents = discord.Intents.all()
enables them anyway?