From 1ee3f6dfa49a64e2ab13c839144f660b59795551 Mon Sep 17 00:00:00 2001 From: "Brian Donohoe (creftos)" Date: Sun, 28 Sep 2014 14:40:48 -0700 Subject: [PATCH 1/2] [dice] Add a reply if .dice/.roll is called without arguments. --- willie/modules/dice.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/willie/modules/dice.py b/willie/modules/dice.py index 4dd981f6d4..dbb5109268 100644 --- a/willie/modules/dice.py +++ b/willie/modules/dice.py @@ -171,6 +171,8 @@ def roll(bot, trigger): # Get a list of all dice expressions, evaluate them and then replace the # expressions in the original string with the results. Replacing is done # using string formatting, so %-characters must be escaped. + if not trigger.group(2): + return bot.reply("No dice to roll.") arg_str = trigger.group(2) dice_expressions = re.findall(dice_regexp, arg_str) arg_str = arg_str.replace("%", "%%") From d51db97ed12a6dee2f0bcc659ce9ead5bae3dafe Mon Sep 17 00:00:00 2001 From: "Brian Donohoe (creftos)" Date: Sun, 28 Sep 2014 21:24:29 -0700 Subject: [PATCH 2/2] [dice] Fix #626. Willie will now give you an appropriate message if your dice don't have any sides. --- willie/modules/dice.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/willie/modules/dice.py b/willie/modules/dice.py index dbb5109268..b55e062152 100644 --- a/willie/modules/dice.py +++ b/willie/modules/dice.py @@ -117,7 +117,7 @@ def get_number_of_faces(self): return len(self.dice) + len(self.dropped) -def _roll_dice(dice_expression): +def _roll_dice(bot, dice_expression): result = re.search( r""" (?P\d*) @@ -131,11 +131,17 @@ def _roll_dice(dice_expression): dice_num = int(result.group('dice_num') or 1) dice_type = int(result.group('dice_type')) + # Dice can't have zero or a negative number of sides. + if dice_type <= 0: + bot.reply("I don't have any dice with %d sides. =(" % dice_type) + return None # Signal there was a problem + # Upper limit for dice should be at most a million. Creating a dict with # more than a million elements already takes a noticeable amount of time # on a fast computer and ~55kB of memory. if dice_num > 1000: - return None + bot.reply('I only have 1000 dice. =(') + return None # Signal there was a problem dice = DicePouch(dice_num, dice_type, 0) @@ -177,9 +183,12 @@ def roll(bot, trigger): dice_expressions = re.findall(dice_regexp, arg_str) arg_str = arg_str.replace("%", "%%") arg_str = re.sub(dice_regexp, "%s", arg_str) - dice = list(map(_roll_dice, dice_expressions)) + + f = lambda dice_expr: _roll_dice (bot, dice_expr) + dice = list(map(f, dice_expressions)) + if None in dice: - bot.reply("I only have 1000 dice. =(") + # Stop computing roll if there was a problem rolling dice. return def _get_eval_str(dice):