From d275da418f1e661db831e1e0a679b3c3c6d08f74 Mon Sep 17 00:00:00 2001 From: ducklet Date: Tue, 10 Nov 2020 21:08:56 +0100 Subject: [PATCH] roll: allow junk at end of line This enables "fun" stuff like "!roll 20 bottles of milk" --- hotdog/command/roll.py | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/hotdog/command/roll.py b/hotdog/command/roll.py index 25a3497..37f708b 100644 --- a/hotdog/command/roll.py +++ b/hotdog/command/roll.py @@ -17,26 +17,26 @@ def init(bot): async def handle(message: Message): - if len(message.args) == 1 and message.args[0].isdecimal(): - dice = [(1, int(message.args[0]))] - else: - dice = [] - num = None - for arg in message.args: - if (match := parse_die(arg)) : - if num and match["num"]: - return - num, sides = int(match["num"] or num or 1), int(match["sides"]) - if not 1 <= num < 1000 or not 2 <= sides <= 100: - return - dice.append((num, sides)) - num = None - elif arg.isdecimal(): - if num is not None: - return - num = int(arg) - else: + dice = [] + num = None + for consumed, arg in enumerate(message.args, start=1): + if (match := parse_die(arg)) : + if num and match["num"]: return + num, sides = int(match["num"] or num or 1), int(match["sides"]) + if not 1 <= num < 1000 or not 2 <= sides <= 100: + return + dice.append((num, sides)) + num = None + elif arg.isdecimal(): + if num is not None: + return + num = int(arg) + else: + consumed -= 1 + break + if not dice and num: + dice.append((1, num)) if not 0 < len(dice) < 20: return @@ -56,4 +56,8 @@ async def handle(message: Message): for sides, vs in sorted(rolls.items(), key=lambda i: i[0], reverse=1) ) text = f"{total} = {dicestr}" + + if len(message.args) > consumed: + text += " " + " ".join(message.args[consumed:]) + await reply(message, plain=text, in_thread=True)