Search code examples
pythonbotsirctwitch

Primitive Python IRC Chat bot for twitch


I'm currently working on an IRC bot for Twitch.tv and I was wondering how I can implement a banned words list? Here is what I have so far and I'm stumped because of my limited knowledge of python. Everything is working great so far except checking to see if banned words are in the message. This is the bit of code in question:

if bannedWords.split in message:
                sendMessage(s, "/ban " + user)
                break

I was thiking of checking a list to see if the message containts anything from the list?

bannedWords = ["badword1", "badword1"]

But I'm just not sure..

import string
from Read import getUser, getMessage
from Socket import openSocket, sendMessage
from Initialize import joinRoom

s = openSocket()
joinRoom(s)
readbuffer = ""
bannedWords = ["badword1", "badword1"]
while True:
        readbuffer = readbuffer + s.recv(1024)
        temp = string.split(readbuffer, "\n")
        readbuffer = temp.pop()

        for line in temp:
            print(line)
            if "PING" in line:
                s.send(line.replace("PING", "PONG"))
                break
            user = getUser(line)
            message = getMessage(line)
            print user + " typed :" + message
            if bannedWords.split in message:
                sendMessage(s, "/ban " + user)
                break

Thanks in advance!!


Solution

  • If you want exact matches, use a set of words, call lower on the string and check if the set of bad words is disjoint or not:

    banned_set = {"badword1", "badword2"}
    if banned_set.isdisjoint(message.lower().split())
       # no bad words
    

    if "foo" was a banned and "foobar" was perfectly valid then using in/__contains__ will wrongly filter the words so you need to carefully decide what way to go.

    if banned_set.isdisjoint(message.lower().split()) evaluate to True it is safe to proceed:

    In [3]: banned_set = {"badword1", "badword2"}
    
    In [4]: banned_set.isdisjoint("foo bar".split())
    Out[4]: True
    
    In [5]: banned_set.isdisjoint("foo bar badword1".split())
    Out[5]: False