pythonpython-3.xdiscord.py

AppCommand get endless parameters


Original:

@client.tree.command()
@app_commands.describe(members='The members you want to get the joined date from; defaults to the user who uses the command')
async def joined(interaction: discord.Interaction, members: Optional[List[discord.Member]]):
    """Says when a members joined."""
    # If no member is explicitly provided then we use the command user here
    members = members or [interaction.user]
    text = ""
    for member in members:
        text += f'{member} joined {discord.utils.format_dt(member.joined_at)}\n'
    # The format_dt function formats the date time into a human readable representation in the official client
    await interaction.response.send_message(text)

members: Optional[List[discord.Member]]

I was: /joined Member1 Member2 Member3 ...

Error: unsupported type annotation typing.List[discord.member.Member] File "*", line *, in async def joined(interaction: discord.Interaction, members: Optional[List[discord.Member]]): TypeError: unsupported type annotation typing.List[discord.member.Member]


Solution

  • Discord slash commands do not support endless parameters/variadic options. The easiest solution would be to only allow a single member per command and make the user run it multiple times.

    If you insist on using multiple members per command, you will need to use a workaround by creating a str argument, making the user put multiple mentions in there and then parse them. Here is an example using regular expressions:

    import re
    
    @client.tree.command()
    @app_commands.describe(mentions='The members you want to get the joined date from; defaults to the user who uses the command')
    async def joined(interaction: discord.Interaction, mentions: Optional[str]):
        if not mentions:
            members = [interaction.user]
        else:
            members = []
            for user_id in re.findall(r"\d{17,19}", mentions):
                member = interaction.guild.get_member(int(user_id))
                if member:
                    members.append(member)
    

    This requires Server Members privileged intent.

    Reference