“Discord 机器人中缺少必需的位置参数‘ctx’”

问题描述 投票:0回答:1

我正在尝试制作一个不和谐的机器人,但我从标题中得到了错误。这是我的代码:

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'

为什么会发生这种情况,如何解决?

python discord discord.py
1个回答
0
投票

范围已关闭。您正在添加命令,但它有

self
,因为它在类中。当你使用装饰器时,你不能有
self
,因为这样第一个位置参数将是类本身而不是
ctx

以下(我测试过)有效:

@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)

另外,如果

self.intents = discord.Intents.all()
无论如何都启用了它们,为什么还要启用一堆意图?

© www.soinside.com 2019 - 2024. All rights reserved.