From 7876fd57acc357a0899743594914ff2e7c26c232 Mon Sep 17 00:00:00 2001 From: Patrick Hurst Date: Mon, 16 Aug 2010 05:32:28 -0400 Subject: [PATCH] dicerolls now have descriptions --- plugins/dice.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/plugins/dice.py b/plugins/dice.py index 6005cf5..54445ab 100644 --- a/plugins/dice.py +++ b/plugins/dice.py @@ -9,7 +9,7 @@ from util import hook whitespace_re = re.compile(r'\s+') -valid_diceroll = r'^([+-]?(\d+|\d*d(\d+|F))([+-](\d+|\d*d(\d+|F)))*)$' +valid_diceroll = r'^([+-]?(?:\d+|\d*d(?:\d+|F))(?:[+-](?:\d+|\d*d(?:\d+|F)))*)( .+)?$' valid_diceroll_re = re.compile(valid_diceroll, re.I) sign_re = re.compile(r'[+-]?(?:\d*d)?(?:\d+|F)', re.I) split_re = re.compile(r'([\d+-]*)d?(F|\d*)', re.I) @@ -36,10 +36,13 @@ def nrolls(count, n): def dice(inp): ".dice -- simulates dicerolls, e.g. .dice 2d20-d5+4 roll 2 " \ "D20s, subtract 1D5, add 4" - try: - inp = inp.groups()[0] # try to grab the roll + desc = None + try: # if inp is a re.match object... + (inp, desc) = inp.groups() except AttributeError: pass # we got called via hook.command, inp is already the roll + if desc == None: (inp, desc) = valid_diceroll_re.match(inp).groups() + desc = desc.strip() spec = whitespace_re.sub('', inp) if not valid_diceroll_re.match(spec): return "Invalid diceroll" @@ -65,4 +68,4 @@ def dice(inp): except OverflowError: return "Thanks for overflowing a float, jerk >:[" - return str(sum) + return "%s: %d" % (desc, sum)