KuFyOn
KuFyOn

Reputation: 15

Python Discord bot message.content

I'm trying to make my Discord bot to respond automatically when someone sends a specific word, the issue is that the command only works if the word is the first thing written in the sentence though. I would want the bot to respond to the message even when the speciifc word is in the middle or anywhere in the sentence.

Is there anything I could change to make it work?

@client.event
async def on_message(message):
    words = ['test']

    if message.content in words: 
        await message.channel.send("test1")

Upvotes: 0

Views: 554

Answers (1)

artie
artie

Reputation: 129

In python the format is if substring in string:

So if you have a list of words you're looking for then you need to first iterate over that list of words then check if it is in the main string:

for word in words:
    if word in message.content:

or, you can do a sneaky:

if any(map(message.content.__contains__, words)):

Which will return true if any of the words in words is in the message content.

Upvotes: 0

Related Questions