python - 显示 discord bot.py 缺少权限
问题描述
所以我想让我的机器人可以打印某些操作缺少的权限。这是我的代码:
@bot.event
async def on_command_error(ctx, error): #error commands handler
if isinstance(error, commands.BotMissingPermissions):
await ctx.send("Bot didn\'t has such a permissions")
#this line gonna print what are the missing permissions
@bot.command(pass_context = True)
@commands.bot_has_permissions(read_messages = True, manage_messages = True, read_message_history =
True)
async def purge(ctx, amount : int): #clear amount of messages
#do something
那么如何打印缺少的权限呢?
解决方案
所以这段代码将打印所有必需的权限,不仅是机器人没有的权限,而且我认为它仍然很不错
@bot.event
async def on_command_error(ctx, error): #error commands handler
if isinstance(error, commands.BotMissingPermissions):
await ctx.send("Bot didn\'t has such a permissions")
await.ctx.send(f'{commands.BotMissingPermissions(["read_message",
"manage_messages", "read_message_history"])}') #this line gonna print what
are the required permissions
@bot.command(pass_context = True)
@commands.bot_has_permissions(read_messages = True, manage_messages = True,
read_message_history = True)
async def purge(ctx, amount : int): #clear amount of messages
#do something
推荐阅读
- python - 使用python的re从字符串中删除URL
- ssl - traefik ssl 证书背后的代理
- c++ - 在 C++ 中以多态类型存储的 RTTI 信息、vtables 等的正式名称是什么?
- android - 有更新时通知
- c# - 复选框总是显示 False 或者它是 True
- python - 用于列表分解的优雅 Python 代码
- mysql - 我的查询生成器中有语法错误
- python-3.x - 如何获取电子邮件 gmail python API 的主题
- amazon-web-services - 我创建了 4 个 EC2 并创建了 2 个目标组,每个目标组有 2 个 EC2。我创建了负载均衡器并根据片段创建了规则
- sql - 如何从另一个表更新 Oracle 表中的列