roll: allow junk at end of line

This enables "fun" stuff like "!roll 20 bottles of milk"
This commit is contained in:
ducklet 2020-11-10 21:08:56 +01:00
parent 76266d8d2f
commit d275da418f

View file

@ -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)