From f048d7c193c5cda42a9af43634d39bd822976518 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Sat, 22 Jun 2019 11:52:41 -0500 Subject: [PATCH] add basic form of dice rolling via dr.botzo not done yet, just getting the code out there for now --- hitomi/backends.py | 21 +++++++++++++++++++++ hitomi/config.py | 4 ++++ hitomi/dice.py | 27 +++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 hitomi/backends.py create mode 100644 hitomi/dice.py diff --git a/hitomi/backends.py b/hitomi/backends.py new file mode 100644 index 0000000..35ed48d --- /dev/null +++ b/hitomi/backends.py @@ -0,0 +1,21 @@ +"""Wrap backend requests for usage in commands.""" +import logging +from urllib.parse import urljoin + +import requests + +import hitomi.config as config + +logger = logging.getLogger(__name__) + + +class DrBotzoBackend(object): + """Basic HTTP requests API, wrapped with some authentication and config stuff.""" + + def post(self, url, **kwargs): + """Wrap requests.post with authentication and hostname settings.""" + return requests.post(urljoin(config.DR_BOTZO_BACKEND_HOST, url), + auth=(config.DR_BOTZO_BACKEND_USER, config.DR_BOTZO_BACKEND_PASS), **kwargs) + + +dr_botzo = DrBotzoBackend() diff --git a/hitomi/config.py b/hitomi/config.py index 9b829dc..dcda3ad 100644 --- a/hitomi/config.py +++ b/hitomi/config.py @@ -10,6 +10,10 @@ BOT_MAX_MESSAGES = int(os.environ.get('HITOMI_BOT_MAX_MESSAGES', '5000')) BOT_PLUGIN_STR = os.environ.get('HITOMI_BOT_PLUGINS', '') BOT_PLUGINS = BOT_PLUGIN_STR.split(',') if BOT_PLUGIN_STR != '' else [] +DR_BOTZO_BACKEND_HOST = os.environ.get('HITOMI_DR_BOTZO_BACKEND_HOST', 'http://localhost:8000/') +DR_BOTZO_BACKEND_USER = os.environ.get('HITOMI_DR_BOTZO_BACKEND_USER', '') +DR_BOTZO_BACKEND_PASS = os.environ.get('HITOMI_DR_BOTZO_BACKEND_PASS', '') + logging.config.dictConfig({ 'version': 1, 'disable_existing_loggers': False, diff --git a/hitomi/dice.py b/hitomi/dice.py new file mode 100644 index 0000000..ec4cff6 --- /dev/null +++ b/hitomi/dice.py @@ -0,0 +1,27 @@ +"""Commands for dice rolling type behavior.""" +import logging + +from discord.ext import commands + +from hitomi import bot +from hitomi.backends import dr_botzo + +logger = logging.getLogger(__name__) + + +class Dice(commands.Cog): + """Commands for rolling dice and whatnot.""" + + @commands.command() + async def roll(self, ctx, dice): + """Roll a provided dice string. + + Format: T#K/NdS+M; T = times, K = keep, N = number, S = sides, M = modifier + """ + logger.info("rolling dice: %s", dice) + result = dr_botzo.post('/dice/rpc/roll/', json={'dice': dice}) + logger.debug(result) + await ctx.send(result.json()) + + +bot.add_cog(Dice(bot))