PDA

View Full Version : IRC Bot with python help



Awkwardsaw
09-06-2009, 08:29 AM
i'm trying to make an irc bot with python, but i'm having trouble having it respond.

how do you fix it, and how does it work and all that good stuff? :)



import socket

class Bot :

def __init__(self, server="milamber.ny.us.swiftirc.net", port=6667):
"""creates the socket object and connects to the server"""
self.sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
address = (server, port)
self.sock.connect(address)
self.input = self.sock.makefile('rb',0)
self.output = self.sock.makefile('wb',0)

def identify(self, nick = 'AkwardBot', realname = 'AkwardBot'):
"""passes nickname and username to the server"""
self.nick = nick
self.output.write('NICK '+nick+'\r\n')
self.output.write('USER '+nick+' 8 * :'+realname+'\r\n')

def join_channel(self, channel = '#ethan') :
""" Join the channel. """
self.channel = channel
done = False
while not done :
inMsg = self.input.readline().strip()
print inMsg
if inMsg.find('PRIVMSG') != -1 :
done = True
self.output.write('JOIN '+self.channel+'\r\n')
print self.input.readline().strip()

def work(self):
"""Stay in the channel"""
message = self.input.readline().strip()
while True:
if message.find('PRIVMSG') != -1:
self.parseMessage(message)
message = message.split()
if message[0] is 'PING':
self.output.write('PONG '+message[1]+'\n')

message = self.input.readline().strip()
#this is the responding function, whats it do, and how do i fix it?
def parseMessage(self,message):
msg = message.split(':')[2]
author = message.split(':')[1].split('!')[0]
print "%s : %s" % (author,msg)
if msg.find('hi '+self.nick) != -1 :
self.output.write('PRIVMSG '+self.channel+' : '+author+', hi\r\n')


bot = Bot()
bot.identify()
bot.join_channel()
bot.work()

noidea
09-06-2009, 09:43 AM
Ask NxTitle. He is python wiz :S

NxTitle
09-06-2009, 10:06 AM
LMAO thanks noidea.

This is the weirdest bot I have ever seen. Why would anything ever redirect standard input and output to sockets, and not just use the sockets? That stops it from allowing you to have debugging output. Either way, it should be something I can work around (after all, there is standard error output as well :P )

I'll definitely take a look at it and come back if I can get it to work.

PS. is this original work?

Awkwardsaw
09-06-2009, 10:27 AM
LMAO thanks noidea.

This is the weirdest bot I have ever seen. Why would anything ever redirect standard input and output to sockets, and not just use the sockets? That stops it from allowing you to have debugging output. Either way, it should be something I can work around (after all, there is standard error output as well :P )

I'll definitely take a look at it and come back if I can get it to work.

PS. is this original work?

no, its not my original work. i studied it from a tut, and may have copypasta'd a few lines :p

also, me != sockets, so i might have to read up on them later