Tristan T Tristan T - 2 months ago 17
Python Question

sockets irc bot not sending complete message

I am trying to make an irc bot. It connects but doesn't send the complete message. If I want to send "hello world" it only sends "hello". It just sends everything until the first space.

In this program if you type hello in irc, the bot is supposed to send hello world. But it only sends hello.

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

Answer

You forgot a : before the message. This should work:

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