Spaces:
Runtime error
Runtime error
| import discord | |
| from discord.ext import commands | |
| class Moderation(commands.Cog): | |
| def __init__(self, bot: commands.Bot): | |
| self.bot = bot | |
| 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) | |
| 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 "")) | |
| 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) | |
| async def purge_error(self, ctx: commands.Context, error: Exception): | |
| if isinstance(error, commands.MissingPermissions): | |
| await ctx.send("You lack the manage_messages permission.") | |
| 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)) |