【问题标题】:Discord.py kick command not working (missing perms error)Discord.py kick 命令不起作用(缺少 perms 错误)
【发布时间】:2020-11-15 05:00:59
【问题描述】:

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

@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

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

【问题讨论】:

    标签: python command discord discord.py


    【解决方案1】:

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

      @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
    

    【讨论】:

      【解决方案2】:

      这个问题是您正在检查 manage_guild 这不是正确的权限。另外请记住,您混淆了bot 和commands

      @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)
      

      【讨论】:

      • 执行此操作后出现错误:Traceback (most recent call last): File "main.py", line 214, in <module> @bot.has_permissions(kick_members = True) AttributeError: 'Bot' object has no attribute 'has_permissions'
      • 你应该使用你在这里命名你的机器人的任何东西bot = commands.Bot(command_prefix="$", intents=intents)
      • 嗯,我试过 intents = discord.Intents.default() bot = commands.Bot(command_prefix="$", intents=intents) 但我收到错误 Traceback (most recent call last): File "main.py", line 14, in <module> intents = discord.Intents.default() AttributeError: module 'discord' has no attribute 'Intents'
      • 检查上面我添加的代码。确保更新你的图书馆。
      猜你喜欢
      • 1970-01-01
      • 2019-08-22
      • 2020-11-17
      • 1970-01-01
      • 2021-01-05
      • 2020-08-07
      • 2020-12-30
      • 2021-05-17
      • 2021-05-13
      相关资源
      最近更新 更多