add basic form of dice rolling via dr.botzo

not done yet, just getting the code out there for now
This commit is contained in:
Brian S. Stephan 2019-06-22 11:52:41 -05:00
parent d4ea843088
commit f048d7c193
3 changed files with 52 additions and 0 deletions

21
hitomi/backends.py Normal file
View File

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

View File

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

27
hitomi/dice.py Normal file
View File

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