所以目前我的Twitch频道这个机器人的问题在于,当Authlist被列为列表时,它阻止我在1个字符串中有多个单词。
实施例: 我想禁止foo1,foo2,foo3和foo4这两个词,但是当我们将它们全部放在1个字符串中时,我需要在聊天中键入所有4个,以便我的机器人能够禁止这个人,但是如果他说其中一个4个字。
提前致谢!
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)
答案 0 :(得分:2)
使用正则表达式可以避免循环并一次检查所有单词。
您可以只审查被禁止的字词(如果您正在记录/存档会话):
>>> 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]!'
或者你可以测试被禁止的单词并禁止用户:
>>> if re.search(regexp, message): print "Consider yourself banned, sir!"
...
Consider yourself banned, sir!
[更新]
Jon写道:
可能最好将banned_words放入降序长度(首先匹配最长的单词)并通过re.escape运行它们以防万一... - Jon Clements
根据列表源,您可能想要转义对正则表达式具有特殊含义的序列,只是为了安全起见。
>>> 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'
您可能希望增强正则表达式,以使其不区分大小写并匹配单词边界(防止误报)。
将正则表达式包装在\ b(...)\ b中也是一个好主意,以免你不小心禁止某人说“弹劾”(或更现实地说,“斯坎索普”)。 - Ilmari Karonen
记住你必须逃避反斜杠(或使用原始字符串):
>>> regexp = r'\b(' + regexp + r')\b'
>>> regexp
'\\b(phuck|deeck|peach|azz)\\b'
答案 1 :(得分:1)
执行此操作的一种方法是使用yourstring.split()
将空格分隔的禁止字符串拆分为列表:
>>> banned_string = "word1 word2 word3"
>>> banned_string.split()
['word1', 'word2', 'word3']
然后你可以迭代这些单词并在邮件中查找它们。
完整示例:
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)
执行该程序会导致:
not banned
banned for saying: badword2