如何验证成员是否有权执行斜杠命令

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

我做了一个明确的斜杠命令,但机器人不验证interaction.user的权限。 我尝试使用该

@commands.has_permissions(administrator=True)
但它不起作用,因为它使用经典的前缀命令。

此外,我还发出了错误消息;

@bot.event
async def on_command_error(interaction, error):
    if isinstance(error, commands.MemberNotFound):
- await interaction.send("Unknown Member!")
    if isinstance(error, commands.MissingPermissions):
        await interaction.send("You do not have right permissions !", delete_after = 3)

这是我的

clear
命令代码:

@bot.tree.command(guild=discord.Object(id=1264715762199691446), name="clear", description="Supprimer un certain nombre de messages.")
@commands.has_permissions(manage_messages = True)
@app_commands.describe(nombre="Le nombre de messages à supprimer.")
async def clear(interaction: discord.Interaction, nombre : int):
    messages = [message async for message in interaction.channel.history(limit = nombre)]
    for message in messages:
        await message.delete()
        

    return await interaction.response.send_message(f"**<:trash:1264750959372927057> __{nombre}__ message(s) supprimé(s).**", delete_after=3)

我实际上希望我的机器人使用斜线命令检测到缺少权限,并向用户抛出一条错误消息,如上所示。 另外,由于该成员没有正确的权限,我希望机器人不返回任何内容。

谢谢你。(我也是法语,所以如果你在字符串中看到一些法语行,那是正常的)

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

相同的东西,但不同的装饰器,而不是使用

commands.has_permissions
使用
app_commands.checks.has_permissions
:

from discord import app_commands


@bot.tree.command(guild=discord.Object(id=1264715762199691446), name="clear", description="Supprimer un certain nombre de messages.")
@app_commands.checks.has_permissions(manage_messages=True)
@app_commands.describe(nombre="Le nombre de messages à supprimer.")
async def clear(interaction: discord.Interaction, nombre : int):
    messages = [message async for message in interaction.channel.history(limit = nombre)]
    for message in messages:
        await message.delete()
        

    return await interaction.response.send_message(f"**<:trash:1264750959372927057> __{nombre}__ message(s) supprimé(s).**", delete_after=3)

参考:

app_commands.checks.has_permissions

© www.soinside.com 2019 - 2024. All rights reserved.