dr.botzo/dr_botzo/facts/ircplugin.py

45 lines
1.1 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
import logging
from ircbot.lib import Plugin
from facts.models import Fact
log = logging.getLogger('facts.ircplugin')
class Facts(Plugin):
"""Present facts to IRC."""
def start(self):
"""Set up the handlers."""
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!facts\s+(\S+)(\s+(.*)$|$)',
self.handle_facts, -20)
super(Facts, self).start()
def stop(self):
"""Tear down handlers."""
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_facts)
super(Facts, self).stop()
def handle_facts(self, connection, event, match):
"""Respond to the facts command with desired fact."""
category = match.group(1)
regex = None
if match.group(2) != '':
regex = match.group(3)
fact = Fact.objects.random_fact(category, regex)
if fact:
return self.bot.reply(event, fact.fact)
plugin = Facts