File size: 3,123 Bytes
7cc32a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import discord
from discord.ext import commands


class Moderation(commands.Cog):
    def __init__(self, bot: commands.Bot):
        self.bot = bot

    @commands.hybrid_command(name="purge")
    @commands.has_permissions(manage_messages=True)
    async def purge(self, ctx: commands.Context, limit: int):
        """Delete a number of messages from the current channel."""
        deleted = await ctx.channel.purge(limit=limit + 1)
        await ctx.send(f"Deleted {len(deleted) - 1} messages.", delete_after=5)

    @commands.hybrid_command(name="kick")
    @commands.has_permissions(kick_members=True)
    async def kick(self, ctx: commands.Context, member: discord.Member, *, reason: str | None = None):
        """Kick a member from the server."""
        await member.kick(reason=reason)
        await ctx.send(f"Kicked {member} " + (f"for: {reason}" if reason else ""))

    @commands.hybrid_command(name="delete")
    async def delete(self, ctx: commands.Context, amount: int, user: discord.Member = None):
        """Delete messages in this channel (only works in specific server)"""
        if ctx.guild.id != 811634680302010449:
            await ctx.send("This command can only be used in the authorized server.")
            return
        
        if amount < 1:
            await ctx.send("Please specify a valid number of messages to delete (at least 1).")
            return
        
        is_slash = ctx.interaction is not None
        
        if user:
            if is_slash:
                await ctx.defer(ephemeral=True)
            
            deleted_count = 0
            def check(m):
                nonlocal deleted_count
                if m.author.id == user.id and deleted_count < amount:
                    deleted_count += 1
                    return True
                return False
            
            await ctx.channel.purge(limit=100, check=check)
            success_msg = f"Deleted {deleted_count} message(s) from {user.mention}."
            
            if is_slash:
                await ctx.send(success_msg, ephemeral=True)
            else:
                await ctx.send(success_msg, delete_after=10)
        else:
            limit = amount if is_slash else amount + 1
            deleted = await ctx.channel.purge(limit=limit)
            actual_deleted = len(deleted) if is_slash else len(deleted) - 1
            success_msg = f"Deleted {actual_deleted} message(s)."
            
            if is_slash:
                await ctx.send(success_msg, ephemeral=True)
            else:
                await ctx.send(success_msg, delete_after=10)


    @purge.error
    async def purge_error(self, ctx: commands.Context, error: Exception):
        if isinstance(error, commands.MissingPermissions):
            await ctx.send("You lack the manage_messages permission.")

    @kick.error
    async def kick_error(self, ctx: commands.Context, error: Exception):
        if isinstance(error, commands.MissingPermissions):
            await ctx.send("You lack the kick_members permission.")


async def setup(bot: commands.Bot):
    await bot.add_cog(Moderation(bot))