Discord.py - on_message() 与 bot 命令冲突

时间:2021-04-14 13:10:40

标签: python-3.x discord.py bots

问题:当我设置 on_message 时,sendGif 命令不起作用。当我注释掉 on_message 时,它可以完美运行。

我的代码:

bot = commands.Bot(command_prefix='!')
#----------------------------------------------------------#
@bot.event
async def on_message(message):
if message.author == bot.user:
return
content = message.content
print(content)
if "test" in content:
await message.channel.send("You test" + message.author.mention)
await message.add_reaction(":haha:")
else:
pass
#----------------------------------------------------------#
@bot.command()
async def sendGif(ctx, link):
embed = discord.Embed(title="Embed GIF")
embed.set_thumbnail(url="Gif Link here")
await ctx.send(embed=embed)

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

在您的 on_message 事件引用中,您必须添加行 await bot.process_commands(message)discord.py documentation 解释了 process_commands method

<块引用>

这个函数处理已经注册到bot和其他组的命令。没有这个协程,任何命令都不会被触发。

<块引用>

默认情况下,此协程在 on_message() 事件内调用。如果您选择覆盖 on_message() 事件,那么您也应该调用该协程。

因此,您的 on_message() 事件引用应如下所示:

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    content = message.content
    print(content)
    if "test" in content:
        await message.channel.send("You test" + message.author.mention)
        await message.add_reaction(":haha:")
    await bot.process_commands(message)