discord - 尽管已授予 Bot 管理员权限,但仍要求踢成员(member)许可

标签 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))

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

继续前进,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.

ban、unban 和 clear messages 命令会引发类似的错误。

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

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

最佳答案

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

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

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

接下来,制作一个错误处理函数。您可以只为 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 函数。它用于使权限列表在一个字符串中可读。您可以随时删除它或根据需要更改它。

关于discord - 尽管已授予 Bot 管理员权限,但仍要求踢成员(member)许可,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67594883/

相关文章:

javascript - message.content 在 Discord.js v14 中没有任何值(value)

node.js - Discord Oauth2 访问 token 'Grant type None is not supported'

python - Discord.py ReWrite-@has_permissions无法正常工作

python - 在 python 中编程 Discord 机器人 - 如何让机器人从一组图像中发送随机图像?

python - 带 Discord.py 跳线的 Discord Bot

python - 如何使用discord.py检索以前的消息

python - 将上下文传递给后台任务discord.py

python-3.x - 如何让 discord bot 队列本地 mp3?

javascript - 创建角色,然后将该角色分配给成员不起作用,是否有更好的方法来处理这个问题? Discord.js

python - 如何在on_message中检查用户是否具有特定角色?