-3

So currently my problem with this bot for my Twitch channel is that it prevents me from having multiple words in 1 string while Authlist is being threated as an list.

Example: I want to ban the words foo1, foo2, foo3 and foo4 but while having them all in 1 string I need to type all 4 of them in chat in order that my bot is able to ban the person, but not if he says one of the 4 words.

Thanks in advance!

import socket

authlist = "patyyebot patyye"
banword = "foo1 foo2 foo3 foo4"
server = "patyye.jtvirc.com"
name = "patyyebot"
port = 6667
channel = "#patyye"
password = "xx"
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
irc.connect((server, port))
irc.send("PASS " + password + "\n")
irc.send("NICK " + name + "\n")
irc.send("USER patyyebot patyyebot patyyebot :PatyYeBot\n")
irc.send("JOIN " + channel + "\n")
while True:

    def message(msg):
        irc.send("PRIVMSG " + channel + " :" + msg + "\n")
    def ban(msg):
        irc.send("PRIVMSG " + channel + " :/ban " + msg + "\n")


    data = irc.recv(1204)
    data = data.strip('\r\n')
    senderusr = data.split(" ")
    senderusr = senderusr[0]
    senderusr = senderusr.split("!")
    senderusr = senderusr[0]
    senderusr = senderusr.strip(":")

    print data
    if data.find == "PONG" :
        irc.send("PING")

    if "!facebook" in data and senderusr in authlist:
        message("@" + senderusr + ": Facebook is private")

    if "!twitter" in data:
        message("Follow PatyYe on Twitter: https://twitter.com/PatyYe")

    if data in banword:
        message("@" + senderusr + ":  zei een gebanned woord! Ban uitgevoerd")
        ban(senderusr)
4

2 回答 2

2

Using regular expressions you can avoid the loop and check all words in one pass.

You can censor just the banned words (if you are logging/archiving the conversations):

>>> banned_words = "phuck azz deeck peach"
>>> regexp = '|'.join(banned_words.split())
>>> message = "You son of a peach!"
>>> import re
>>> re.sub(regexp, '[beeeeeep]', message)
'You son of a [beeeeeep]!'

Or you can test for the banned words and ban the user:

>>> if re.search(regexp, message): print "Consider yourself banned, sir!"
... 
Consider yourself banned, sir!

[update]

Jon wrote:

Probably best to put banned_words into descending length order (to match longest words first) and running them through re.escape just in case... – Jon Clements

Depending on the list source you may want to escape sequences that have special meaning for regular expressions, just to be safe.

>>> ordered_list = sorted(banned_words.split(), key=lambda x: len(x), reverse=True)
>>> ordered_list
['phuck', 'deeck', 'peach', 'azz']
>>> regexp = '|'.join([re.escape(word) for word in ordered_list])
>>> regexp
'phuck|deeck|peach|azz'

You probably want to enhance the regular expression in order to make it case insensitive and to match word boundaries (preventing false positives).

It might also be a good idea to wrap the regexp in \b(...)\b, lest you accidentally ban someone for saying "impeachment" (or, more realistically, "Scunthorpe"). – Ilmari Karonen

Remember you have to escape the backslashes (or use raw strings):

>>> regexp = r'\b(' + regexp + r')\b'
>>> regexp
'\\b(phuck|deeck|peach|azz)\\b'
于 2013-07-20T15:02:13.967 回答
1

One way to do this is to use yourstring.split() to split the space-delimited string of banned words into a list:

>>> banned_string = "word1 word2 word3"
>>> banned_string.split()
['word1', 'word2', 'word3']

Then you can iterate over the words and look for them in the message.

Full example:

def checkmessage(msg):
    banned_words = "badword1 badword2 badword3"
    banned_list= banned_words.split()

    for word in banned_list:
         if word in msg:
             print("banned for saying: " + word)
             return
    print("not banned")


msg1 = "Nothing special here"
msg2 = "I say the badword2."

checkmessage(msg1)
checkmessage(msg2)

Executing that program results in:

not banned
banned for saying: badword2
于 2013-07-20T10:58:49.460 回答