尽管已授予 Bot 管理员权限,但仍请求 Kick Member 许可

时间:2021-05-18 22:46:31

标签: discord discord.py

所以我正在设置一个机器人,我正在测试它。我有一个小功能是踢,禁止和取消禁止用户,在一个齿轮中设置,如下所示:

import discord
from discord.ext import commands

class Moderator(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

#KICK command
@commands.command()
@commands.has_permissions(kick_members=True)
async def kick(self, ctx, member : discord.Member, *, reason=None):
    ('About to kick')
    await member.kick(reason = reason)

@commands.command()
@commands.has_permissions(kick_members=False)
async def kick(self, ctx, member : discord.Member, *, reason=None):
    await ctx.send(f'You do not have permission to kick any member, {ctx.message.author.mention}!')

#BAN command
@commands.command()
@commands.has_permissions(ban_members=True)
async def ban(self, ctx, member : discord.Member, *, reason=None):
    await member.ban(reason = reason)
    await ctx.send(f'Banned {member.mention}')

@commands.command()
@commands.has_permissions(kick_members=False)
async def ban(self, ctx, member : discord.Member, *, reason=None):
    await ctx.send(f'You do not have permission to ban any member, {ctx.message.author.mention}!')

#UNBAN command
@commands.command()
@commands.has_permissions(ban_members=True)
async def unban(self, ctx, *, member):
    banned_users = await ctx.guild.bans()
    member_name, member_discriminator = member.split('#')

    for ban_entry in banned_users:
        user = ban_entry.user

        if (user.name, user.discriminator) == (member_name, member_discriminator):
            await ctx.guild.unban(user)
            await ctx.send(f'Unbanned {user.mention}')
            return

@commands.command()
@commands.has_permissions(kick_members=False)
async def unban(self, ctx, member : discord.Member, *, reason=None):
    await ctx.send(f'You do not have permission to unban any member, {ctx.message.author.mention}!')


#CLEAR MESSAGES
@commands.command()
@commands.has_permissions(manage_messages=True)
async def clear(self, ctx, amount=2):
    await ctx.channel.purge(limit=amount)

@commands.command()
@commands.has_permissions(manage_messages=False)
async def clear(self, ctx, amount=2):
    await ctx.send(f'You do not have permission to delete messages in this way, {ctx.message.author.mention}!')

def setup(bot):
    bot.add_cog(Moderator(bot))

现在我已经用一些空格格式化了上面的代码,使其适合一个代码块,所以如果你将它复制粘贴到其他地方,你可能会遇到缩进错误。 >

继续,机器人本身也具有管理员权限以及单独的踢和禁止权限。它也位于角色层次结构的顶部,被视为:

Role Hierarchy

我的机器人的名字是 JunkBot。

现在,每当我作为服务器所有者时,尝试使用命令.kick @user,它会弹出以下错误:

Error upon using kick command

错误的文本形式为:

Ignoring exception in command kick:
Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
    await self.prepare(ctx)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 777, in prepare
    if not await self.can_run(ctx):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 1087, in can_run
    return await discord.utils.async_all(predicate(ctx) for predicate in predicates)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\utils.py", line 348, in async_all
    for elem in gen:
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 1087, in <genexpr>
    return await discord.utils.async_all(predicate(ctx) for predicate in predicates)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 1790, in predicate
    raise MissingPermissions(missing)
discord.ext.commands.errors.MissingPermissions: You are missing Kick Members permission(s) to run this command.

禁止、取消禁止和清除消息命令出现类似错误。

有趣的部分是,作为所有者的我得到了这个错误,但是假设另一个用户,我的一个没有踢、禁止或消息管理角色的朋友,完美地运行他们的代码行,在哪里她从机器人那里得到消息,说她无权踢、禁止或清除消息。附上截图。

我不知道我哪里出错了。请帮我调试一下。

2 个答案:

答案 0 :(得分:2)

这不是错误处理的工作方式。您应该查看文档的 error handling 部分,了解如何处理缺少的权限。

编辑,因为人们提醒我提供更多信息

您不应该通过 has_permissions 处理错误。从我从您的代码中可以看出,您希望在调用者没有使用该命令所需的权限时发送消息。这可以通过错误处理程序来实现。错误处理程序将捕获 MissingPermissions 异常并基于此执行某些操作。下面是一个例子:

@commands.Cog.listener()
async def on_command_error(self, ctx, error): #this is the event that catches errors
    if isinstance(error, commands.MissingPermissions): #seeing if the error is the missing permissions error
        await ctx.send(f"You do not have the permission to do this, {ctx.author.mention}!")

这使用了全局错误处理程序,这意味着无论何时捕获到错误,它都会捕获它。但是,如果您想要每个命令的错误处理程序,您只需更改装饰器和函数名称。例如:

@kick.error
async def on_kick_error(self, ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send(f"You do not have the permission to kick any member, {ctx.author.mention} ")

这个只会在 kick 命令错误时触发,其他都不会。

答案 1 :(得分:2)

您的问题在于重写函数。我假设您希望制作一个 errorhandler。但是,这不是这样做的方法。

您需要制作正确版本的错误处理程序,而不是使用不同的 has_permissions 参数重写函数。

首先,删除所有重复的函数。这些是在 False 参数上需要 has_permissions 的函数。

接下来,制作一个错误处理函数。您可以只为 Cog 命令、所有命令或特定命令创建一个。如果它用于所有命令,您将使用事件 on_command_error,(我建议在主文件中或作为侦听器使用它)。如果是用于 Cog,则必须使用 cog_command_error。如果是针对特定命令,则应制作 Command.error 装饰器。
我将展示 Cog 特定的处理程序,但来回切换不会花费太长时间。

# Indent into Cog level
async def cog_command_error(self, ctx, error):
    # Allows us to check for original exceptions raised and sent to CommandInvokeError.
    # If nothing is found. We keep the exception passed to on_command_error.
    error = getattr(error, 'original', error)
   
    if isinstance(error, commands.BotMissingPermissions): # Check if bot is missing permissions
        await ctx.send(f'I am missing these permissions to do this command:\n{self.lts(error.missing_perms)}')

    elif isinstance(error, commands.MissingPermissions): # Check if user is missing permissions
        await ctx.send(f'You are missing these permissions to do this command:\n{self.lts(error.missing_perms)}')


@staticmethod
def lts(list_: list): # Used to make reading the list of permissions easier.
    """List to string.
        For use in `self.on_command_error`"""
    return ', '.join([obj.name if isinstance(obj, discord.Role) else str(obj).replace('_', ' ') for obj in list_])

以下是您可以在处理程序中使用的异常列表:Exceptions

仅供参考,处理程序不需要我拥有的 lts 函数。它用于使权限列表在一个字符串中可读。您可以随时将其删除或根据需要进行更改。