I’m working on a Discord bot using Python and I need help with message detection. Right now I have a list of specific words that I want my bot to react to. The problem is my current code doesn’t work properly - it’s supposed to check if any of those words appear anywhere in a user’s message, but it’s not detecting them at all.
I want the bot to scan the entire message content and if it finds any word from my predefined list, it should reply with a specific response. The words could be at the beginning, middle, or end of the message.
Here’s what I have so far:
import discord
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix = "!")
trigger_words = ["MEOW", "PURR", "HISS", "MEOOOOW"]
@client.event
async def on_ready():
print("Bot is online!")
@client.event
async def on_message(msg):
if str(trigger_words) in msg.content:
await msg.channel.send("cat detected!")
What am I doing wrong with the message comparison logic?
you’re checking str(trigger_words) which is converting the whole list to a string like “[‘MEOW’, ‘PURR’, ‘HISS’, ‘MEOOOOW’]” - that’s why it’s not working. use any(word in msg.content.upper() for word in trigger_words) instead since your words are uppercase.
You’re converting the whole list to a string, so it’ll never match actual message content. Had this same issue building my moderation bot last year. You’re literally searching for “[‘MEOW’, ‘PURR’, ‘HISS’, ‘MEOOOOW’]” instead of checking individual words. Here’s what fixed it for me - simple loop with case-insensitive matching: ```python @client.event
async def on_message(msg):
message_upper = msg.content.upper()
for word in trigger_words:
if word in message_upper:
await msg.channel.send(“cat detected!”)
return # prevents multiple responses
Key changes: check each word individually and convert the message to uppercase first. This also catches partial matches, so "meowing" triggers on "MEOW".
Your comparison logic is broken. When you do str(trigger_words) in msg.content, you’re converting the whole array into a string that looks like “[‘MEOW’, ‘PURR’, ‘HISS’, ‘MEOOOOW’]” and checking if that exact string exists in the message. That’ll never match. You need to check each word individually. Try this: for word in trigger_words: if word in msg.content.upper(): await msg.channel.send("cat detected!") break. I added .upper() since your trigger words are uppercase. The break makes sure the bot only responds once even if it finds multiple trigger words. Also, don’t forget await client.process_commands(msg) at the end of your on_message function if you want to use other commands later.