discord purge limit workaround:self.bot.delete_messages()错误

时间:2018-01-09 13:11:48

标签: discord.py

所以我一直试图解决discord 100消息discord.py清除限制,将清除分成几组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 个答案:

答案 0 :(得分:1)

而不是经常硬编码变量来处理溢出。我建议只是按比例减少个别清除量。

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