discord.py on_message在执行新命令时取消

时间:2020-05-13 17:01:08

标签: python-3.x discord.py

执行另一个命令时,如何取消on_message事件?我的代码:

@client.event
async def on_message(message):
    channel = message.author
    def check(m):
        return m.channel == message.channel and m.author != client.user

    if message.content.startswith("!order"):
        await channel.send("in game name")
        in_game_name = await client.wait_for('message', check=check)

        await channel.send("in game ID")
        in_game_ID = await client.wait_for('message', check=check)

    else:
        await client.process_commands(message)

2 个答案:

答案 0 :(得分:0)

on_message实际上仅应保留给需要on_message事件的事件(例如,如果您正在编写自动审核功能)。

对于命令,应该在功能之前使用@client.command@bot.command装饰器。 Here是命令的用法。以下是一些您为什么应该使用命令的原因(直接从discord.py discord上的?tag命令获得):

  • 防止意大利面条代码
    • 更好的表现
    • 轻松处理和处理命令参数
    • 参数类型转换器
    • 简单的子命令
    • 命令冷却时间
    • 内置帮助功能
    • 轻松的前缀管理
    • 命令检查,用于控制何时调用它们
    • 能够通过扩展名/齿轮添加命令模块
    • 仍然能够使用Client做所有事情

如果您有任何疑问,请随时对此答案发表评论!

答案 1 :(得分:0)

您可以将on_message事件放在嵌齿轮内。这样,on_message事件(或任何其他事件/命令)将仅在嵌齿轮处于活动状态时触发。

class Foo(commands.Cog):

    @commands.Cog.listener()
    async def on_message(self, message):
        # Do something

要注册齿轮(激活):

bot.add_cog(Foo(bot))

要移除齿轮(停用):

bot.remove_cog('Foo')

Link to cog doc