【问题标题】:Discord.py - on_message() conflicting with bot commandDiscord.py - on_message() 与 bot 命令冲突
【发布时间】:2021-07-09 13:05:44
【问题描述】:

问题:当我设置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)

【问题讨论】:

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


【解决方案1】:

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

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

默认情况下,这个协程在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)

【讨论】:

    【解决方案2】:

    嗨到最后 on_message 添加

    await bot.process_commands (message)
    

    https://discordpy.readthedocs.io/en/latest/faq.html#why-does-on-message-make-my-commands-stop-working

    【讨论】:

    • 请添加代码示例(使用 tic ` 标记制作代码块)网址可以更改或网站可以关闭。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 2021-01-17
    相关资源
    最近更新 更多