facts: allow adding facts via IRC
This commit is contained in:
parent
934f83b734
commit
241de26dd5
@ -4,8 +4,8 @@ import logging
|
||||
|
||||
from irc.client import NickMask
|
||||
|
||||
from ircbot.lib import Plugin
|
||||
from facts.models import Fact
|
||||
from ircbot.lib import Plugin, has_permission
|
||||
from facts.models import Fact, FactCategory
|
||||
|
||||
|
||||
log = logging.getLogger('facts.ircplugin')
|
||||
@ -18,6 +18,8 @@ class Facts(Plugin):
|
||||
def start(self):
|
||||
"""Set up the handlers."""
|
||||
|
||||
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!facts\s+add\s+(\S+)\s+(.*)$',
|
||||
self.handle_add_fact, -20)
|
||||
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!facts\s+(\S+)(\s+(.*)$|$)',
|
||||
self.handle_facts, -20)
|
||||
|
||||
@ -26,6 +28,7 @@ class Facts(Plugin):
|
||||
def stop(self):
|
||||
"""Tear down handlers."""
|
||||
|
||||
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_add_fact)
|
||||
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_facts)
|
||||
|
||||
super(Facts, self).stop()
|
||||
@ -48,5 +51,18 @@ class Facts(Plugin):
|
||||
|
||||
return self.bot.reply(event, msg)
|
||||
|
||||
def handle_add_fact(self, connection, event, match):
|
||||
"""Add a new fact to the database."""
|
||||
|
||||
category_name = match.group(1)
|
||||
fact_text = match.group(2)
|
||||
|
||||
if has_permission(event.source, 'facts.add_fact'):
|
||||
# create the category
|
||||
category, created = FactCategory.objects.get_or_create(name=category_name)
|
||||
fact = Fact.objects.create(fact=fact_text, category=category, nickmask=event.source)
|
||||
if fact:
|
||||
return self.bot.reply(event, "fact added to {0:s}".format(category.name))
|
||||
|
||||
|
||||
plugin = Facts
|
||||
|
Loading…
Reference in New Issue
Block a user