代码之家  ›  专栏  ›  技术社区  ›  Jacques Amsel

不一致清除限制解决方法:自我。机器人程序。delete\u messages()错误

  •  0
  • Jacques Amsel  · 技术社区  · 7 年前

    所以我一直在努力解决100条不和谐信息。py purge limit通过将清除分为几组100个,但我遇到了一个问题,当我使用这个命令时,我发现了一个错误。我已经测试过了 节省物 列表中的消息

    错误

    Ignoring exception in command purge
    Traceback (most recent call last):
      File "C:\Users\info\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 50, in wrapped
        ret = yield from coro(*args, **kwargs)
      File "C:\Users\info\OneDrive\Desktop\Azogthe Rewrite\moderation.py", line 312, in purge
        await self.bot.delete_messages(mgs1)
      File "C:\Users\info\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\client.py", line 1301, in delete_messages
        yield from self.http.delete_messages(channel.id, message_ids, guild_id)
      File "C:\Users\info\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\http.py", line 200, in request
        raise HTTPException(r, data)
    discord.errors.HTTPException: BAD REQUEST (status code: 400)
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "C:\Users\info\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\bot.py", line 846, in process_commands
        yield from command.invoke(ctx)
      File "C:\Users\info\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 374, in invoke
        yield from injected(*ctx.args, **ctx.kwargs)
      File "C:\Users\info\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 54, in wrapped
        raise CommandInvokeError(e) from e
    discord.ext.commands.errors.CommandInvokeError: Command raised an exception: HTTPException: BAD REQUEST (status code: 400)
    

    密码

            @commands.command(pass_context=True, no_pm=True)
            async def purge(self, ctx, amount = None, channel:discord.Channel = None):
                mgs1 = []
                mgs2 = []
                mgs3 = []
                mgs4 = []
                mgs5 = []
                if channel == None:
                    channel = ctx.message.channel
                amount = int(amount)
                if amount > 1:
                    async for x in self.bot.logs_from((channel), limit = int(amount+1)):
                        while len(mgs1) < 100:
                            mgs1.append(x)
                        while len(mgs1) == 100 and len(mgs2) < 100:
                            mgs2.append(x)
                        while len(mgs2) == 100 and len(mgs3) < 100:
                            mgs3.append(x)
                        while len(mgs3) == 100 and len(mgs4) < 100:
                            mgs4.append(x)
                        while len(mgs4) == 100 and len(mgs5) < 100:
                            mgs5.append(x)
                        print(mgs1, mgs2, mgs3, mgs4, mgs5)
                        await self.bot.delete_messages(mgs1)
                        await self.bot.delete_messages(mgs2)
                        await self.bot.delete_messages(mgs3)
                        await self.bot.delete_messages(mgs4)
                        await self.bot.delete_messages(mgs5)
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   mental    7 年前

    而不是不断地硬编码变量来处理溢出。我建议你只需减少每次清洗的次数。

    @commands.command(pass_context=True, no_pm=True)
    async def purge(self, ctx, amount, channel: discord.Channel=None):
        channel = channel or ctx.message.channel
        try:
            amount = int(amount)
            await self.bot.delete_message(ctx.message)
            for amount in range(amount, 0, -100):
                await self.bot.purge_from(channel, limit=amount)
        except ValueError:
            return await self.bot.say('You have to enter a full number!')
    

    在这里,无论用户输入什么,命令都将在for循环中清除,每次迭代减少100。