Search code examples
pythonpython-3.xdiscordpycord

How to get mentioned roles ids in slash command pycord


@bot.slash_command(name = "test", description = "testing command")
async def test(ctx, mention: discord.Option(name="mention", description="mention.", required=True)):
        print(mention.mentions.role_mentions)

i tried to do this but throws error that says str has no attribute mentions

i am trying to get all mentioned roles ids in pycord and got error


Solution

  • You've not specified the type for the mention parameter - by default this is a string (hence your error that "str has no attribute mentions"). The first arg to discord.Option should be the type of the option - the library will then convert that and allow you to select mentionables when using the slash command and you'll have a mentionable object type in the code when the slash command is used. Perhaps consider re-reading this section in the guides.

    @bot.slash_command(name = "test", description = "testing command")
    async def test(
        ctx,
        mention: discord.Option(
            discord.SlashCommandOptionType.mentionable,
            name="mention",
            description="mention.",
            required=True
        )
    ):
        # whatever you want to do with that here