sockets irc bot 不发送完整消息

sockets irc bot not sending complete message

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

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

您在消息前忘记了一个 : 。这应该有效:

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