Use module structure

This commit is contained in:
Joscha 2018-08-08 14:44:22 +00:00
parent 85f2839b9e
commit 50e1acc8fc

View file

@ -8,51 +8,36 @@ import yaboli
from yaboli.utils import * from yaboli.utils import *
# Turn all debugging on class Roller:
asyncio.get_event_loop().set_debug(True) SHORT_DESCRIPTION = "roll dice for dnd"
#logging.getLogger("asyncio").setLevel(logging.INFO) DESCRIPTION = (
#logging.getLogger("yaboli").setLevel(logging.DEBUG) "'roller' is based on PouncySilverkitten's @Roller bot, but has been rewritten for yaboli"
logging.basicConfig(level=logging.DEBUG) " and now supports a more complicated syntax for dice rolls.\n"
)
COMMANDS = (
ROLL = r"[!/]r(oll)?\s+(.*)"
THROW = r"\s*([+-])?\s*(\d+)?d(\d+)" # 1: sign, 2: amount (default 1), 3: sides
ADVANTAGE = r"\s*([+-])?\s*(\d+)?([ad])d(\d+)" # 1: sign, 2: amount (default 2), 3: a/d, 4: sides
NUMBER = r"\s*([+-])?\s*(\d+)" # 1: sign, 2: number
class Roller(yaboli.Bot):
LONG_HELP = (
"!roll <dice> <description> - roll dice\n" "!roll <dice> <description> - roll dice\n"
"/roll can be used instead of !roll. !r and /r also work.\n" "/roll can be used instead of !roll. !r and /r also work.\n"
"\n" )
SYNTAX = (
"Dice throws, can be added/subtracted to each other:\n" "Dice throws, can be added/subtracted to each other:\n"
"XdY - Throw X Y-sided dice. X defaults to 1.\n" "XdY - Throw X Y-sided dice. X defaults to 1.\n"
"XadY - Throw X Y-sided dice with advantage. X defaults to 2.\n" "XadY - Throw X Y-sided dice with advantage. X defaults to 2.\n"
"XddY - Throw X Y-sided dice with disadvantage. X defaults to 2.\n" "XddY - Throw X Y-sided dice with disadvantage. X defaults to 2.\n"
"X - Constant number\n" "X - Constant number\n"
"\n" )
EXAMPLES = (
"Example throws:\n" "Example throws:\n"
"!roll d20\n" "!roll d20\n"
"/roll ad20 + 5 damage\n" "/roll ad20 + 5 damage\n"
"!r 2d20-d10+10\n" "!r 2d20-d10+10\n"
) )
AUTHOR = "Created by @Garmy using github.com/Garmelon/yaboli\n"
CREDITS = "Inspired by and based on PouncySilverkitten's @Roller bot.\n"
async def on_command_specific(self, room, message, command, nick, argstr): ROLL = r"[!/]r(oll)?\s+(.*)"
if similar(nick, room.session.nick) and not argstr: THROW = r"\s*([+-])?\s*(\d+)?d(\d+)" # 1: sign, 2: amount (default 1), 3: sides
await self.botrulez_ping(room, message, command) ADVANTAGE = r"\s*([+-])?\s*(\d+)?([ad])d(\d+)" # 1: sign, 2: amount (default 2), 3: a/d, 4: sides
await self.botrulez_help(room, message, command, text=self.LONG_HELP) NUMBER = r"\s*([+-])?\s*(\d+)" # 1: sign, 2: number
await self.botrulez_uptime(room, message, command)
await self.botrulez_kill(room, message, command)
await self.botrulez_restart(room, message, command)
async def on_command_general(self, room, message, command, argstr):
if not argstr:
await self.botrulez_ping(room, message, command)
await self.botrulez_help(room, message, command, text="I roll dice for dnd")
async def on_send(self, room, message):
await super().on_send(room, message)
await self.trigger_roll(room, message)
@yaboli.trigger(ROLL) @yaboli.trigger(ROLL)
async def trigger_roll(self, room, message, match): async def trigger_roll(self, room, message, match):
@ -62,9 +47,9 @@ class Roller(yaboli.Bot):
rest = match.group(2) rest = match.group(2)
while True: while True:
mthrow, mthrowrest = self.match_and_split(THROW, rest) mthrow, mthrowrest = self.match_and_split(self.THROW, rest)
madvantage, madvantagerest = self.match_and_split(ADVANTAGE, rest) madvantage, madvantagerest = self.match_and_split(self.ADVANTAGE, rest)
mnumber, mnumberrest = self.match_and_split(NUMBER, rest) mnumber, mnumberrest = self.match_and_split(self.NUMBER, rest)
if mthrow: if mthrow:
sign = self.to_sign(mthrow.group(1)) sign = self.to_sign(mthrow.group(1))
amount = self.to_amount(mthrow.group(2), default=1) amount = self.to_amount(mthrow.group(2), default=1)
@ -146,13 +131,43 @@ class Roller(yaboli.Bot):
def number(number): def number(number):
return number, str(number) return number, str(number)
class RollerBot(yaboli.Bot):
SHORT_HELP = Roller.SHORT_DESCRIPTION
LONG_HELP = Roller.DESCRIPTION + Roller.COMMANDS + "\n" + Roller.SYNTAX + "\n" + Roller.EXAMPLES + "\n" + Roller.AUTHOR + Roller.CREDITS
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.roller = Roller()
async def on_command_specific(self, room, message, command, nick, argstr):
if similar(nick, room.session.nick) and not argstr:
await self.botrulez_ping(room, message, command)
await self.botrulez_help(room, message, command, text=self.LONG_HELP)
await self.botrulez_uptime(room, message, command)
await self.botrulez_kill(room, message, command)
await self.botrulez_restart(room, message, command)
async def on_command_general(self, room, message, command, argstr):
if not argstr:
await self.botrulez_ping(room, message, command)
await self.botrulez_help(room, message, command, text=self.SHORT_HELP)
async def on_send(self, room, message):
await super().on_send(room, message)
await self.roller.trigger_roll(room, message)
def main(configfile): def main(configfile):
logging.basicConfig(level=logging.INFO)
config = configparser.ConfigParser(allow_no_value=True) config = configparser.ConfigParser(allow_no_value=True)
config.read(configfile) config.read(configfile)
nick = config.get("general", "nick") nick = config.get("general", "nick")
cookiefile = config.get("general", "cookiefile", fallback=None) cookiefile = config.get("general", "cookiefile", fallback=None)
bot = Roller(nick, cookiefile=cookiefile) bot = RollerBot(nick, cookiefile=cookiefile)
for room, password in config.items("rooms"): for room, password in config.items("rooms"):
if not password: if not password: