class Bot(irc.IRCClient): """Core Bot events, subclassed from Twisted's IRCClient Respond to privmsgs in channels and wrap self.msg to delegate replies based on the context (query versus public channel). This also maintains the Redis and last.fm connections while creating contacts as users talk. """ def __init__(self, nickname, chans, fact): """initialize the Bot info, Redis client, and last.fm connection""" self.nickname = nickname self.chans = chans self.factory = fact self.db = Contacts() self.youtube = Youtube(secrets.YOUTUBE_API_KEY) self.commands = Commands(self) self.last = pylast.LastFMNetwork(api_key=secrets.LAST_API_KEY, api_secret=secrets.LAST_API_SECRET, username=secrets.LAST_USER, password_hash=secrets.LAST_PASS_HASH) def _isPrivate(self, nick, channel): """sets the private context based on channel or user""" return (channel == self.nickname and nick != self.nickname) def signedOn(self): for chan in self.chans: self.join(chan) def msg(self, contact, message): """wraps self.msg to delegate the reply""" channel = contact.nick if contact.private else contact.channel irc.IRCClient.msg(self, channel, message) def privmsg(self, user, channel, message): """manages contacts based on message and dispatches it to Commands Interface with Redis for getting, or creating, the Contact and setting its context, then passing it off to be parsed. """ contact = self.db.get(user) # update private context for replies to existing contact if contact: private = self._isPrivate(contact.nick, channel) contact.channel = channel contact.private = private # if new contact, create and set private context else: contact = Contact(user, channel) contact.private = self._isPrivate(contact.nick, channel) self.db.set(contact.user, contact) # only respond if it's properly said if contact.private or message.startswith("!"): self.commands.parse(contact, message)
def __init__(self, nickname, chans, fact): """initialize the Bot info, Redis client, and last.fm connection""" self.nickname = nickname self.chans = chans self.factory = fact self.db = Contacts() self.youtube = Youtube(secrets.YOUTUBE_API_KEY) self.commands = Commands(self) self.last = pylast.LastFMNetwork(api_key=secrets.LAST_API_KEY, api_secret=secrets.LAST_API_SECRET, username=secrets.LAST_USER, password_hash=secrets.LAST_PASS_HASH)