0
votes

i wrote a bot using Python 3.6 IDLE and i want to add a purge command but i don't know how to add it. Can some of you people help me with giving any examples or suggestion i will be pleased thanks.

3
There's a Client.delete_message coroutine. What exactly are you struggling with?Patrick Haugh
Which messages would be deleted after calling your purge command? All the messages in that channel, or all the messages on that server, all the messages from a specific user?Patrick Haugh

3 Answers

1
votes
@bot.command(pass_context=True)
async def purge(ctx, amount):
    await bot.purge_from(ctx.message.channel, limit=amount)

pretty sure that would work.

0
votes
@client.command(pass_context = True, aliases=['Clear'])
async def clear(ctx, number):
    mgs = [] #Empty list to put all the messages in the log
    number = int(number) #Converting the amount of messages to delete to an 
    #integer(number)
    async for x in ctx.logs_from(ctx.message.channel, limit = number):
        mgs.append(x)
    await ctx.delete_messages(mgs)
0
votes

I have created this command myself and I am sharing it with you.

First I would add the code and then explain.

Code:

@MyBot.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def clear(ctx, limit):
    await ctx.message.delete()
    limit = int(limit)
    deleted = await ctx.channel.purge(limit=limit)
    cofirmdelete_embed = discord.Embed(title='Delete Successfull!', description=f'Deleted {len(deleted)} messages in #{ctx.channel}', color=0x4fff4d)
    await ctx.channel.send(embed=cofirmdelete_embed, delete_after=4.0)

Explanation:

  • @command.has_permissions(administrator=True) is added so that this command can be used by administrators only. You can use any discord permissions here and set them to True or False as per your wish. Eg: manage_messages=True

  • In async def clear(ctx, limit), limit is the amount of messages that you want to delete.

  • ctx.message.delete() deletes the command that you enter to clear. That is, when you give the bot command: !clear it would first delete this command and then execute the latter.

  • deleted is the variable that I use to get the number of messages that were deleted.

  • await ctx.channel.purge(limit=limit). Here, purge is a coroutine in the discord.py library. It deleted the messages with the limit that was added by the user.

  • In the confirmdelete_embed, I have added a description where I have added len(delete). This counts the number of deleted messages and then add it to the embed.


This was all that I could explain. I am still here if something comes up or you have any doubt. Feel free to ask me.