diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..979852b --- /dev/null +++ b/bot.py @@ -0,0 +1,38 @@ +import discord +from discord.ext import commands +from discord.ext.commands.context import Context + +from dice import Dice, DiceTextError + +prefix = "$" + +intents = discord.Intents.default() +intents.message_content = True + +bot = commands.Bot(command_prefix=prefix, intents=intents) + + +@bot.event +async def on_ready(): + print("Hello World!") + + +@bot.command() +async def roll(ctx: Context, *args: str): + dice = [] + for arg in args: + dice.append(Dice(arg)) + + try: + results = ["(" + ",".join(d.roll()) + "," for d in dice] + except DiceTextError as e: + print(e) + await ctx.send(str(e)) + return + + msg = ",".join(results) + print(msg) + await ctx.send(msg) + + +bot.add_command(roll) diff --git a/dice.py b/dice.py new file mode 100644 index 0000000..cd49f99 --- /dev/null +++ b/dice.py @@ -0,0 +1,36 @@ +import re +from typing import Iterable +from random import randint + + +class Dice: + text: str + count: int + sides: int + + def __init__(self, text: str): + self.text = text + parsed_text = self.parse_text(text) + if parsed_text is None: + raise DiceTextError(f"Could not parse {text}") + + (self.count, self.sides) = parsed_text + + def roll(self) -> Iterable[int]: + result = [randint(1, self.sides) for _ in range(self.count)] + print(f"rolling {self.text} => {result}") + return result + + @staticmethod + def parse_text(text: str) -> tuple[int, int] | None: + match = re.match(r"^(\d*)d(\d+)$", text) + if match is None: + return None + + count = match.group(1) + sides = match.group(2) + + return (int(count), int(sides)) + + +class DiceTextError(RuntimeError): ... diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..844f49a --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +discord.py diff --git a/todo b/todo new file mode 100644 index 0000000..bbd3bec --- /dev/null +++ b/todo @@ -0,0 +1,4 @@ +- [ ] rolling privately +- [ ] rolling toward GM +- [ ] logging and debuging +- [ ] session managment