class MusicBot(irc.IRCClient): """A logging IRC bot.""" commandPrefix = "!music" def __init__(self): self.commandHandler = CommandHandler(CommandManager()) def connectionMade(self): irc.IRCClient.connectionMade(self) self.logger = MessageLogger(open(self.factory.filename, "a")) self.logger.log("[connected at %s]" % time.asctime(time.localtime(time.time()))) def connectionLost(self, reason): irc.IRCClient.connectionLost(self, reason) self.logger.log("[disconnected at %s]" % time.asctime(time.localtime(time.time()))) self.logger.close() # callbacks for events def signedOn(self): """Called when bot has succesfully signed on to server.""" self.join(self.factory.channel) def privmsg(self, user, channel, msg): """This will get called when the bot receives a message.""" user = user.split("!", 1)[0] self.logger.log("<%s> %s" % (user, msg)) if channel == self.factory.channel: if str.startswith(msg, self.commandPrefix): print "command prefix found!\n" words = msg.split(" ") commandName = "help" if len(words) > 1: commandName = words[1] arguments = words[2::] output = self.commandHandler.handleCommand(self, user, commandName, arguments) if output is not None: self.sendMessage(output) def registerCommand(self, command): self.commandHandler.getCommandManager().addCommand(command) def sendMessage(self, message): print "sending message" print message lines = message.split("\n") for line in lines: self.say(self.factory.channel, line) def getCommandHandler(self): return self.commandHandler def getMaster(self): return self.factory.master