简体   繁体   English

Discord py自动反应问题

[英]Discord py auto reaction issue

I have an event in my discord.py bot that is supposed to only react with an emoji to a message containing "welc" or "welcome", however, the bot reacts to all messages sent in the chat.我的 discord.py 机器人中有一个事件,它应该只对包含“welc”或“welcome”的消息使用表情符号做出反应,但是,机器人会对聊天中发送的所有消息做出反应。 Heres the code for the event.这是事件的代码。

    @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>')

I have tried using an else pass in my if statement but that doesn't seem to work.我尝试在 if 语句中使用 else 传递,但这似乎不起作用。

The reason is, if message.content == welcome1 or welc: and if message.content == welcome1 or message.content == welc: are not the same.原因是, if message.content == welcome1 or welc:if message.content == welcome1 or message.content == welc:不一样。 You can use if message.content in [welcome1, 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>')

if message.content == welcome1 or welc: is where it's breaking. if message.content == welcome1 or welc:是它正在破坏的地方。 or welc which means if welc is not None and welc is not False. or welc表示如果welc不是 None 并且welc不是 False。 In this case welc has the value of welc在这种情况下, welc具有welc的值

You can use a list like in my example to reduce the need for or statements.您可以使用我的示例中的列表来减少对 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>')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM