Bosen
Bosen

Reputation: 139

Python bot delete messages

How to make bot delete xx messages. example !clear xx and !clear xx @user

If i use `!clear xx` it should delete xx message.
If i use `!clear xx @user` it should delete xx message of User tagged.

Upvotes: 0

Views: 938

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 60944

Using this answer as a starting point:

from discord.ext.commands import has_permissions
from datetime import datetime

@bot.command(pass_context=True)
@has_permissions(administrator=True)  # Like the properties of a Permissions object
async def censor(ctx, limit: int, target: discord.User = None):
    check = (lambda message: message.author.id == target.id) if target else None
    try:
        await bot.purge_from(ctx.message.channel, 
                             check=check, 
                             limit=limit)
    except discord.Forbidden:
        await bot.say("I don't have permission to do that")

@censor.error
async def censor_error(error, ctx):
    if isinstance(error, commands.CheckFailure):
        await bot.send_message(ctx.message.channel, "You don't have permissions")

Note that purge_from requires a bot account.

Upvotes: 1

Related Questions