Skip to content
Advertisement

Python Discord Bot that responds to keywords while not being case sensitive

I’m very new to python as in I’m making this bot for fun, but I’ve been trying to make a simple discord.py bot that will respond to keywords that aren’t case sensitive as well as detect the word in a message. I’ve been able to have it be non case sensitive, but the main issue that I’m having is being able to detect the keyword in the message along with that.

The snippet below is what I’ve used for being non case sensitive, but I just haven’t figured out how to allow it to find it in a message.

if message.content.lower() == "ok":
        await message.channel.send('Ok') 

Advertisement

Answer

You can check if the string is already in the message:

if "ok" in message.content.lower():
    # do stuff

However, this will match things like ok, ok and also this (these are good), but also in the middle of words: uhueanbfononoeiw**ok**ajjasoifjojq.

If you want it to match only word boundaries, you can do:

if "ok" in message.content.lower().split(' '):
    # do stuff

If you want to be even more fancy you can look into levenshtein distance.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement