2016-10-02 75 views
0

我正在嘗試製作一個irc bot。它連接但不發送完整的消息。如果我想發送「hello world」,它只會發送「hello」。它只是發送一切,直到第一個空格。插槽irc bot不發送完整消息

在這個程序中,如果你在irc中輸入hello,bot應該發送hello world。但它只發送你好。

import socket 

channel = "#bots" 
server = "chat.freenode.org" 
nickname = "my_bot" 


class IRC: 
    irc = socket.socket() 

    def __init__(self): 
     self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

    def send(self, chan, msg): 
     self.irc.send("PRIVMSG " + chan + " " + msg + "\n") 

    def connect(self, server, channel, botnick): 
     # defines the socket 
     print("connecting to: " + server) 
     self.irc.connect((server, 6667)) # connects to the server 

     self.irc.send("NICK %s\n" % botnick) 
     self.irc.send("USER %s %s Ibot :%s\n" % (botnick, botnick, botnick)) 
     self.irc.send("JOIN %s\n" % channel) 
     self.irc.send("PRIVMSG %s :Hello Master\n" % channel) 

    def get_text(self): 
     text = self.irc.recv(2040) # receive the text 

     if text.find('PING') != -1: 
      self.irc.send('PONG ' + text.split()[1] + 'rn') 

     return text 

irc = IRC() 

irc.connect(server, channel, nickname) 

while True: 
    text = irc.get_text().strip() 

    if "hello" in text.lower(): 
     irc.send(channel, "hello world") 

    print text 

回答

2

你忘了:在留言之前。這應該工作:

def send(self, chan, msg): 
    self.irc.send("PRIVMSG " + chan + " :" + msg + "\n")