51 lines
1.1 KiB
Python
51 lines
1.1 KiB
Python
import discord
|
|
from discord.app_commands import CommandTree
|
|
from os import environ
|
|
|
|
from dice import Dice, DiceTextError
|
|
|
|
|
|
intents = discord.Intents.default()
|
|
intents.message_content = True
|
|
|
|
bot = discord.Client(intents=intents)
|
|
tree = CommandTree(bot)
|
|
|
|
|
|
@bot.event
|
|
async def on_ready():
|
|
await tree.sync() # Bad idea
|
|
print("Bot Ready")
|
|
|
|
|
|
@tree.command()
|
|
@discord.app_commands.describe(
|
|
dice="Dice to be rolled (format: [number]d[sides])",
|
|
private="Roll privatly or publicly",
|
|
)
|
|
async def roll(interaction: discord.Interaction, dice: str, private: bool):
|
|
dice_objs = [Dice(d) for d in dice.split(" ")]
|
|
|
|
try:
|
|
result_text = [die.roll_as_text() for die in dice_objs]
|
|
except DiceTextError as e:
|
|
print(e)
|
|
await interaction.response.send_message(str(e))
|
|
return
|
|
|
|
if not private:
|
|
intro = f"@{interaction.user.display_name} rolled:\n"
|
|
else:
|
|
intro = "You rolled:\n"
|
|
msg = intro + "/n".join(result_text)
|
|
|
|
await interaction.response.send_message(msg, ephemeral=private)
|
|
|
|
|
|
token = environ.get("DISCORD_TOKEN")
|
|
if token is None:
|
|
print("Could not load token")
|
|
quit()
|
|
|
|
bot.run(token)
|