initial commit
This commit is contained in:
parent
edd1553b7e
commit
e99ac43c9b
|
|
@ -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)
|
||||
|
|
@ -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): ...
|
||||
|
|
@ -0,0 +1 @@
|
|||
discord.py
|
||||
Loading…
Reference in New Issue