initial commit

This commit is contained in:
Andrei Stoica 2025-01-15 08:54:17 -05:00
parent edd1553b7e
commit e99ac43c9b
4 changed files with 79 additions and 0 deletions

38
bot.py Normal file
View File

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

36
dice.py Normal file
View File

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

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
discord.py

4
todo Normal file
View File

@ -0,0 +1,4 @@
- [ ] rolling privately
- [ ] rolling toward GM
- [ ] logging and debuging
- [ ] session managment