我正在try 制作一个discord机器人,但标题中出现了错误.这是我的代码:
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)
以下是完整的错误追溯:
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'
为什么会发生这种情况?如何解决它?