【问题标题】:Discord py auto reaction issueDiscord py自动反应问题
【发布时间】:2021-04-23 02:17:09
【问题描述】:

我的 discord.py 机器人中有一个事件,它应该只对包含“welc”或“welcome”的消息使用表情符号做出反应,但是,机器人会对聊天中发送的所有消息做出反应。这是事件的代码。

    @commands.Cog.listener()
    async def on_message(self, message):
        welcome1 = 'welcome'
        welc = 'welc'
        if message.content == welcome1 or welc:
            await message.add_reaction('<:z_heart1:786021804690636810>')

我尝试在 if 语句中使用 else 传递,但这似乎不起作用。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    if message.content == welcome1 or welc: 是它崩溃的地方。 or welc 这意味着如果 welc 不是 None 并且 welc 不是 False。在这种情况下,welc 的值为 welc

    您可以使用我的示例中的列表来减少对 or 语句的需求。

        @commands.Cog.listener()
        async def on_message(self, message):
            welcome1 = 'welcome'
            welc = 'welc'
            if message.content in [welcome1,welc]:
                await message.add_reaction('<:z_heart1:786021804690636810>')
    

    【讨论】:

      【解决方案2】:

      原因是,if message.content == welcome1 or welc: 和 if message.content == welcome1 or message.content == welc: 不一样。你可以使用if message.content in [welcome1, welc]。

          welcome1 = 'welcome'
          welc = 'welc'
          @commands.Cog.listener()
          async def on_message(self, message):
              if message.content in [welcome1, welc]:
                  await message.add_reaction('<:z_heart1:786021804690636810>')
      

      【讨论】:

        猜你喜欢
        • 2021-11-22
        • 1970-01-01
        • 2022-01-20
        • 1970-01-01
        • 2021-06-04
        • 2018-08-19
        • 2020-06-27
        • 2021-08-26
        • 2021-03-24
        相关资源
        最近更新 更多