rolling privately and command tree

This commit is contained in:
Andrei Stoica 2025-01-16 14:53:10 -05:00
parent c5d950ca07
commit 33c3b0cf67
1 changed files with 19 additions and 13 deletions

32
bot.py
View File

@ -1,41 +1,47 @@
import discord import discord
from discord.ext import commands from discord.app_commands import CommandTree
from discord.ext.commands.context import Context
from os import environ from os import environ
from dice import Dice, DiceTextError from dice import Dice, DiceTextError
prefix = "$"
intents = discord.Intents.default() intents = discord.Intents.default()
intents.message_content = True intents.message_content = True
bot = commands.Bot(command_prefix=prefix, intents=intents) bot = discord.Client(intents=intents)
tree = CommandTree(bot)
@bot.event @bot.event
async def on_ready(): async def on_ready():
print("Hello World!") await tree.sync() # Bad idea
@bot.command() @tree.command()
async def roll(ctx: Context, *args: str): @discord.app_commands.describe(
dice = [] dice="Dice to be rolled in format [number]d[sides]",
for arg in args: private="Roll privatly or publicly",
dice.append(Dice(arg)) )
async def roll(interaction: discord.Interaction, dice: str, private: bool):
dice_objs = [Dice(d) for d in dice.split(" ")]
try: try:
text = [] text = []
for die in dice: for die in dice_objs:
roll = die.roll() roll = die.roll()
text.append(f"{die.text} => {roll} = {sum(roll)}") text.append(f"{die.text} => {roll} = {sum(roll)}")
print(text) print(text)
except DiceTextError as e: except DiceTextError as e:
print(e) print(e)
await ctx.send(str(e)) await interaction.response.send_message(str(e))
return return
await ctx.send("\n".join(text)) 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)
token = environ.get("DISCORD_TOKEN") token = environ.get("DISCORD_TOKEN")