roll: allow junk at end of line
This enables "fun" stuff like "!roll 20 bottles of milk"
This commit is contained in:
parent
76266d8d2f
commit
d275da418f
1 changed files with 23 additions and 19 deletions
|
|
@ -17,26 +17,26 @@ def init(bot):
|
||||||
|
|
||||||
|
|
||||||
async def handle(message: Message):
|
async def handle(message: Message):
|
||||||
if len(message.args) == 1 and message.args[0].isdecimal():
|
dice = []
|
||||||
dice = [(1, int(message.args[0]))]
|
num = None
|
||||||
else:
|
for consumed, arg in enumerate(message.args, start=1):
|
||||||
dice = []
|
if (match := parse_die(arg)) :
|
||||||
num = None
|
if num and match["num"]:
|
||||||
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:
|
|
||||||
return
|
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:
|
if not 0 < len(dice) < 20:
|
||||||
return
|
return
|
||||||
|
|
@ -56,4 +56,8 @@ async def handle(message: Message):
|
||||||
for sides, vs in sorted(rolls.items(), key=lambda i: i[0], reverse=1)
|
for sides, vs in sorted(rolls.items(), key=lambda i: i[0], reverse=1)
|
||||||
)
|
)
|
||||||
text = f"{total} = {dicestr}"
|
text = f"{total} = {dicestr}"
|
||||||
|
|
||||||
|
if len(message.args) > consumed:
|
||||||
|
text += " " + " ".join(message.args[consumed:])
|
||||||
|
|
||||||
await reply(message, plain=text, in_thread=True)
|
await reply(message, plain=text, in_thread=True)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue