首页 > 解决方案 > 当用户没有权限时发送错误消息(Discord Bot)

问题描述

我正在制作一个不和谐的机器人,并且想在用户没有权限使用我尝试了几个小时的命令(使用 if 和 else)时添加一条“你无权使用此命令”消息,并且可以没办法

    @commands.command(aliases=['limpar'])
    @commands.has_permissions(manage_messages=True)
    async def clear(self, ctx, amount : int):
            await ctx.channel.purge(limit=amount+1)
            embed=discord.Embed(title="", url="", description=f"Squeaky clean!!!", color=0x000000)
            await ctx.send(embed=embed, delete_after=2)
            print('Squeaky clean!!!')

标签: pythonpermissionsdiscord.py

解决方案


您可以使用错误处理程序事件,它将用于所有命令。

@commands.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send(f"You do not have {commands.MissingPermissions.missing_perms} to run this command.")

commands.MissingPermissions


推荐阅读