Skip to content

Commit

Permalink
Code Refactoring, Admin Cog
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoz committed Nov 15, 2024
1 parent 86a45f4 commit 66bf78a
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 6 deletions.
96 changes: 96 additions & 0 deletions cogs/admin/admin.py
Original file line number Diff line number Diff line change
@@ -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))
13 changes: 7 additions & 6 deletions cogs/tickets.py → cogs/admin/tickets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <true/false> <true/false>` command.",
value=f"Toggle DM on close and transcript generation using the `{prefix}tickets transcript <true/false> <true/false>` command.",
inline=False
)
embed.set_footer(text="Still in testing...", icon_url=constants.FOOTER_IMAGE)
Expand Down

0 comments on commit 66bf78a

Please sign in to comment.