discord.py

Is there a way to allow the selection of multiple users via slash command in discord.py?


As per title, I'd like to know if there is a possibility to allow a user to select multiple discord User via SlashCommand with discord.py.

I've tried to look up in the documentation, but I'm confused because, searching for the allowed AppCommandOptionType, it says

The application command’s option type. This is usually the type of parameter an application command takes.

Emphasis on "usually"; so is it possible to accept other types of parameters?

At this time, my command looks like this:

@app_commands.command(
    name="my_command",
    description="My Command description"
)
async def my_command(self, interaction: "Interaction", user: "User"):
    process_user(user) # Fictional function - The user should be processed here
    await interaction.response.send_message(content=f"You selected {user} and it was correctly processed")

and the user can select a User easily thanks to the Discord dropdown menu, but I'd like to have something along these line (keeping the ease of selection):

@app_commands.command(
    name="my_command",
    description="My Command description"
)
@app_commands.describe(users="list of User slected")
async def my_command(self, interaction: "Interaction", users: List["User"]):
    for user in users:
        process_user(user) # Fictional function - The user should be processed here
    await interaction.response.send_message(content=f"The selected users were correctly processed")

Hence, I have tried to declare it as a list (i.e. user: List["User"]), but I only got

Failed to load extension commands.My_command: Extension 'commands.my_command' raised an error: TypeError: unsupported type annotation typing.List[discord.user.User]

So, there is a workaround or there is an intended way to select multiple Users?


Solution

  • Natively via a parameter - no. Your best options would be using a string parameter where the user puts IDs and parse that (see my past answer for an example), or using a UserSelect UI component (allows up to 25 picked). Or you can just make the user run the command multiple times.

    Here is a quick example of using UserSelect:

    import discord
    from discord.ui import View, UserSelect
    
    async def my_command(interaction: discord.Interaction):
        async def select_callback(interaction):
            await interaction.response.defer()
            for user in select.values:
                print(user)
            await interaction.edit_original_response(content="ok", view=None)
    
        view = View(timeout=3600)
        select = UserSelect(min_values=1, max_values=25)
        select.callback = select_callback
        view.add_item(select)
        await interaction.response.send_message(view=view)