首页 > 解决方案 > Discord py自动反应问题

问题描述

我的 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 传递,但这似乎不起作用。

标签: pythondiscorddiscord.py

解决方案


if message.content == welcome1 or welc:是它破裂的地方。or welc这意味着 ifwelc不是 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>')

推荐阅读