From ab02c3e59a136b358ef73b2ec39bc7568f3cfa77 Mon Sep 17 00:00:00 2001 From: urlbot Date: Sat, 20 Dec 2014 17:02:26 +0100 Subject: [PATCH] patch command_dice to roll N dices --- plugins.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/plugins.py b/plugins.py index 862c8a3..1458ff4 100644 --- a/plugins.py +++ b/plugins.py @@ -267,8 +267,8 @@ def command_dice(args): if 'register' == args: return { 'name': 'dice', - 'desc': 'rolls a dice', - 'args': ('argv0', 'reply_user'), + 'desc': 'rolls a dice, optional N times', + 'args': ('argv0', 'argv1', 'reply_user'), 'ratelimit_class': RATE_INTERACTIVE } @@ -283,8 +283,30 @@ def command_dice(args): logger('plugin', 'sent random') dice_char = ['◇', '⚀', '⚁', '⚂', '⚃', '⚄', '⚅'] + count = 0 + msg = None + + try: + count = 1 if None is args['argv1'] else int(args['argv1']) + except ValueError as e: + return { + 'msg': '%s: dice: error when parsing int(%s): %s' % ( + args['reply_user'], args['argv1'], str(e) + ) + } + + if 0 >= count or 5 <= count: + return { + 'msg': '%s: dice: invalid arguments (0 < N < 5)' % args['reply_user'] + } + + msg = [ + 'rolling a dice for %s: %s (%d)' % (args['reply_user'], dice_char[rnd], rnd) + for i in range(count) + ] + return { - 'msg': 'rolling a dice for %s: %s (%d)' % (args['reply_user'], dice_char[rnd], rnd) + 'msg': msg[0] if 1 == count else msg } def command_uptime(args):