如何使用 Discord.py 检索斜杠命令的 ID

问题描述 投票:0回答:1

我正在开发一个 Discord 机器人,并尝试创建一个可点击的斜杠命令链接。但是,我无法找到斜杠命令的 ID

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

不幸的是,我发现

command
没有
id
属性,这导致生成可点击链接时出现问题。

discord discord.py bots
1个回答
0
投票

get_command()
函数返回
bot.command
而不是
bot.tree.command
。 为此,您需要
fetch_commands()
函数。

这是例子:

@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)
© www.soinside.com 2019 - 2024. All rights reserved.