Hi everyone, I am trying to figure out how I can create dynamic choices in my slash command.
This is the code I have so far, it works to populate the choices based on the content of the .JSON file, but the issue is that I need to update the enum choices on a regular interval (10 seconds) incase of changes to the .JSON file!
I've been trying to resolve this for 2 days and its taken that long to get to this point 😄 The code below does not use the interactions library yet but im hopeful it would be used to resolve my issue! Any help would be greatly appreciated!
import discord
from discord import app_commands
from discord.ext import commands
import colorama
from utils.log import *
import enum
import json
# Load tokens from JSON file
try:
with open('tokens.json', 'r') as file:
tokens = json.load(file)
except FileNotFoundError:
print("Error: tokens.json file not found.")
tokens = []
except json.JSONDecodeError:
print("Error: tokens.json file is not valid JSON.")
tokens = []
# Ensure there are tokens loaded
if not tokens:
print("Error: No tokens found in tokens.json.")
exit()
# Print the tokens loaded from the JSON file for debugging
print("Tokens loaded from JSON file:", tokens)
# Create a dictionary to map tokens to their corresponding values
token_values = {token: idx + 1 for idx, token in enumerate(tokens)}
# Define Fruits enum based on the token_values dictionary
Fruits = enum.Enum("Fruits", token_values)
# Initialize bot with command prefix
bot = commands.Bot(
command_prefix='!',
intents=discord.Intents.all()
)
# Event: Bot is ready
@bot.event
async def on_ready():
try:
synced = await bot.tree.sync()
log(colorama.Fore.MAGENTA + f"Synced {len(synced)} commands")
log(colorama.Fore.GREEN + f'Bot is ready as {bot.user.name} ({bot.user.id})')
except Exception as e:
log(colorama.Fore.RED + f'Error: {e}')
@bot.tree.command(name="fruits")
@app_commands.describe(fruits='fruits to choose from')
async def fruit(interaction: discord.Interaction, fruits: Fruits):
await interaction.response.send_message(f'Your favourite fruit is {fruits}.')
if __name__ == "__main__":
bot.run(token)