Adding conditional fields to Slash command

Viewed 52

how can I add some fields that will show if the user chooses an option from discord.Option() list? I mean if user chooses 'Option a1' he/she will see field 'a1' and if he chooses 'Option b2' he/she will see field 'a2'. this is my code for option list:

@warn_command_group.command(name='remove',description='حذف اخطار')
    async def remove_warn(self, ctx:discord.ApplicationContext,
                          mode:discord.Option(str,'حالت حذف',choices=[
                              'حذف کل اخطار های سرور', # delete all warns
                              'حذف کل اخطار های یک ممبر', # delete all warns from a member
                              'حذف یک اخظار خاص(با استفاده از آیدی اون اخطار)' # delete a specific warn (with that warn's id)
                          ])
                          ):

I mean if they choose the first option, they will not see any forced field if they choose the second option, they will see a field named "member" with the type of discord.Member and if they choose the third option, they'll see a field named "id" with the type of int

1 Answers

First of all, you should not indent the function after the decorator. Then, To create choices for your mode parameter, you can use the arg choices as you did, which is a list.

In this list, there should be discord.OptionChoice() classes in it.

In your case, you will get:

@warn_command_group.command(name='remove',description='حذف اخطار')
async def remove_warn(self, ctx:discord.ApplicationContext,
                      mode:discord.Option(str,'حالت حذف', required=True, choices=[
                          discord.OptionChoice(name='حذف کل اخطار های سرور', value="deleteAll"),
                          discord.OptionChoice(name='حذف کل اخطار های یک ممبر', value="deleteMemberAll"),
                          discord.OptionChoice(name='حذف یک اخظار خاص(با استفاده از آیدی اون اخطار)', value="deleteWarn")
                      ])
                      ):

   if mode == "deleteAll":
         #Some stuff if the user has selected the first choice

   elif mode == "deleteMemberAll":
        #Some stuff is the user has selected the second choice
   
   else:
        #Some stuff is the user has selected the third choice

See https://docs.pycord.dev/en/master/api.html?highlight=discord%20option#discord.Option.choices

Related