error-handling - 如果我的机器人没有权限,我希望它发送错误消息
问题描述
我在 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.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}")
推荐阅读
- python-2.7 - 元素的 QPainterPath 百分比/t 值
- phpbb - phpBB 的 Vagrantfile 给出有关缺少 laravel 宅基地文件的错误
- javascript - Django - 使用 Javascript 将 ID 传递到 url 模板标签
- html - 在另一台笔记本电脑中打开网页设计的不同视图
- javascript - Javascript Mouseup / Touchend eventListener 变量未定义
- arangodb - 获取结果集的索引
- c++ - 数组大于 C++ 中的指定大小
- javascript - 我从哪里调用“付款成功”消息?
- regex - 带下划线和正斜杠的 Perl RegEX
- ios - 将图像覆盖到 CGRect swift