give better output to discord of dice rolls, handle errors

This commit is contained in:
Brian S. Stephan 2019-06-29 10:55:26 -05:00
parent 5e4bcf6b1c
commit 061a15caa3

View File

@ -1,10 +1,11 @@
"""Commands for dice rolling type behavior.""" """Commands for dice rolling type behavior."""
import logging import logging
import re
from discord.ext import commands from discord.ext import commands
from hitomi import bot from hitomi import bot
from hitomi.backends import dr_botzo from hitomi.backends import dr_botzo, DrBotzoError
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -13,15 +14,23 @@ class Dice(commands.Cog):
"""Commands for rolling dice and whatnot.""" """Commands for rolling dice and whatnot."""
@commands.command() @commands.command()
async def roll(self, ctx, dice): async def roll(self, ctx, *, dice):
"""Roll a provided dice string. """Roll a provided dice string.
Format: T#K/NdS+M; T = times, K = keep, N = number, S = sides, M = modifier Format: T#K/NdS+M; T = times, K = keep, N = number, S = sides, M = modifier
""" """
await ctx.trigger_typing()
logger.info("rolling dice: %s", dice) logger.info("rolling dice: %s", dice)
result = dr_botzo.post('/dice/rpc/roll/', json={'dice': dice}) try:
logger.debug(result) response = dr_botzo.post('/dice/rpc/roll/', json={'dice': dice})
await ctx.send(result.json()) response.raise_for_status()
logger.debug("result of rolling dice: HTTP %s, %s", response.status_code, response.text)
output = re.sub(r'(\d+)(.*?\s+)(\(.*?\))', r'**\1**\2\3', response.json()['result'])
output = '\n'.join(output.split('; '))
await ctx.send(output)
except DrBotzoError as drex:
logger.exception("received an error from dr.botzo attempting to roll %s: %s", dice, drex)
await ctx.send(f"*{drex.detail}*")
bot.add_cog(Dice(bot)) bot.add_cog(Dice(bot))