0

I'm working on an IRC bot and I'm trying to make a anti-swear plugin easier.

It's currently working like this:

if text.find('swearword') != -1:
message('Please do not use that word')

But I want to make it so instead of having to make a new if for each swear word, I figured it would be easier if it was like this: if text.find(isinarray). So if it's in the array (where I will put all the swear words) it will display a message.

mustaccio
  • 18,234
  • 16
  • 48
  • 57
SDEV
  • 11
  • 2
  • 1
    Add to an array and then just loop on the array? Have some other function to add/remove from the array? – paulm Oct 07 '14 at 17:26
  • 1
    Hrm, why not just make a swear.txt and have the bot load that instead of hard coding the swear words. Then just check that with one quick search. Be aware of the Scunthorpe problem though: http://en.wikipedia.org/wiki/Scunthorpe_problem – Bjorn Oct 07 '14 at 17:27
  • `blacklist=['word1','word2', ]`, then `if word in blacklist:` – ivan_pozdeev Oct 07 '14 at 17:27
  • This seems to have some good answers, only in PHP http://stackoverflow.com/questions/273516/how-do-you-implement-a-good-profanity-filter – Bjorn Oct 07 '14 at 17:30
  • Would this work Ivan?: if text in blacklist != -1: message('Please do not use that word') – SDEV Oct 07 '14 at 17:31
  • Doesn't seem to work – SDEV Oct 07 '14 at 17:40

2 Answers2

0

here is a simple code that might help you

badwords = ["love", "peace", "hello"]
message = "hi I love you"
for badword in badwords:
  if (badword in message.lower()):
   print "oh thats bad /ban"

but I recommmend you to make a function for that

this detection system is really simple and could be improved of course

Arnaud Aliès
  • 1,079
  • 13
  • 26
0

Here is some code that actually works. It ignores bot messages alltogether.

@client.event
async def on_message(msg):
  author = msg.author
  channel = msg.channel
  for word in filtered_words:
    if word in msg.content:
      if msg.author.bot:
        return
      else:
        await msg.delete()
        await channel.send(author.mention, "You're not allowed to say that.")
        return
  await client.process_commands(msg)

Hope it helped :)

TigerNinja
  • 55
  • 7