purge command

got bored
mail@pastecode.io avatarunknown
python
a month ago
1.9 kB
5
Indexable
Never
import typing
from datetime import timedelta
from discord.ext import commands
from discord import User, errors, TextChannel, Forbidden


class Purge(commands.Cog, name='Purge'):
    def __init__(self, client):
        self.client = client

    async def cog_check(self, ctx):
        return self.client.user_is_admin(ctx.author)

    @commands.command(
        name='purge',
        hidden=True,
    )
    async def purge(
        self, ctx,
        num_messages: int,
    ):
        
        channel = ctx.message.channel
        await ctx.message.delete()
        await channel.purge(limit=num_messages)
        return True

    @commands.command(
        name='purge_until',
        hidden=True,
    )
    async def purge_until(
        self, ctx,
        message_id: int,
    ):
       
        channel = ctx.message.channel
        try:
            message = await channel.fetch_message(message_id)
        except errors.NotFound:
            await ctx.send("Message could not be found in this channel")
            return

        await ctx.message.delete()
        await channel.purge(after=message)
        return True

    @commands.command(
        name='purge_user',
        hidden=True,
        aliases=['purgeuser'],
    )
    async def purge_user(
        self, ctx,
        user: User,
        num_minutes: typing.Optional[int] = 5,
    ):
        after = ctx.message.created_at - timedelta(minutes=num_minutes)

        def check(msg):
            return msg.author.id == user.id

        for channel in await ctx.guild.fetch_channels():
            if type(channel) is TextChannel:
                try:
                    await channel.purge(limit=10*num_minutes, check=check, after=after)
                except Forbidden:
                    continue


async def setup(client):
    await client.add_cog(Purge(client))