2013-07-20 33 views
-3

所以目前我的Twitch頻道的這個機器人的問題是它阻止我在一個字符串中有多個單詞,而Authlist被作爲一個列表進行威脅。IRC bot,製作被禁止的單詞列表?

例如: 我想禁止單詞foo1,foo2,foo3和foo4,但是當它們全部在1個字符串中時,我需要在聊天中鍵入所有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

這是一個小我很難準確地瞭解你的意思。 如果所有4個單詞出現或只有1個單詞足夠,您是否只想禁止? 你能改述你的標準嗎? – immortal

+0

我想他想禁止任何一個單詞。你應該將字符串分割成一個數組(空格是分隔符)。然後遍歷數組並查找單詞。 – varesa

回答

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 
+0

謝謝,這確實解決了! –

+0

我確實增加了一些功能,讓文件中的單詞現在可以檢查它是否被禁止的單詞! DEF checkmessage(MSG): \t banfile =開放( 'file.txt的', 'R') \t banned_words = banfile.read() \t banned_list = banned_words.split() \t banfile.close() –

+0

@ PatrickKnobbout很好,你有它的工作。您應該將此答案標記爲已接受,以便問題得到「解決」。隨時upvote太:) – varesa

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! 

[更新]

喬恩寫道:

或許最好把banned_words成遞減長度順序(匹配最長的單詞第一個),併爲了以防萬一運行他們通過re.escape ... - 喬恩克萊門茨

根據列表源可能要轉義序列對正則表達式有特殊意義,爲了安全起見。

>>> 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中包裝正則表達式也許是個好主意,免得你意外地禁止某人說「彈」「(或者更現實的說,是」Scunthorpe「)。 - ILMARI Karonen

記住,你必須逃離反斜槓(或使用原始字符串):

>>> regexp = r'\b(' + regexp + r')\b' 
>>> regexp 
'\\b(phuck|deeck|peach|azz)\\b' 
+2

可能最好將banned_words放入長度遞減順序(最先匹配最長的單詞)並通過're.escape'運行以防萬一...... –

+0

@JonClements:很好的建議。 –

+0

將正則表達式包裝在'\ b(...)\ b'中也許是一個好主意,以免意外地禁止某人說「彈」「(或者更現實的說,」[Scunthorpe](http:// en.wikipedia.org/wiki/Scunthorpe_problem)「)。 –