sir-rollington/bot.py

51 lines
1.1 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-21 10:06:04 -05:00
print("Bot Ready")
2025-01-15 08:54:17 -05:00
2025-01-16 14:53:10 -05:00
@tree.command()
@discord.app_commands.describe(
2025-01-21 10:06:04 -05:00
dice="Dice to be rolled (format: [number]d[sides])",
2025-01-16 14:53:10 -05:00
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-21 10:06:04 -05:00
result_text = [die.roll_as_text() for die in dice_objs]
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:
2025-01-21 10:06:04 -05:00
intro = f"@{interaction.user.display_name} rolled:\n"
2025-01-16 14:53:10 -05:00
else:
intro = "You rolled:\n"
2025-01-21 10:06:04 -05:00
msg = intro + "/n".join(result_text)
2025-01-16 14:53:10 -05:00
2025-01-21 10:06:04 -05:00
await interaction.response.send_message(msg, 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)