From 66bf78a26ad45b1a272bc51b41892b9c68a8e103 Mon Sep 17 00:00:00 2001 From: Kozejin <2613841+dkoz@users.noreply.github.com> Date: Fri, 15 Nov 2024 10:33:03 -0500 Subject: [PATCH] Code Refactoring, Admin Cog --- cogs/admin/admin.py | 96 +++++++++++++++++++++++++++++++++++++ cogs/{ => admin}/tickets.py | 13 ++--- 2 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 cogs/admin/admin.py rename cogs/{ => admin}/tickets.py (94%) diff --git a/cogs/admin/admin.py b/cogs/admin/admin.py new file mode 100644 index 0000000..8160015 --- /dev/null +++ b/cogs/admin/admin.py @@ -0,0 +1,96 @@ +import nextcord +from nextcord.ext import commands +from nextcord.ext.commands import has_permissions +import datetime + +class AdminCog(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.group(name="admin", alias="a", description="Show list of admin commands.", invoke_without_command=True) + @has_permissions(administrator=True) + async def admin(self, ctx): + prefix = ctx.prefix + + embed = nextcord.Embed( + title="Administrative Commands", + description=f"`{prefix}admin kick` - Kick a user\n" + f"`{prefix}admin ban` - Ban a user\n" + f"`{prefix}admin unban` - Unban a user\n" + f"`{prefix}admin purge` - Purge messages\n" + f"`{prefix}admin purgeuser` - Purge messages from a user\n" + f"`{prefix}admin mute` - Mute a user\n" + f"`{prefix}admin unmute` - Unmute a user\n" + f"`{prefix}admin timeout` - Timeout a user", + color=nextcord.Color.blue() + ) + await ctx.send(embed=embed) + + @admin.command(name="kick") + @has_permissions(kick_members=True) + async def kick(self, ctx, member: nextcord.Member, *, reason=None): + await member.kick(reason=reason) + await ctx.send(f"{member} has been kicked.") + + @admin.command(name="ban") + @has_permissions(ban_members=True) + async def ban(self, ctx, member: nextcord.Member, *, reason=None): + await member.ban(reason=reason) + await ctx.send(f"{member} has been banned.") + + @admin.command(name="unban") + @has_permissions(ban_members=True) + async def unban(self, ctx, *, member): + banned_users = await ctx.guild.bans() + member_name, member_discriminator = member.split("#") + + for ban_entry in banned_users: + user = ban_entry.user + + if (user.name, user.discriminator) == (member_name, member_discriminator): + await ctx.guild.unban(user) + await ctx.send(f"{user.mention} has been unbanned.") + return + + await ctx.send(f"{member} was not found.", ephemeral=True) + + @admin.command(name="purge") + @has_permissions(manage_messages=True) + async def purge(self, ctx, amount: int): + await ctx.channel.purge(limit=amount + 1) + await ctx.send(f"{amount} messages have been deleted.", ephemeral=True) + + @admin.command(name="purgeuser") + @has_permissions(manage_messages=True) + async def purgeuser(self, ctx, member: nextcord.Member, amount: int): + def is_member(message): + return message.author == member + + await ctx.channel.purge(limit=amount + 1, check=is_member) + await ctx.send(f"{amount} messages from {member} have been deleted.", ephemeral=True) + + @admin.command(name="mute") + @has_permissions(mute_members=True) + async def mute(self, ctx, member: nextcord.Member): + await member.edit(mute=True) + await ctx.send(f"{member.mention} has been muted.", ephemeral=True) + + @admin.command(name="unmute") + @has_permissions(mute_members=True) + async def unmute(self, ctx, member: nextcord.Member): + await member.edit(mute=False) + await ctx.send(f"{member.mention} has been unmuted.", ephemeral=True) + + @admin.command(name="timeout") + @has_permissions(moderate_members=True) + async def timeout(self, ctx, member: nextcord.Member, duration: int, *, reason=None): + if duration <= 0: + await ctx.send("Please provide a valid duration in minutes.") + return + + timeout_duration = datetime.timedelta(minutes=duration) + await member.timeout(timeout_duration, reason=reason) + await ctx.send(f"{member.mention} has been timed out for {duration} minutes. Reason: {reason or 'No reason provided.'}") + +def setup(bot): + bot.add_cog(AdminCog(bot)) \ No newline at end of file diff --git a/cogs/tickets.py b/cogs/admin/tickets.py similarity index 94% rename from cogs/tickets.py rename to cogs/admin/tickets.py index 161994a..024d665 100644 --- a/cogs/tickets.py +++ b/cogs/admin/tickets.py @@ -75,34 +75,35 @@ async def tickets(self, ctx): @tickets.command(name="setup") @has_permissions(manage_channels=True) async def setup(self, ctx): + prefix = ctx.prefix embed = nextcord.Embed( title="Ticket System Setup Guide", - description="Follow the steps below to set up the ticket system.", + description=f"Follow the steps below to set up the ticket system.", color=nextcord.Color.orange() ) embed.add_field( name="Step 1: Create a Ticket Channel", - value="Create a new text channel where users can create tickets. Use the `[p]tickets channel` command to set this channel.", + value=f"Create a new text channel where users can create tickets. Use the `{prefix}tickets channel` command to set this channel.", inline=False ) embed.add_field( name="Step 2: Set Log Channel (Optional)", - value="Set a log channel to log ticket creations and closures. Use the `[p]tickets logchannel` command to set this channel.", + value=f"Set a log channel to log ticket creations. Use the `{prefix}tickets logchannel` command to set this channel.", inline=False ) embed.add_field( name="Step 3: Add Ticket Categories", - value="Add ticket categories using the `[p]tickets addcategory` command. Users can create tickets in these categories.", + value=f"Add ticket categories using the `{prefix}tickets addcategory` command. Users can create tickets in these categories.", inline=False ) embed.add_field( name="Step 4: Add Ticket Roles", - value="Add roles that can access the ticket system using the `[p]tickets role` command.", + value=f"Add roles that can access the ticket system using the `{prefix}tickets role` command.", inline=False ) embed.add_field( name="Step 5: Toggle DM on Close and Transcript Generation", - value="Toggle DM on close and transcript generation using the `[p]tickets transcript ` command.", + value=f"Toggle DM on close and transcript generation using the `{prefix}tickets transcript ` command.", inline=False ) embed.set_footer(text="Still in testing...", icon_url=constants.FOOTER_IMAGE)