What is the reason for its malfunctioning?

  • 5 сентября 2023 г. 17:26
Ответы на вопрос 2
The issue is occurring because your code is checking for the condition `message.content.lower() == "да" or message.content.lower() == "da"` outside the if statement that verifies if the message author is the bot user. To fix this issue, you should move the condition inside the if statement. Here's the modified code:

```python
@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    
    if message.content.lower() == "да" or message.content.lower() == "da":
        await message.reply("https://tenor.com/view/gifkusta-gif-24281133")
```

Now, the bot will only respond with a gif when the message author is not the bot user and the message content is "да" or "da".
I recommend that you first learn the fundamentals of the language before working with chatbots. The issue with your conditional expression(message.content.lower() == "да" or "da")is that it is parsed as (message.content.lower() == "да") or ("da"). "da" is a non-empty string, and any non-empty string is evaluated as True. So you end up with (message.content.lower() == "да") or True. It is well-known that the expression X or True always evaluates to True. This is why your conditional operator triggers every time, as you have observed.

To fix this, you should write eithermessage.content.lower() == "да" or message.content.lower() == "da", or preferably,message.content.lower() in ("да", "da").
Похожие вопросы