2015-05-27 21:01:28 -05:00
|
|
|
|
"""Roll dice when asked, intended for RPGs."""
|
2017-03-02 09:40:23 -06:00
|
|
|
|
import logging
|
2015-05-27 21:01:28 -05:00
|
|
|
|
import random
|
2022-07-14 23:00:05 -05:00
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
from django.conf import settings
|
2015-05-27 21:01:28 -05:00
|
|
|
|
|
|
|
|
|
from irc.client import NickMask
|
|
|
|
|
|
2019-06-21 16:51:43 -05:00
|
|
|
|
from dice.roller import DiceRoller
|
2015-05-27 21:01:28 -05:00
|
|
|
|
from ircbot.lib import Plugin
|
|
|
|
|
|
2017-03-02 09:40:23 -06:00
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
2015-05-27 21:01:28 -05:00
|
|
|
|
class Dice(Plugin):
|
|
|
|
|
"""Roll simple or complex dice strings."""
|
|
|
|
|
|
2021-04-24 12:57:12 -05:00
|
|
|
|
def __init__(self, bot, connection, event):
|
2019-06-21 16:49:02 -05:00
|
|
|
|
"""Set up the plugin."""
|
2015-05-27 21:01:28 -05:00
|
|
|
|
self.roller = DiceRoller()
|
|
|
|
|
|
2021-04-24 12:57:12 -05:00
|
|
|
|
super(Dice, self).__init__(bot, connection, event)
|
|
|
|
|
|
2019-06-21 16:49:02 -05:00
|
|
|
|
def start(self):
|
|
|
|
|
"""Set up the handlers."""
|
2015-05-27 21:01:28 -05:00
|
|
|
|
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!roll\s+(.*)$',
|
|
|
|
|
self.handle_roll, -20)
|
2017-02-04 10:03:47 -06:00
|
|
|
|
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!random\s+(.*)$',
|
|
|
|
|
self.handle_random, -20)
|
2015-05-27 21:01:28 -05:00
|
|
|
|
|
|
|
|
|
super(Dice, self).start()
|
|
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
|
"""Tear down handlers."""
|
|
|
|
|
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_roll)
|
2017-02-04 10:03:47 -06:00
|
|
|
|
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_random)
|
2015-05-27 21:01:28 -05:00
|
|
|
|
|
|
|
|
|
super(Dice, self).stop()
|
|
|
|
|
|
2017-02-04 10:03:47 -06:00
|
|
|
|
def handle_random(self, connection, event, match):
|
|
|
|
|
"""Handle the !random command which picks an item from a list."""
|
|
|
|
|
nick = NickMask(event.source).nick
|
|
|
|
|
choices = match.group(1)
|
|
|
|
|
|
|
|
|
|
choices_list = choices.split(' ')
|
2022-07-14 23:00:05 -05:00
|
|
|
|
choice = random.SystemRandom().choice(choices_list)
|
2017-02-04 10:03:47 -06:00
|
|
|
|
|
2017-03-10 18:29:31 -06:00
|
|
|
|
logger.debug(event.recursing)
|
|
|
|
|
if event.recursing:
|
2017-03-02 09:40:23 -06:00
|
|
|
|
reply = "{0:s}".format(choice)
|
2022-07-14 23:00:05 -05:00
|
|
|
|
elif settings.DICE_PREFIX_ROLLER:
|
2017-03-02 09:40:23 -06:00
|
|
|
|
reply = "{0:s}: {1:s}".format(nick, choice)
|
2022-07-14 23:00:05 -05:00
|
|
|
|
else:
|
|
|
|
|
reply = "{0:s}".format(choice)
|
2017-02-04 10:03:47 -06:00
|
|
|
|
return self.bot.reply(event, reply)
|
|
|
|
|
|
2015-05-27 21:01:28 -05:00
|
|
|
|
def handle_roll(self, connection, event, match):
|
|
|
|
|
"""Handle the !roll command which covers most common dice stuff."""
|
|
|
|
|
nick = NickMask(event.source).nick
|
|
|
|
|
dicestr = match.group(1)
|
|
|
|
|
|
2017-03-10 18:29:31 -06:00
|
|
|
|
logger.debug(event.recursing)
|
2019-06-21 16:51:43 -05:00
|
|
|
|
try:
|
|
|
|
|
reply_str = self.roller.do_roll(dicestr)
|
|
|
|
|
except AssertionError as aex:
|
|
|
|
|
reply_str = f"Could not roll dice: {aex}"
|
|
|
|
|
except ValueError:
|
|
|
|
|
reply_str = "Unable to parse roll"
|
|
|
|
|
|
2017-03-10 18:29:31 -06:00
|
|
|
|
if event.recursing:
|
2019-06-21 16:51:43 -05:00
|
|
|
|
reply = "{0:s}".format(reply_str)
|
2022-07-14 23:00:05 -05:00
|
|
|
|
elif settings.DICE_PREFIX_ROLLER:
|
2019-06-21 16:51:43 -05:00
|
|
|
|
reply = "{0:s}: {1:s}".format(nick, reply_str)
|
2022-07-14 23:00:05 -05:00
|
|
|
|
else:
|
|
|
|
|
reply = "{0:s}".format(reply_str)
|
2015-05-27 21:01:28 -05:00
|
|
|
|
return self.bot.reply(event, re.sub(r'(\d+)(.*?\s+)(\(.*?\))', r'\1\214\3', reply))
|
|
|
|
|
|
2022-07-14 23:00:05 -05:00
|
|
|
|
|
2015-05-27 21:01:28 -05:00
|
|
|
|
plugin = Dice
|