Python discord bot how to compare messages with a list

Viewed 956

I'm making a python Discord Bot and now i'm trying to make him respond to a certain message within a list, but there are some issues, because he only respond when the message starts the text (not in the middle or end). So i want to figure out how to make him compare all the text and match with the messages list, sending a text answer.

Python 3.8.2

Code:

import discord
from discord.ext        import commands
from discord.ext.commands   import Bot
import asyncio

bot = commands.Bot(command_prefix = "$")
phrases = ["QWACK","KWAK","AARK","KWAAAK"]

@bot.event
async def on_ready():
    print ("I'm ready!")

@bot.event
async def on_message(message):
    if str(phrases) in message.content:                                                    
        await message.channel.send("dhbang")
2 Answers

i'm amazed the bot responds at all xD

str(phrases) is a string, this specific string: "["QWACK","KWAK","AARK","KWAAAK"]"

You should iterate over each word, and check if that word is in the message.

You should also take into consideration capitalization and multiple keywords appearing in one message.

As wrong1man said, str(phrases) is a string, so it should only respond to messages like Lovely day, isn't it ["QWACK","KWAK","AARK","KWAAAK"]?. I would just like to extend wrong1man's answer by giving some code:

if any((phrase in message.content) for phrase in phrases):                                                    
    await message.channel.send("dhbang")

The above is the more concise version using a generator expression. If you are a Python begginer, you may be more comfortable with a normal for loop:

for phrase in phrases:
    if phrase in message.content:                                                    
        await message.channel.send("dhbang")
        break
Related