Compare commits
2 Commits
33c3b0cf67
...
3fe374337b
| Author | SHA1 | Date |
|---|---|---|
|
|
3fe374337b | |
|
|
43d9b17f5f |
14
bot.py
14
bot.py
|
|
@ -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
10
dice.py
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue