discord 清除限制解决方法:self.bot.delete_messages() 错误

discord purge limit workaround: self.bot.delete_messages() error

所以我一直在尝试通过将清除分成几组 100 组来解决 discord 100 消息 discord.py 清除限制,但我 运行 遇到了问题,当我用这个命令,我报错了。我已经测试过,将消息保存到列表

错误

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)

而不是不断地对变量进行硬编码来处理溢出。我建议简单地按递减的数量对单个清除进行分块。

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