简体   繁体   English

Discord.py 终生无法设置斜杠命令

[英]Discord.py cannot set up slash commands for the life of me

Whenever I attempt to make a slash command in discord.py, the commands never work or even show up no matter how many times I sync them.每当我尝试在 discord.py 中执行斜杠命令时,无论我同步多少次,这些命令都不会起作用,甚至不会显示。

Here is a snip of the code for the slash command I wish to add `这是我希望添加的斜杠命令的代码片段

@app_commands.command(name='clearnono', description="Cleaw the numbew of nono messages you want to wemove.")
@app_commands.rename(num='quantity')
@app_commands.describe(num='How many?')
async def clear(interaction: discord.Interaction, num: int):
  await interaction.channel.purge(limit=num)
  await interaction.response.send_message(f"{num} messages were cleawed swccessfwlly.", ephemeral=True)

And this is the code I use to sync the commands `这是我用来同步命令的代码`

@bot.command()
@commands.guild_only()
@commands.is_owner()
async def sync(
  ctx: Context, guilds: Greedy[discord.Object], spec: Optional[Literal["~", "*", "^"]] = None) -> None:
    if not guilds:
        if spec == "~":
            synced = await ctx.bot.tree.sync(guild=ctx.guild)
        elif spec == "*":
            ctx.bot.tree.copy_global_to(guild=ctx.guild)
            synced = await ctx.bot.tree.sync(guild=ctx.guild)
        elif spec == "^":
            ctx.bot.tree.clear_commands(guild=ctx.guild)
            await ctx.bot.tree.sync(guild=ctx.guild)
            synced = []
        else:
            synced = await ctx.bot.tree.sync()

        await ctx.send(
            f"Synced {len(synced)} commands {'globally' if spec is None else 'to the current guild.'}"
        )
        return

    ret = 0
    for guild in guilds:
        try:
            await ctx.bot.tree.sync(guild=guild)
        except discord.HTTPException:
            pass
        else:
            ret += 1

    await ctx.send(f"Synced the tree to {ret}/{len(guilds)}.")

Expectation: The command is added to the bot, and I am able to use the slash command to clear messages预期:该命令已添加到机器人,我可以使用斜线命令清除消息

What actually happens: The command does not work or even show up when attempting to use it in the bot, despire the normal non-slash commands working just fine.实际发生了什么:该命令不起作用,甚至在尝试在机器人中使用它时甚至不显示,期望正常的非斜线命令工作得很好。

Did you invite your bot with the correct permissions?您是否以正确的权限邀请了您的机器人? Bots need to be invited with the applications.commands scope in addition to the bot scope. For example, invite the bot with the following URL: https://discord.com/oauth2/authorize?client_id=1234567890&scope=applications.commands+bot除了机器人 scope 之外,还需要使用applications.commands scope 邀请机器人。例如,使用以下 URL 邀请机器人: https://discord.com/oauth2/authorize?client_id=1234567890&scope=applications.command

- discord.py FAQ: My bot's commands are not showing up! - discord.py 常见问题解答: 我的机器人命令没有显示!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM