首页 > 解决方案 > 如果我的机器人没有权限,我希望它发送错误消息

问题描述

我在 discord.py 中编写了一个不和谐的机器人,我有一个问题。当 BOT 没有权限以及机器人缺少哪些权限时,我想要一个错误处理程序。这是我现在拥有的错误处理程序,但我不知道如何使用机器人权限以及他缺少哪个权限。

async def on_command_error(ctx, error):
  if isinstance(error, commands.MissingRequiredArgument):
    embe=discord.Embed(title="<:redcross:781952086454960138>Error", description="**Please pass in all required arguments!**", color=0x7289da)
    await ctx.send(embed=embe)

  elif isinstance(error, commands.MissingPermissions):
    embe=discord.Embed(title="<:redcross:781952086454960138>Error", description="**Insufficient permissions!**", color=0x7289da)
    await ctx.send(embed=embe)
  else:
    raise error

所以我希望机器人返回

embe=discord.Embed(title="<:redcross:781952086454960138>Error", description="**I dont have the right permissions to do that! Please give me {missingpermission}!**", color=0x7289da)

标签: error-handlingdiscord.py

解决方案


您可以使用,顺便说一下,当调用者没有权限而不是机器人本身error.missing_perms时,会引发此错误。

if isinstance(error, commands.MissingPermissions):
    await ctx.send(f"Permissions needed {error.missing_perms}")

如果您希望在机器人没有使用装饰器commands.bot_has_permissions(**perms)和错误处理程序所需的权限时引发错误commands.BotMissingPermissions,这里有一个示例:

@bot.command()
@commands.bot_has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
    await member.kick(reason=reason)


@kick.error
async def kick_error(ctx, error):
    if isinstance(error, commands.BotMissingPermissions):
        await ctx.send(f"I don't have the required permissions, please enable {error.missing_perms}")

推荐阅读