Search code examples
pythondiscorddiscord.py

How to show items when the command is typed (OnKeyUp) in python?


I have a list of shop items and i want those items to show when the command is typed.

    @app_commands.command(name="delete", description="delete an item from shop")
    async def delete(self, interaction: discord.Interaction, name: str):

Within the name part i want the items to show like the example below

enter image description here

so, is there any specific way i have to/can do this?


Solution

  • You can modify this code to show what you want:

    from discord import app_commands
    
    choosable = ["a", "b", "c", "d", "e", "f"] #a list of the items (develop your function to get what you would like to get back as an option)
    choices = [app_commands.Choice(name=choice, value=choice) for choice in choosable] # don't have to touch this unless you want to change varibale names
    
    @bot.tree.command(name="choose")
    @app_commands.choices(option=choices)
    async def choose(interaction:discord.Interaction,option:app_commands.Choice[str]): #showing the options 
        await interaction.response.send_message(option.value,ephemeral=True) # send back the chosen value.