discord.py 机器人不能在同一定义中使用消息和 ctx



当某个用户键入命令时,我正在尝试做某件事。 例如,如果 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(整数值)。

最新更新