rolling privately and command tree
This commit is contained in:
parent
c5d950ca07
commit
33c3b0cf67
32
bot.py
32
bot.py
|
|
@ -1,41 +1,47 @@
|
|||
import discord
|
||||
from discord.ext import commands
|
||||
from discord.ext.commands.context import Context
|
||||
from discord.app_commands import CommandTree
|
||||
from os import environ
|
||||
|
||||
from dice import Dice, DiceTextError
|
||||
|
||||
prefix = "$"
|
||||
|
||||
intents = discord.Intents.default()
|
||||
intents.message_content = True
|
||||
|
||||
bot = commands.Bot(command_prefix=prefix, intents=intents)
|
||||
bot = discord.Client(intents=intents)
|
||||
tree = CommandTree(bot)
|
||||
|
||||
|
||||
@bot.event
|
||||
async def on_ready():
|
||||
print("Hello World!")
|
||||
await tree.sync() # Bad idea
|
||||
|
||||
|
||||
@bot.command()
|
||||
async def roll(ctx: Context, *args: str):
|
||||
dice = []
|
||||
for arg in args:
|
||||
dice.append(Dice(arg))
|
||||
@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(" ")]
|
||||
|
||||
try:
|
||||
text = []
|
||||
for die in dice:
|
||||
for die in dice_objs:
|
||||
roll = die.roll()
|
||||
text.append(f"{die.text} => {roll} = {sum(roll)}")
|
||||
print(text)
|
||||
except DiceTextError as e:
|
||||
print(e)
|
||||
await ctx.send(str(e))
|
||||
await interaction.response.send_message(str(e))
|
||||
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")
|
||||
|
|
|
|||
Loading…
Reference in New Issue