Discord.py kick 命令无效(缺少 perms 错误)

Discord.py kick command not working (missing perms error)

我目前正在开发一个 kick 命令,该命令在对我的 discord 机器人执行操作之前与用户进行双重检查,并想出了这个:

@bot.command()
@commands.has_permissions(manage_guild=True)
async def kick(ctx,
               member: discord.Member = None,
               *,
               reason="No reason provided"):

    server_name = ctx.guild.name
    user = member

    if member == None:
        await ctx.send(
            f'{x_mark} **{ctx.message.author.name},** please mention somebody to kick.')
        return

    if member == ctx.message.author:
        await ctx.send(
            f'{x_mark} **{ctx.message.author.name},** you can\'t kick yourself, silly.')
        return

    embedcheck = discord.Embed(
        title="Kick",
        colour=0xFFD166,
        description=f'Are you sure you want to kick **{user}?**')

    embeddone = discord.Embed(
        title="Kicked",
        colour=0x06D6A0,
        description=f'**{user}** has been kicked from the server.')

    embedfail = discord.Embed(
        title="Not Kicked",
        colour=0xEF476F,
        description=f'The kick did not carry out.')

    msg = await ctx.send(embed=embedcheck)
    await msg.add_reaction(check_mark)
    await msg.add_reaction(x_mark)

    def check(rctn, user):
        return user.id == ctx.author.id and str(rctn) in [check_mark, x_mark]

    while True:
        try:
            reaction, user = await bot.wait_for(
                'reaction_add', timeout=60.0, check=check)
            if str(reaction.emoji) == check_mark:
                await msg.edit(embed=embeddone)
                await user.kick(reason=reason)
                if reason == None:
                    await user.send(
                        f'**{user.name}**, you were kicked from {server_name}. No reason was provided.'
                    )
                else:
                    await user.send(
                        f'**{user.name}**, you were kicked from {server_name} for {reason}.'
                    )

                return

            elif str(reaction.emoji) == x_mark:
                await msg.edit(embed=embedfail)
                return

        except asyncio.TimeoutError:
            await msg.edit(embed=embedfail)
            return

但是,当我这样做时,出现错误:

    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: 403 Forbidden (error code: 50013): Missing Permissions

我不知道为什么会这样,因为机器人已经检查了所有权限,我也是,而且我是服务器所有者。如有任何帮助,我们将不胜感激。

此问题是因为您检查的 manage_guild 权限不正确。另外请记住,您混淆了 botcommands

@bot.command()
@bot.has_permissions(kick_user = True) # to check the user itself
@bot.bot_has_permissions(kick_user = True) # to check the bot
async def kick(ctx):

记得允许所有这样的意图

intents = discord.Intents().all()
bot = commands.Bot(command_prefix="$", intents=intents)

更新:我发现错误,当运行命令时它会尝试踢出命令的用户而不是指定的成员。这是我更新的代码:

  @commands.command()
  @commands.has_permissions(kick_members = True)
  @commands.bot_has_permissions(kick_members = True)
  async def kick(self, ctx, member: discord.Member, *, reason="No reason provided"):

    server_name = ctx.guild.name

    if member == ctx.message.author:
        await ctx.send(
            f'{x_mark} **{ctx.message.author.name},** you can\'t kick yourself, silly.')
        return

    embedcheck = discord.Embed(
        title="Kick",
        colour=0xFFD166,
        description=f'Are you sure you want to kick **{member}?**')

    embeddone = discord.Embed(
        title="Kicked",
        colour=0x06D6A0,
        description=f'**{member}** has been kicked from the server.')

    embedfail = discord.Embed(
        title="Not Kicked",
        colour=0xEF476F,
        description=f'The kick did not carry out.')

    msg = await ctx.send(embed=embedcheck)
    await msg.add_reaction(check_mark)
    await msg.add_reaction(x_mark)

    def check(rctn, user):
        return user.id == ctx.author.id and str(rctn) in [check_mark, x_mark]

    while True:
        try:
            reaction, user = await self.bot.wait_for(
                'reaction_add', timeout=60.0, check=check)
            if str(reaction.emoji) == check_mark:
                await msg.edit(embed=embeddone)
                await member.kick(reason=reason)
                await member.send(f'**{member.name}**, you were kicked from {server_name} for {reason}.')
                return

            elif str(reaction.emoji) == x_mark:
                await msg.edit(embed=embedfail)
                return

        except asyncio.TimeoutError:
            await msg.edit(embed=embedfail)
            return

  @kick.error
  async def kick_error(self, ctx, error):
      if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send(f'{x_mark} **{ctx.message.author.name}**, you need to mention someone to kick.')
      elif isinstance(error, commands.BadArgument):
        await ctx.send(f'{x_mark} **{ctx.message.author.name}**, I could not find a user with that name.')
      else:
        raise error