From e8d9228b61f771c906abeb7678e02fc3a1bf53f7 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Sun, 25 Jul 2010 21:11:27 -0500 Subject: [PATCH] convert countdown to a Module. one of many steps in the plan for recursion --- dr.botzo.py | 112 +++++++++++++++++++++++++++------------------------- 1 file changed, 58 insertions(+), 54 deletions(-) diff --git a/dr.botzo.py b/dr.botzo.py index 95238f5..cf34db0 100755 --- a/dr.botzo.py +++ b/dr.botzo.py @@ -127,6 +127,63 @@ class GoogleTranslate(Module): translation = translation[:end_idx] connection.privmsg(replypath, translation) +class Countdown(Module): + """Class that adds a countdown item to the bot + """ + + def __init__(self, config, server): + super(Countdown, self).__init__(config, server) + + def register_handlers(self, server): + server.add_global_handler('pubmsg', self.on_pubmsg) + server.add_global_handler('privmsg', self.on_privmsg) + + def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked): + whats = what.split(' ') + if whats[0] == 'countdown' and len(whats) >= 2: + if whats[1] == 'add' and len(whats) >= 4: + item = whats[2] + target = parse(' '.join(whats[3:]), default=datetime.now().replace(tzinfo=tzlocal())) + if not self.config.has_section('countdown'): + self.config.add_section('countdown') + + self.config.set('countdown', item, target.astimezone(tzutc()).isoformat()) + connection.privmsg(replypath, 'added countdown item ' + whats[2]) + elif whats[1] == 'remove': + try: + if self.config.remove_option('countdown', whats[2]): + connection.privmsg(replypath, 'removed countdown item ' + whats[2]) + except NoSectionError: pass + elif whats[1] == 'list': + try: + cdlist = self.config.options('countdown') + cdlist.remove('debug') + cdlist.sort() + liststr = ', '.join(cdlist) + connection.privmsg(replypath, liststr) + except NoSectionError: pass + else: + try: + timestr = self.config.get('countdown', whats[1]) + time = parse(timestr) + rdelta = relativedelta(time, datetime.now().replace(tzinfo=tzlocal())) + relstr = whats[1] + ' will occur in ' + if rdelta.years != 0: + relstr += str(rdelta.years) + ' years ' + if rdelta.months != 0: + relstr += str(rdelta.months) + ' months ' + if rdelta.days != 0: + relstr += str(rdelta.days) + ' days ' + if rdelta.hours != 0: + relstr += str(rdelta.hours) + ' hours ' + if rdelta.minutes != 0: + relstr += str(rdelta.minutes) + ' minutes ' + if rdelta.seconds != 0: + relstr += str(rdelta.seconds) + ' seconds' + #relstr += ' (' + timestr + ')' + connection.privmsg(replypath, relstr) + except NoOptionError: pass + ##### # sub_join_channel # join a channel when told to by an admin @@ -247,57 +304,6 @@ def sub_change_nick(connection, event, nick, userhost, replypath, what, admin_un config.set('IRC', 'nick', newnick) connection.privmsg(replypath, 'changed nickname') -##### -# sub_countdown -# add a countdown item to the bot -##### - -def sub_countdown(connection, event, nick, userhost, replypath, what, admin_unlocked): - whats = what.split(' ') - if whats[0] == 'countdown' and len(whats) >= 2: - if whats[1] == 'add' and len(whats) >= 4: - item = whats[2] - target = parse(' '.join(whats[3:]), default=datetime.now().replace(tzinfo=tzlocal())) - if not config.has_section('countdown'): - config.add_section('countdown') - - config.set('countdown', item, target.astimezone(tzutc()).isoformat()) - connection.privmsg(replypath, 'added countdown item ' + whats[2]) - elif whats[1] == 'remove': - try: - if config.remove_option('countdown', whats[2]): - connection.privmsg(replypath, 'removed countdown item ' + whats[2]) - except NoSectionError: pass - elif whats[1] == 'list': - try: - cdlist = config.options('countdown') - cdlist.remove('debug') - cdlist.sort() - liststr = ', '.join(cdlist) - connection.privmsg(replypath, liststr) - except NoSectionError: pass - else: - try: - timestr = config.get('countdown', whats[1]) - time = parse(timestr) - rdelta = relativedelta(time, datetime.now().replace(tzinfo=tzlocal())) - relstr = whats[1] + ' will occur in ' - if rdelta.years != 0: - relstr += str(rdelta.years) + ' years ' - if rdelta.months != 0: - relstr += str(rdelta.months) + ' months ' - if rdelta.days != 0: - relstr += str(rdelta.days) + ' days ' - if rdelta.hours != 0: - relstr += str(rdelta.hours) + ' hours ' - if rdelta.minutes != 0: - relstr += str(rdelta.minutes) + ' minutes ' - if rdelta.seconds != 0: - relstr += str(rdelta.seconds) + ' seconds' - #relstr += ' (' + timestr + ')' - connection.privmsg(replypath, relstr) - except NoOptionError: pass - ##### # sub_dice # roll dice, primarily for roleplaying games @@ -417,7 +423,6 @@ def on_privmsg(connection, event): # standard commands sub_report_seen(connection, event, nick, userhost, replypath, what, admin_unlocked) - sub_countdown(connection, event, nick, userhost, replypath, what, admin_unlocked) sub_dice(connection, event, nick, userhost, replypath, what, admin_unlocked) ##### @@ -459,7 +464,6 @@ def on_pubmsg(connection, event): # standard commands sub_report_seen(connection, event, nick, userhost, replypath, what, admin_unlocked) - sub_countdown(connection, event, nick, userhost, replypath, what, admin_unlocked) sub_dice(connection, event, nick, userhost, replypath, what, admin_unlocked) ##### @@ -497,8 +501,8 @@ server.add_global_handler("welcome", on_connect) server.add_global_handler('privmsg', on_privmsg) server.add_global_handler('pubmsg', on_pubmsg) -# test gt = GoogleTranslate(config, server) +count = Countdown(config, server) # loop forever irc.process_forever()