diff --git a/roller.py b/roller.py index 7d809a9..9f8a7bd 100644 --- a/roller.py +++ b/roller.py @@ -8,51 +8,36 @@ import yaboli from yaboli.utils import * -# Turn all debugging on -asyncio.get_event_loop().set_debug(True) -#logging.getLogger("asyncio").setLevel(logging.INFO) -#logging.getLogger("yaboli").setLevel(logging.DEBUG) -logging.basicConfig(level=logging.DEBUG) - - -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 = ( +class Roller: + SHORT_DESCRIPTION = "roll dice for dnd" + DESCRIPTION = ( + "'roller' is based on PouncySilverkitten's @Roller bot, but has been rewritten for yaboli" + " and now supports a more complicated syntax for dice rolls.\n" + ) + COMMANDS = ( "!roll - roll dice\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" "XdY - Throw X Y-sided dice. X defaults to 1.\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" "X - Constant number\n" - "\n" + ) + EXAMPLES = ( "Example throws:\n" "!roll d20\n" "/roll ad20 + 5 damage\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): - 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="I roll dice for dnd") - - async def on_send(self, room, message): - await super().on_send(room, message) - await self.trigger_roll(room, message) + 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 @yaboli.trigger(ROLL) async def trigger_roll(self, room, message, match): @@ -62,9 +47,9 @@ class Roller(yaboli.Bot): rest = match.group(2) while True: - mthrow, mthrowrest = self.match_and_split(THROW, rest) - madvantage, madvantagerest = self.match_and_split(ADVANTAGE, rest) - mnumber, mnumberrest = self.match_and_split(NUMBER, rest) + mthrow, mthrowrest = self.match_and_split(self.THROW, rest) + madvantage, madvantagerest = self.match_and_split(self.ADVANTAGE, rest) + mnumber, mnumberrest = self.match_and_split(self.NUMBER, rest) if mthrow: sign = self.to_sign(mthrow.group(1)) amount = self.to_amount(mthrow.group(2), default=1) @@ -146,13 +131,43 @@ class Roller(yaboli.Bot): def number(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): + logging.basicConfig(level=logging.INFO) + config = configparser.ConfigParser(allow_no_value=True) config.read(configfile) nick = config.get("general", "nick") cookiefile = config.get("general", "cookiefile", fallback=None) - bot = Roller(nick, cookiefile=cookiefile) + bot = RollerBot(nick, cookiefile=cookiefile) for room, password in config.items("rooms"): if not password: