How do you make a working purge command
I have been trying for a long time now
epicseg
Here you go! This command will only work for admins.
@bot.command(pass_context=True) @commands.has_permissions(administrator=True) async def purge(ctx, number: int): if not ctx.message.channel.permissions_for(ctx.message.author.guild.me).manage_messages: await ctx.send("I do not have permission to delete messages.") return if number > 200: await ctx.send("Please specify a lower number. Discord rate limits us, so we cant really go any higher :(") return to_delete = [] async for message in ctx.message.channel.history(limit=number+1): to_delete.append(message) while to_delete: if len(to_delete) > 1: await ctx.message.channel.delete_messages(to_delete[:100]) to_delete = to_delete[100:] else: await to_delete.delete() to_delete = [] await asyncio.sleep(1.5)
CircuitSacul
@epicseg This isn't really a good way. It's slow and likely to hit the API ratelimit.
CircuitSacul
Why not just use channel.purge()? It's litteraly an API endpoint that discord has ended for this purpose. @epicseg
CircuitSacul
@epicseg No need to apologize, I was just wondering.
You would use
channel.purge()
to do this. Here is an example command:Two things to note. First off, you are using
discord.Client
instead ofdiscord.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
with
Also, you should add this to your imports: