首页 > 解决方案 > 显示 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

那么如何打印缺少的权限呢?

标签: pythondiscord.pydiscord.py-rewrite

解决方案


所以这段代码将打印所有必需的权限,不仅是机器人没有的权限,而且我认为它仍然很不错

 @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

推荐阅读