#Dynamic Choices in Slash Command

1 messages · Page 1 of 1 (latest)

dreamy bough
#

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)
mossy coveBOT
#

Hey! Once your issue is solved, press the button below to close this thread!

mossy coveBOT
#

We've detected that you're mixing discord.py and interactions.py code together - do not do this, and only use one for your bots.

Why?

  • Both interactions.py and discord.py are full Discord Python libraries that cover the entirety of the Discord API. It's redundant to use both, as they can do the same things.
  • While the two can do the same things, how they do is drastically different from each other. For example, despite being named the same, interactions.Client is very different from discord.Client, not just in implementation but in their core design. There's no way to make the two work together because of this because of this - code from one won't be understood by the other.
  • Relating to the above two points, using both at the same time will lead to errors.

While the decision of which library is up to you, it's important to only use one. As for us, as of our latest versions, we cover everything discord.py offers while boasting a better, simplier, more developer friendly experience when making bots. Take a look at the documentation and the discord.py migration guide if you're interested!

heady mural
hasty dust