Compare commits

..

No commits in common. "061a15caa3828bff50484fc30c4338942ba9de62" and "f048d7c193c5cda42a9af43634d39bd822976518" have entirely different histories.

2 changed files with 7 additions and 39 deletions

View File

@ -9,36 +9,13 @@ import hitomi.config as config
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class DrBotzoError(requests.HTTPError):
"""Wrap HTTPError with the error detail from the dr.botzo API, if available."""
def __init__(self, *args, **kwargs):
"""Initialize DrBotzoError."""
detail = kwargs.pop('detail', None)
self.detail = detail
super(DrBotzoError, self).__init__(*args, **kwargs)
class DrBotzoBackend(object): class DrBotzoBackend(object):
"""Basic HTTP requests API, wrapped with some authentication and config stuff.""" """Basic HTTP requests API, wrapped with some authentication and config stuff."""
def post(self, url, **kwargs): def post(self, url, **kwargs):
"""Wrap requests.post with authentication and hostname settings.""" """Wrap requests.post with authentication and hostname settings."""
try: return requests.post(urljoin(config.DR_BOTZO_BACKEND_HOST, url),
response = requests.post(urljoin(config.DR_BOTZO_BACKEND_HOST, url), auth=(config.DR_BOTZO_BACKEND_USER, config.DR_BOTZO_BACKEND_PASS), **kwargs)
auth=(config.DR_BOTZO_BACKEND_USER, config.DR_BOTZO_BACKEND_PASS), **kwargs)
response.raise_for_status()
return response
except requests.ConnectionError as cex:
logger.exception("received a connection error during POST %s", url)
raise DrBotzoError(cex, detail="A connection error occurred.")
except requests.HTTPError as httpex:
logger.exception("received an HTTP error during POST %s", url)
try:
detail = httpex.response.json()['detail']
raise DrBotzoError(httpex, detail=detail)
except (ValueError, KeyError):
raise DrBotzoError(httpex, detail="An unexpected error occurred.")
dr_botzo = DrBotzoBackend() dr_botzo = DrBotzoBackend()

View File

@ -1,11 +1,10 @@
"""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, DrBotzoError from hitomi.backends import dr_botzo
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -14,23 +13,15 @@ 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)
try: result = dr_botzo.post('/dice/rpc/roll/', json={'dice': dice})
response = dr_botzo.post('/dice/rpc/roll/', json={'dice': dice}) logger.debug(result)
response.raise_for_status() await ctx.send(result.json())
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))