Ask coding questions

← Back to all posts
How do you make a working purge command
CUSULe (0)

I have been trying for a long time now

LD1 (52)

You would use channel.purge() to do this. Here is an example command:

async def purge(ctx, limit: int):
    if limit > 1000:
        await ctx.send("You can only purge up to 1000 messages")
    elif limit < 1:
        await ctx.send("You must purge at least 1 message")


Two things to note. First off, you are using discord.Client instead of discord.ext.commands.Bot. You should always use Bot, never use Client (unless you're making a self bot ig).

All you have to do is replace

client = discord.Client()


bot = discord.ext.commands.Bot(command_prefix="!")

Also, you should add this to your imports:

from discord.ext import commands
epicseg (3)

Here you go! This command will only work for admins.

async def purge(ctx, number: int):
    if not
        await ctx.send("I do not have permission to delete messages.")
    if number > 200:
        await ctx.send("Please specify a lower number. Discord rate limits us, so we cant really go any higher :(")
    to_delete = []
    async for message in
    while to_delete:
        if len(to_delete) > 1:
            to_delete = to_delete[100:]
            await to_delete.delete()
            to_delete = []
        await asyncio.sleep(1.5)
epicseg (3)

Change bot to client. @CUSULe

LD1 (52)

@epicseg This isn't really a good way. It's slow and likely to hit the API ratelimit.

epicseg (3)

@LD1 Better then nothing, but it’s somthing

LD1 (52)

Why not just use channel.purge()? It's litteraly an API endpoint that discord has ended for this purpose. @epicseg

epicseg (3)

@LD1 idk I’m not really good at coding. Srry

LD1 (52)

@epicseg No need to apologize, I was just wondering.