Mcrft
Mcrft

Reputation: 13

Looking in a Discord message for bad words?

I am currently trying to make my own Discord bot (I'm not very good) and I want to filter the messages the bot gets for bad words. So what I researched and tried was this:

Here I made another Python script for the words because I wanted to keep the code organized. The other script is a simple list.

from badwords import *

if badwords.badword in message:
     await message.channel.send('Thats not nice! STOP!')

I also tried the any() method but I removed that and now I don't have that anymore and it didn't work too.

Upvotes: 0

Views: 242

Answers (2)

Antricks
Antricks

Reputation: 169

I've experimented with the library you used and it seems like that library is a Chinese profanity filter. (Judging by what I've found out... It also doesn't seem very well documented.) badwords.badword("text", "/path/to/wordlist.txt") just puts the negating word 不 (bù) before words from the wordlist.

What you're more probably looking for is something like better_profanity:

from better_profanity import profanity

profanity.contains_profanity("fuck this shit")
# returns True


profanity.contains_profanity("Would you like a cup of tea, sir?")
# returns False


# better_profanity also detects 1337 speech
profanity.censor("fVcK this 5h17, I'm out!")
# returns "**** this ****, I'm out!"

Upvotes: 0

Aadil Faizal
Aadil Faizal

Reputation: 57

Is the message a string? Or is it in a list too? If it is in a string, what you can do is split the string, make it into a list where each word is an entry and check each word.

#  creating a list of the words
words = message.split()
for word in words:
    if word in badwords.badword:
        await message.channel.send('Thats not nice! STOP!')

(Note: This will work but there are probably better ways of doing this too.)

Upvotes: 0

Related Questions