Browse Source

new coat of paint on facts: !f shortcut, natural language fact adding

note that for now anyone can add a fact, going to assume responsible
usage on the part of users on my networks
master
Brian S. Stephan 10 months ago
parent
commit
739d3fa2b7
  1. 27
      facts/ircplugin.py

27
facts/ircplugin.py

@ -3,8 +3,8 @@ import logging
from irc.client import NickMask
from ircbot.lib import Plugin, has_permission
from facts.models import Fact, FactCategory
from ircbot.lib import Plugin
log = logging.getLogger('facts.ircplugin')
@ -14,10 +14,14 @@ 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)
self.connection.reactor.add_global_regex_handler(
['pubmsg', 'privmsg'], r'remember\s+that\s+(?P<which>\S+)\s+(is|means)\s+(?P<what>.*)$',
self.handle_add_fact, -20
)
self.connection.reactor.add_global_regex_handler(
['pubmsg', 'privmsg'], r'^!f(acts)?\s+(?P<which>\S+)(\s+(?P<what>.*)$|$)',
self.handle_facts, -20
)
super(Facts, self).start()
@ -30,10 +34,10 @@ class Facts(Plugin):
def handle_facts(self, connection, event, match):
"""Respond to the facts command with desired fact."""
category = match.group(1)
category = match.group('which')
regex = None
if match.group(2) != '':
regex = match.group(3)
regex = match.group('what')
fact = Fact.objects.random_fact(category, regex)
if fact:
@ -47,15 +51,16 @@ class Facts(Plugin):
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 event.in_privmsg or event.addressed:
category_name = match.group('which')
fact_text = match.group('what')
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))
self.bot.reply(event, f"ok, I now know more about {category.name}")
return 'NO MORE'
plugin = Facts

Loading…
Cancel
Save