【问题标题】:discord.py on_message cancel when new command is executeddiscord.py on_message 在执行新命令时取消
【发布时间】:2020-06-09 01:03:18
【问题描述】:

当我执行另一个命令时,如何取消我的 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)

【问题讨论】:

  • “取消”活动是什么意思?
  • 当我现在执行另一个命令时,它会发送其余的消息,然后执行另一个命令,我怎样才能避免这种情况并简单地执行新命令?
  • 我不明白...你只听了一个命令,那就是'order'
  • 你也在用@client.commands吗?
  • 我还有,1200行代码我就不放这里了

标签: python-3.x discord.py


【解决方案1】:

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

对于命令,您应该在函数之前使用 @client.command@bot.command 装饰器。 Here 是命令的用法。以下是您应该使用命令的几个原因(直接取自 discord.py discord 上的 ?tag 命令):

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

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

【讨论】:

    【解决方案2】:

    您可以将您的 on_message 事件放在一个 cog 中。这样,on_message 事件(或任何其他事件/命令)只会在 cog 处于活动状态时触发。

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

    注册 cog(激活):

    bot.add_cog(Foo(bot))
    

    移除齿轮(停用):

    bot.remove_cog('Foo')
    

    Link to cog doc

    【讨论】:

      猜你喜欢
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 2020-12-31
      • 2020-08-22
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 1970-01-01
      相关资源
      最近更新 更多