2014-07-26 4 views
1

친구의 라이브 스트림에 트위터 봇을 만들려고하는데 메시지를 보내거나 명령을 인식하는 데 문제가 있습니다.IRC 봇이 메시지를 보내지 않아도됩니다.

내 코드 : 나는 엉망했습니다 수있는 곳

import socket,string 
HOST = "irc.twitch.tv" 
PORT = 6667 
NICK = "Axiom" 
IDENT = "Axiom" 
REALNAME = "Axiom" 
CHANNEL = "#itwreckz" 
PASSWORD = "oauth:PASSHERE" 
readbuffer = "" 
print "Connecting.." 
t = socket.socket() 
t.connect((HOST, PORT)) 
print "Connected! Logging in" 
t.send("PASS %s\r\n" % PASSWORD) 
t.send("NICK %s\r\n" % NICK) 
t.send("USER %s %s BOT :%s\r\n" % (IDENT, HOST, REALNAME)) 
print "Logged in. Joining Channel" 
t.send("JOIN %s\r\n" % CHANNEL) 
print "JOINED!" 

while 2>1: 
    readbuffer = readbuffer+t.recv(1024) 
    taco = string.split(readbuffer, "\n") 
    readbuffer = taco.pop() 
    for line in taco: 
     line=string.rstrip(line) 
     line=string.split(line) 
     if len(line) > 3: 
      print line 
      command = line 
      if "!test" in command: 
       print "If you see this, it recognizes command." #Doesn't show 
       t.send("PRIVMSG %s :IT WORKS!\r\n" % CHANNEL) #This either 
     if(line[0]=="PING"): 
      t.send("PONG %s\r\n" % line[1]) 

사람이 알고 있나요?

if len(line) >3:은 채팅에서 전송 된 메시지를 표시하므로 작동합니다 (3 자 이상인 경우에만 해당). 그래서 나는 그 물건을 잘 설정했다고 생각합니다. 메시지 등을 인쇄합니다.

나는 명령을 내릴 수없는 것 같습니다.


편집 : 이것은 정말 오래된 게시물입니다,하지만 난 내가 더 잘 작동하도록 전체 IRC 봇을 리메이크 결국 지적하고 싶습니다. 아마도 관심있는 사람들을위한

:

import socket,time 
network = 'irc.example.net' 
port = 6667 
channels = ['#Channel1','#Channel2'] #Add as many as you want 
nick = 'myIRCBot' 
identify = True 
password = 'superAwesomePassword' 
irc = socket.socket (socket.AF_INET, socket.SOCK_STREAM) 
print "Connecting to "+network+" ..." 
irc.connect ((network, port)) 
print "Changing nick to "+nick+"..." 
irc.send ('NICK '+nick+'\r\n') 
if identify == True: 
     print "Verifying password..." 
     irc.send("PASS %s\n" % (password)) 
print "Setting login data..." 
irc.send ('USER '+nick+' '+nick+' '+nick+' :'+nick+' IRC\r\n') 
time.sleep(1) 
for channel in channels: 
     print "Joining "+channel+"..." 
     irc.send ('JOIN '+channel+'\r\n') 
     irc.send ('PRIVMSG '+channel+' :'+nick+' Started! Type .help for more\r\n') 
     time.sleep(1) 
print nick+" bot started." 
while True: 
     data = irc.recv(4096) 
     if data.find('PING') != -1: 
       irc.send('PONG '+data.split()[1]+'\r\n') 
     try: 
       user = data.split("!",1)[0].replace(":","",1) 
       vhost = data.split(" ",1)[0].split("!",1)[1] 
       dType = data.split(" ",1)[1].split(" ",1)[0] 
       chan = data.split(" ",1)[1].split(" ",1)[1].split(" ",1)[0] 
       msg = data.split(" ",1)[1].split(" ",1)[1].split(" ",1)[1].replace(":","",1).replace("\n","",1).replace("\r","",1) 
       if msg == '.help': 
         irc.send ('PRIVMSG '+chan+' :This is the only command!\r\n') 
       print user+" ("+vhost+") "+dType+" to "+chan+": "+msg 
     except: 
       pass 

답변

0

라인 line=string.rstrip(line)에서, 당신은 줄에서 공백을 후행 제거된다. 다음 줄의 line=string.split(line)에서 줄을 단어로 나눕니다. 그 다음에 line은 메시지의 각 단어 목록입니다. 따라서 실제로 len(line) > 3을 확인할 때 줄에 단어가 4 개 이상 있는지 확인해야합니다.

IRC 메시지에서 명령을 추출하려면 가장 쉬운 방법 (완전히 신뢰할 수는 없지만)은 if line[0] == "PING"을 확인하는 것처럼 지금 가지고있는 단어 목록의 첫 번째 요소를 가져 오는 것입니다. 이 경우 "PING"이 명령입니다.

이제 IRC 프로토콜 명령이 아니라 다른 사용자가 보낸 메시지로 명령을 보내는 것이 다른 이야기입니다. 개인 또는 채널 - - 모든 메시지가 수신이 형식에 따라 : 당신이의 message received 부분에 "!test"가 포함 된 다른 사용자의 메시지를 찾고 있다면 그래서

PRIVMSG <message target> :<message received> 

합니다. 가장 가능성있는 일은 string.split(line) 일 때 message received 부분의 첫 번째 단어가 "!test"이 아닌 ":!test" 일 때 발생합니다. 당신이 정말 당신이 전체 IRC PRIVMSG 그대로의 바로 메시지 부분을 갖고 싶어해야 할 것은, 콜론에 대한보고 라인 message = line[line.find(":"):][1:]이 좀 지저분하다

words = line.split() 
if words[0] == "PRIVMSG": 
    message = line[line.find(":"):][1:] 
    message_words = message.split() 
    if "!test" in message_words: 
     print "If you see this, it recognizes the command" 

즉, 그 다음 모든 것을 잡아,하지만입니다 그곳에서 일어나는 모든 일은 제가 라인에서 ":"의 첫 번째 사건을 발견하고 콜론을 잘라내는 것입니다.

관련 문제