dr.botzo/modules/Countdown.py

77 lines
3.4 KiB
Python
Raw Normal View History

2010-07-27 20:35:01 -05:00
from Module import Module
from irclib import irclib
from ConfigParser import NoOptionError
class Countdown(Module):
# Class that adds a countdown item to the bot
def __init__(self, config, server, modlist, rehash):
super(Countdown, self).__init__(config, server, modlist, rehash)
modlist.append(self)
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())
replystr = 'added countdown item ' + whats[2]
if replypath is None:
return replystr
else:
connection.privmsg(replypath, replystr)
elif whats[1] == 'remove':
try:
if self.config.remove_option('countdown', whats[2]):
replystr = 'removed countdown item ' + whats[2]
if replypath is None:
return replystr
else:
connection.privmsg(replypath, replystr)
except NoSectionError: pass
elif whats[1] == 'list':
try:
cdlist = self.config.options('countdown')
cdlist.remove('debug')
cdlist.sort()
liststr = ', '.join(cdlist)
if replypath is None:
return liststr
else:
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 + ')'
if replypath is None:
return relstr
else:
connection.privmsg(replypath, relstr)
except NoOptionError: pass
# kate: indent-mode python;indent-width 4;replace-tabs on;