How to Enable a Python Discord Bot to Match Messages Against a List

I’m developing a Discord bot using Python, and I would like it to respond to any message that matches an entry in a specified list. Currently, it only reacts when the provided phrases are at the beginning of a message. I need assistance on how to make it compare the entire message to the list of phrases and send an appropriate reply whenever a match is found.

Python version: 3.8.2

Example Code:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='!')
response_list = ['HELLO', 'WORLD', 'TEST', 'PYTHON']

@bot.event
async def on_ready():
    print('Bot is online!')

@bot.event
async def on_message(msg):
    if any(phrase in msg.content for phrase in response_list):
        await msg.channel.send('Matched!')

To improve the bot’s functionality, consider converting both the message content and the items in your response_list to lowercase before performing the comparison. This way, it’ll account for case variations. Additionally, to ensure the bot doesn’t reply to its own messages or others from bots, include a return statement at the beginning of on_message to ignore them. Update your code snippet accordingly:

@bot.event
async def on_message(msg):
    if msg.author.bot:
        return
    content = msg.content.lower()
    if any(phrase.lower() in content for phrase in response_list):
        await msg.channel.send('Matched!')

This modification ensures broader matching and prevents bot loops.