Skip to content
Advertisement

How to stop limit of channel.purge decreasing when check returns false?

I have a command that is meant to go through and remove the last x number of messages sent by bots in a channel. However, the command removes all messages from bots in the last x messages instead of removing the last x number of messages from bots. Is there a way that I can check if x number of messages (from the bots) haven’t been deleted and then increase the value of x, or is there a way to have it not decrease in the first place?

    @client.command(aliases=['bc'])
    async def BotClean(ctx, count=5):
        botCheck = (lambda message : message.author.bot)
        await ctx.channel.purge(limit = count+1, check=botCheck)

Advertisement

Answer

What you are trying to accomplish is pretty hard to do. You will not be able to achieve that using the purge() command, as that limits the messages that will be checked.

You should check the message history within your BotClean function and iterate over the messages retrieved to see if the author is the bot, in case it is the bot, just delete it.

The problem that arises with this design is which would be the limit of history messages that you want to retrieve, as retrieving all of them each time would be pretty dumb memory-wise.

Code example should be something like this:

@client.command(aliases=['bc'])
async def BotClean(self, ctx, number: int):
    messages = await channel.history(limit=200).flatten()
    count = 0
    for message in messages:
        if message.author == client.user and count < number
            count += 1
            await message.delete()
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement