Search code examples
discorddiscord.pybots

How to Retrieve a slash Command's ID using Discord.py


I am working on a Discord bot and trying to create a clickable slash command link. However, I'm having trouble finding the ID the a slash command

command = self.bot.tree.get_command("rank")
await message.reply(f"To view your rank, please click here </rank:{command.id}> 🔍")

Unfortunately, I discovered that command does not have an id attribute, which is causing issues in generating the clickable link.


Solution

  • The get_command() function returns bot.command not bot.tree.command. You need the fetch_commands() function for that.

    Here is the example:

    @bot.command()
    async def commandid(ctx,name):
        commands = await bot.tree.fetch_commands()
        for cmd in commands:
            if cmd.name == name:
                await ctx.send(cmd.id)