sir-rollington/bot.py

53 lines
1.2 KiB
Python
Raw Normal View History

2025-01-15 08:54:17 -05:00
import discord
2025-01-16 14:53:10 -05:00
from discord.app_commands import CommandTree
2025-01-15 22:00:37 -05:00
from os import environ
2025-01-15 08:54:17 -05:00
from dice import Dice, DiceTextError
intents = discord.Intents.default()
intents.message_content = True
2025-01-16 14:53:10 -05:00
bot = discord.Client(intents=intents)
tree = CommandTree(bot)
2025-01-15 08:54:17 -05:00
@bot.event
async def on_ready():
2025-01-16 14:53:10 -05:00
await tree.sync() # Bad idea
2025-01-15 08:54:17 -05:00
2025-01-16 14:53:10 -05:00
@tree.command()
@discord.app_commands.describe(
dice="Dice to be rolled in 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(" ")]
2025-01-15 08:54:17 -05:00
try:
2025-01-16 13:19:52 -05:00
text = []
2025-01-16 14:53:10 -05:00
for die in dice_objs:
2025-01-16 13:19:52 -05:00
roll = die.roll()
text.append(f"{die.text} => {roll} = {sum(roll)}")
print(text)
2025-01-15 08:54:17 -05:00
except DiceTextError as e:
print(e)
2025-01-16 14:53:10 -05:00
await interaction.response.send_message(str(e))
2025-01-15 08:54:17 -05:00
return
2025-01-16 14:53:10 -05:00
if not private:
intro = interaction.user.display_name + " rolled:\n"
else:
intro = "You rolled:\n"
await interaction.response.send_message(intro + "\n".join(text), ephemeral=private)
2025-01-15 08:54:17 -05:00
2025-01-15 22:00:37 -05:00
token = environ.get("DISCORD_TOKEN")
if token is None:
print("Could not load token")
quit()
bot.run(token)