Compare commits

..

2 Commits

Author SHA1 Message Date
Andrei Stoica 3fe374337b updated todo 2025-01-21 10:18:39 -05:00
Andrei Stoica 43d9b17f5f refactoring 2025-01-21 10:16:03 -05:00
3 changed files with 17 additions and 9 deletions

14
bot.py
View File

@ -15,33 +15,31 @@ tree = CommandTree(bot)
@bot.event @bot.event
async def on_ready(): async def on_ready():
await tree.sync() # Bad idea await tree.sync() # Bad idea
print("Bot Ready")
@tree.command() @tree.command()
@discord.app_commands.describe( @discord.app_commands.describe(
dice="Dice to be rolled in format [number]d[sides]", dice="Dice to be rolled (format: [number]d[sides])",
private="Roll privatly or publicly", private="Roll privatly or publicly",
) )
async def roll(interaction: discord.Interaction, dice: str, private: bool): async def roll(interaction: discord.Interaction, dice: str, private: bool):
dice_objs = [Dice(d) for d in dice.split(" ")] dice_objs = [Dice(d) for d in dice.split(" ")]
try: try:
text = [] result_text = [die.roll_as_text() for die in dice_objs]
for die in dice_objs:
roll = die.roll()
text.append(f"{die.text} => {roll} = {sum(roll)}")
print(text)
except DiceTextError as e: except DiceTextError as e:
print(e) print(e)
await interaction.response.send_message(str(e)) await interaction.response.send_message(str(e))
return return
if not private: if not private:
intro = interaction.user.display_name + " rolled:\n" intro = f"@{interaction.user.display_name} rolled:\n"
else: else:
intro = "You rolled:\n" intro = "You rolled:\n"
msg = intro + "/n".join(result_text)
await interaction.response.send_message(intro + "\n".join(text), ephemeral=private) await interaction.response.send_message(msg, ephemeral=private)
token = environ.get("DISCORD_TOKEN") token = environ.get("DISCORD_TOKEN")

10
dice.py
View File

@ -17,10 +17,20 @@ class Dice:
(self.count, self.sides) = parsed_text (self.count, self.sides) = parsed_text
def roll(self) -> Iterable[int]: def roll(self) -> Iterable[int]:
"""Randomly generates `self.count` integers in range [1,`self.sides`]"""
result = [randint(1, self.sides) for _ in range(self.count)] result = [randint(1, self.sides) for _ in range(self.count)]
print(f"rolling {self.text} => {result}") print(f"rolling {self.text} => {result}")
return result return result
def roll_as_text(self) -> str:
"""
Randomly generates `self.count` integers in range [1,`self.sides`]
then returns a formated string.
"""
roll = self.roll()
result = f"{self.text} => {roll} = {sum(roll)}"
return result
@staticmethod @staticmethod
def parse_text(text: str) -> tuple[int, int] | None: def parse_text(text: str) -> tuple[int, int] | None:
match = re.match(r"^(\d*)d(\d+)$", text) match = re.match(r"^(\d*)d(\d+)$", text)

2
todo
View File

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