Search code examples
pythonbotsirc

Error in Python IRC bot


I was making a simple python IRC bot for a channel in freenode, just for fun. It was working all right with the following code for connecting to the server:

irc.connect((server,6667))
irc.send("USER "+botnick+" "+botnick+" "+botnick+" :Howdy mate!\n")
irc.send("NICK "+botnick+"\n")
irc.send("PRIVMSG nickserv :iNOOPE\r\n") #I don't know what this does
irc.send("JOIN "+channel+"\n")

(Sub-question: What does that 4th line do? )

But the while loop was triggering only when irc.recv(2040) was returning something. So I followed online examples and added a

irc.setblocking(False)

after

irc.connect((server,6667))

and a

time.sleep(1)

at the beginning of the while loop.

So now the connecting part looked like this:

irc.connect((server,6667))
irc.setblocking(False)
irc.send("USER "+botnick+" "+botnick+" "+botnick+" :Howdy mate!\n")
irc.send("NICK "+botnick+"\n")
irc.send("PRIVMSG nickserv :iNOOPE\r\n")
irc.send("JOIN "+channel+"\n")

But now when I try to run the program, it shows an error:

irc.send("USER "+botnick+" "+botnick+" "+botnick+" :Howdy mate!\n")
TypeError: 'str' does not support the buffer interface

I can't solve this error and it disappears when I remove irc.setblocking(False). But I really need the loop to execute regardless of irc.recv(2040) returning something or not...So can anyone figure out the error?

EDIT: The error above showed up when I ran this in the python shell (Because CMD was closing too quickly for me to read the error). Now I managed to record and view the error in CMD, and it says:

text=irc.recv(2040)
A non blocking socket operation could not be completed immediatly.

Help?


Solution

  • Ok I solved it myself. The text=irc.recv(2040) was the issue. I put it in a try...except block and it solved the problem.