Reputation: 309
I'm trying to make a profanity filter for my bot. I allowed server admins to make their own list of bad words which my bot uses for their server. In the on_message
function I used this code to check if a user's message contains a word from the filter:
file = open(f"configs/antiswearvalues/customantiswear/{message.guild.id}.txt")
res = [ele for ele in file.read() if (ele in message.content)]
if bool(res):
await message.channel.send("you did a bad. time for the boot.")
The problem is the bot still said you did a bad. time for the boot.
even if the message contained a snippet from the elements and not an entire element.
So if the list was ['omg', 'crap', 'hell']
, the bot would still warn the user if their message contained h
or cr
because hell
and crap
contain those.
My question is: How can I make my bot properly check if a user's message contains an element from a list?
Upvotes: 1
Views: 4490
Reputation: 3994
You should consider using json
. Getting a word blacklist will be way easier than parsing a txt file without using any library:
Your json file (eg. blacklist.json):
['omg', 'crap', 'hell']
Your python code:
from json import loads
@client.event
async def on_message(message):
with open("blacklist.json", "r"):
data = loads(file.read())
for word in data:
if word in message.content:
await message.channel.send("you did a bad. time for the boot.")
return
Your code isn't working as intended because you didn't parsed your file data, you were exploring a string, not a list.
Upvotes: 3