Search code examples
pythondiscorddiscord.pypycord

Is it possible to dynamically enable/disable arguments in a Python Discord slash command using Pycord and Discord.py based on previous user choice?


I am trying to make an command that registers a channel into the database with a specific function, but iu wish for some arguments to only be enabeled when a specific choice was made previously.

@bot.slash_command(name="my command")
async def command(ctx, 
    action: Option(name="action", choices=["add", "remove", "info"]),
    function:Option(name="function"):
    if action == "add":
        ...
    elif "remove":
        ...
    elif "info"
        ...

So what i want is that when "add" or "remove" are chosen for action, it disables the "function" argument or makes it non-required. I could just use the required=False flag, but i hoped that if the add/remove action is chose, the function will get the required=True flag.

I have tried to play around with discord.AutocompleteContext, but this has not yet shown a way to disable or enable arguments, only to change the choices.


Solution

  • To achieve the desired functionality of disabling or making the "function" argument non-required based on the selected action, you can make use of the required parameter of the Option class. By default, all options are required, but you can set required=False for the "function" option when "add" or "remove" actions are chosen.

    Here's an updated version of your code that incorporates this approach:

    @bot.slash_command(name="my_command")
    async def command(ctx, 
        action: Option(str, name="action", choices=["add", "remove", "info"]),
        function: Option(str, name="function", required=False)
    ):
        if action == "add":
            ...
        elif action == "remove":
            ...
        elif action == "info":
            ...