discord.py bot 不能在同一个 def 中使用 message 和 ctx

discord.py bot cant use message and ctx in the same def

我正在尝试在特定用户键入命令时执行特定操作。例如,如果 ID 为 123456 的用户发送我想要 await ctx.send('hi') 的命令,如果 ID 为 7890 的用户发送相同的命令以使用 await ctx.send('hi2')

这是我试过的:

@bot.command()
async def test(message, ctx):

    if message.author.id == "id goes here":
        await ctx.send('hi')
    elif message.author.id == "id goes here":
        await ctx.send('hi but other')

但是当我 运行 命令 !test 时它做了什么:

Ignoring exception in command test:
Traceback (most recent call last):
  File "/home/khvede1a/.local/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "/home/khvede1a/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 851, in invoke
    await self.prepare(ctx)
  File "/home/khvede1a/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 786, in prepare
    await self._parse_arguments(ctx)
  File "/home/khvede1a/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 697, in _parse_arguments
    transformed = await self.transform(ctx, param)
  File "/home/khvede1a/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 542, in transform
    raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing.

有效的是,如果我删除 ctx 并将其打印出来以控制台,它就可以正常工作。但如果 ctxmessage 在同一个 def 中,它们将出于某种原因无法工作。将它传递给另一个 def 可能是解决方案,但我在这里有点抱歉。

提前致谢

改成这样:

@bot.command()
async def test(ctx):
    if ctx.author.id == YOUR_ID:
        await ctx.send('Hi!')
    elif ctx.author.id == YOUR_ID:
        await ctx.send('Hey!')

其中 YOUR_ID 是允许机器人响应的用户 ID(整数值)。