套接字irc bot没有发送完整的消息

时间:2016-10-02 16:47:38

标签: python sockets irc

我正在尝试制作一个irc机器人。它连接但不发送完整的消息。如果我想发送" hello world"它只发送"你好"。它只是将所有内容发送到第一个空间。

在这个程序中如果你在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

1 个答案:

答案 0 :(得分:2)

你忘记了:在消息之前。这应该有效:

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