diff --git a/seen.py b/seen.py index 69821aeb5b..a52744750b 100644 --- a/seen.py +++ b/seen.py @@ -12,8 +12,10 @@ import datetime import pytz from willie.tools import Ddict, Nick +from willie.module import command, rule + +seen_dict = Ddict(dict) -seen_dict=Ddict(dict) def get_user_time(willie, nick): tz = 'UTC' @@ -25,13 +27,15 @@ def get_user_time(willie, nick): tz = 'UTC' return (pytz.timezone(tz.strip()), tformat or '%Y-%m-%d %H:%M:%S %Z') + +@command('seen') def seen(willie, trigger): """Reports when and where the user was last seen.""" if not trigger.group(2): willie.say(".seen - Reports when was last seen.") return nick = Nick(trigger.group(2).strip()) - if seen_dict.has_key(nick): + if nick in seen_dict: timestamp = seen_dict[nick]['timestamp'] channel = seen_dict[nick]['channel'] message = seen_dict[nick]['message'] @@ -44,17 +48,13 @@ def seen(willie, trigger): willie.say(str(trigger.nick) + ': ' + msg) else: willie.say("Sorry, I haven't seen %s around." % nick) -seen.commands = ['seen'] + +@rule('(.*)') +@priority('low') def note(willie, trigger): if trigger.sender.startswith('#'): nick = Nick(trigger.nick) seen_dict[nick]['timestamp'] = time.time() seen_dict[nick]['channel'] = trigger.sender seen_dict[nick]['message'] = trigger - -note.rule = r'(.*)' -note.priority = 'low' - -if __name__ == '__main__': - print __doc__.strip() diff --git a/version.py b/version.py index b8a72bfa47..0276170399 100644 --- a/version.py +++ b/version.py @@ -8,7 +8,7 @@ from datetime import datetime from subprocess import * -from willie import __version__ +import willie def git_info(): @@ -20,49 +20,49 @@ def git_info(): return commit, author, date -def version(willie, trigger): +@willie.module.command('version') +def version(bot, trigger): """Display the latest commit version, if Willie is running in a git repo.""" commit, author, date = git_info() - + if not commit.strip(): - willie.reply("Willie v. " + __version__) + bot.reply("Willie v. " + willie.__version__) return - willie.say(str(trigger.nick) + ": Willie v. %s at commit:" % __version__) - willie.say(" " + commit) - willie.say(" " + author) - willie.say(" " + date) -version.commands = ['version'] + bot.say(str(trigger.nick) + ": Willie v. %s at commit:" % willie.__version__) + bot.say(" " + commit) + bot.say(" " + author) + bot.say(" " + date) -def ctcp_version(willie, trigger): - willie.write(('NOTICE', trigger.nick), - '\x01VERSION Willie IRC Bot version %s\x01' % __version__) -ctcp_version.rule = '\x01VERSION\x01' -ctcp_version.rate = 20 +@willie.module.rule('\x01VERSION\x01') +@willie.module.rate(20) +def ctcp_version(bot, trigger): + bot.write(('NOTICE', trigger.nick), + '\x01VERSION Willie IRC Bot version %s\x01' % willie.__version__) -def ctcp_source(willie, trigger): - willie.write(('NOTICE', trigger.nick), - '\x01SOURCE https://github.com/Embolalia/willie/\x01') -ctcp_source.rule = '\x01SOURCE\x01' -ctcp_source.rate = 20 +@willie.module.rule('\x01SOURCE\x01') +@willie.module.rate(20) +def ctcp_source(bot, trigger): + bot.write(('NOTICE', trigger.nick), + '\x01SOURCE https://github.com/Embolalia/willie/\x01') -def ctcp_ping(willie, trigger): +@willie.module.rule('\x01PING\s(.*)\x01') +@willie.module.rate(10) +def ctcp_ping(bot, trigger): text = trigger.group() text = text.replace("PING ", "") text = text.replace("\x01", "") - willie.write(('NOTICE', trigger.nick), - '\x01PING {0}\x01'.format(text)) -ctcp_ping.rule = '\x01PING\s(.*)\x01' -ctcp_ping.rate = 10 + bot.write(('NOTICE', trigger.nick), + '\x01PING {0}\x01'.format(text)) -def ctcp_time(willie, trigger): +@willie.module.rule('\x01TIME\x01') +@willie.module.rate(20) +def ctcp_time(bot, trigger): dt = datetime.now() current_time = dt.strftime("%A, %d. %B %Y %I:%M%p") - willie.write(('NOTICE', trigger.nick), - '\x01TIME {0}\x01'.format(current_time)) -ctcp_time.rule = '\x01TIME\x01' -ctcp_time.rate = 20 + bot.write(('NOTICE', trigger.nick), + '\x01TIME {0}\x01'.format(current_time)) diff --git a/xkcd.py b/xkcd.py index 610880aaff..9b413bdd7e 100644 --- a/xkcd.py +++ b/xkcd.py @@ -10,11 +10,14 @@ import random from willie.modules.search import google_search from willie.modules.url import find_title +from willie.module import command import urllib2 from lxml import etree import re -def xkcd(willie, trigger): + +@command('xkcd') +def xkcd(bot, trigger): """ .xkcd - Finds an xkcd comic strip. Takes one of 3 inputs: If no input is provided it will return a random comic @@ -28,39 +31,37 @@ def xkcd(willie, trigger): max_int = int(newest.split("/")[-3]) # if no input is given (pre - lior's edits code) - if not trigger.group(2): # get rand comic + if not trigger.group(2): # get rand comic random.seed() - website = "http://xkcd.com/%d/" % random.randint(0,max_int+1) + website = "http://xkcd.com/%d/" % random.randint(0, max_int + 1) else: query = trigger.group(2).strip() # numeric input! get that comic number if it exists if (query.isdigit()): if (int(query) > max_int): - willie.say("Sorry, comic #" + query + " hasn't been posted yet. The last comic was #%d" % max_int) + bot.say("Sorry, comic #" + query + " hasn't been posted yet. The last comic was #%d" % max_int) return - else: website = "http://xkcd.com/" + query - + else: + website = "http://xkcd.com/" + query + # non-numeric input! code lifted from search.g else: - if (query.lower() == "latest" or query.lower() == "newest"): # special commands + if (query.lower() == "latest" or query.lower() == "newest"): # special commands website = "https://xkcd.com/" - else: # just google + else: # just google try: query = query.encode('utf-8') except: pass - website = google_search("site:xkcd.com "+ query) - chkForum = re.match(re.compile(r'.*?([0-9].*?):.*'), find_title(website)) # regex for comic specific forum threads + website = google_search("site:xkcd.com " + query) + chkForum = re.match(re.compile(r'.*?([0-9].*?):.*'), find_title(website)) # regex for comic specific forum threads if (chkForum): website = "http://xkcd.com/" + chkForum.groups()[0].lstrip('0') - if website: # format and say result + if website: # format and say result website += ' [' + find_title(website)[6:] + ']' - willie.say(website) - elif website is False: willie.say("Problem getting data from Google.") - else: willie.say("No results found for '%s'." % query) -xkcd.commands = ['xkcd'] -xkcd.priority = 'low' - -if __name__ == '__main__': - print __doc__.strip() + bot.say(website) + elif website is False: + bot.say("Problem getting data from Google.") + else: + bot.say("No results found for '%s'." % query)