-2

I am trying to make my bot delete messages containing word "nom" how can I make my bot delete message if it's not starting with this word? actually this only works when the message starts with this word, and I don't know how to make it work everytime this word is in the message. I'd also like to make my bot delete it if someone tries to write something like ".nom", "NoM" etc. Can anyone help me?

@client.event

async def on_message(message):

     if message.content == "nom":

          await client.delete_message(message)
Taku
  • 31,927
  • 11
  • 74
  • 85
Alpha Wolfie
  • 1
  • 1
  • 1
  • 2
    Possible duplicate of [What’s a good Python profanity filter library?](https://stackoverflow.com/questions/3531746/what-s-a-good-python-profanity-filter-library) – DarkBee Jan 26 '18 at 12:19

2 Answers2

0

You're currently checking if the content equals "nom". To check if it actually contains the word, try this:

if "nom" in message.content.lower(): await client.delete_message(message)

This also recognises thins like "NoM" as it converts the message to lowercase.

  • 1
    What about if `OP` wanted to flag `tit` and someone typed in `apatite`? Above snippet would delete that message as well – DarkBee Jan 26 '18 at 11:59
  • True, but how else would you also flag `.nom`, `nom/` etc.? –  Jan 26 '18 at 12:10
  • Just pointing out, it's not as straightforward but see [here](https://stackoverflow.com/questions/3531746/what-s-a-good-python-profanity-filter-library) – DarkBee Jan 26 '18 at 12:19
  • thank you very much, but after I did this other commands stopped working, Is it supposed to be high in code or something? – Alpha Wolfie Jan 26 '18 at 16:15
  • 1
    That.. Shouldn't happen, no. Are you sure you put it in place of your current `if` statement (under `on_message()`)? –  Jan 26 '18 at 22:02
0

This would be my recommended way of doing it

import shlex

split = shlex.split(message.content.lower())
if 'nom' in split:
    # rewrite
    await message.delete()

    # async
    await client.delete_message(message)

Although if you are working with dynamic lists of profanity i reccomend doing something like

split = shlex.split(message.content.lower())
if any(substring for bad in bad_words for substring in split if bad in substring):
    # rewrite
    await message.delete()

    # async
    await client.delete_message(message)

The genexr in the if statement was taken from here.

mental
  • 836
  • 6
  • 18