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?
python Code:
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()