refactor(moderation): commands into group cog

Converted individual moderation command cogs (ban, kick, purge, warnings, archive, hackban, nick) into command factory functions and registered them under a new Moderation GroupCog in cogs/moderation/__init__.py. Updated bot.py and help.py to support the new moderation group and category. This improves organization and enables grouped moderation commands.
This commit is contained in:
neoarz
2025-09-28 23:07:46 -04:00
parent 51393ece85
commit e5ea7f1dac
10 changed files with 112 additions and 79 deletions

View File

@@ -79,11 +79,7 @@ class Help(commands.Cog, name="help"):
"translate": "utilities",
# Miscellaneous Commands
"keanu": "miscellaneous",
"labubu": "miscellaneous",
"piracy": "miscellaneous",
"tryitandsee": "miscellaneous",
"rr": "miscellaneous",
"miscellaneous": "miscellaneous",
}
category_descriptions = {
@@ -170,7 +166,7 @@ class Help(commands.Cog, name="help"):
commands_in_category.append((app_command.name, description))
seen_names.add(app_command.name)
if hasattr(app_command, 'commands') and category in ["fun", "general", "idevice"]:
if hasattr(app_command, 'commands') and category in ["fun", "general", "idevice", "miscellaneous", "moderation"]:
for subcommand in app_command.commands:
if subcommand.name in seen_names:
continue

View File

@@ -0,0 +1,77 @@
import discord
from discord.ext import commands
from discord.ext.commands import Context
from .ban import ban_command
from .kick import kick_command
from .purge import purge_command
from .warnings import warnings_command
from .archive import archive_command
from .hackban import hackban_command
from .nick import nick_command
class Moderation(commands.GroupCog, name="moderation"):
def __init__(self, bot) -> None:
self.bot = bot
super().__init__()
@commands.hybrid_command(
name="ban",
description="Bans a user from the server."
)
async def ban(self, context, user: discord.User, *, reason: str = "Not specified", delete_messages: str = "none"):
return await ban_command()(self, context, user=user, reason=reason, delete_messages=delete_messages)
@commands.hybrid_command(
name="kick",
description="Kicks a user from the server."
)
async def kick(self, context, user: discord.User, *, reason: str = "Not specified"):
return await kick_command()(self, context, user=user, reason=reason)
@commands.hybrid_command(
name="purge",
description="Delete a number of messages."
)
async def purge(self, context, amount: int):
return await purge_command()(self, context, amount=amount)
@commands.hybrid_command(
name="warnings",
description="Manage warnings of a user on a server."
)
async def warnings(self, context):
return await warnings_command()(self, context)
@commands.hybrid_command(
name="archive",
description="Archives in a text file the last messages with a chosen limit of messages."
)
async def archive(self, context, limit: int = 10):
return await archive_command()(self, context, limit=limit)
@commands.hybrid_command(
name="hackban",
description="Bans a user without the user having to be in the server."
)
async def hackban(self, context, user_id: int, *, reason: str = "Not specified"):
return await hackban_command()(self, context, user_id=user_id, reason=reason)
@commands.hybrid_command(
name="nick",
description="Change the nickname of a user on a server."
)
async def nick(self, context, user: discord.User, *, nickname: str = None):
return await nick_command()(self, context, user=user, nickname=nickname)
async def setup(bot) -> None:
cog = Moderation(bot)
await bot.add_cog(cog)
bot.logger.info("Loaded extension 'moderation.ban'")
bot.logger.info("Loaded extension 'moderation.kick'")
bot.logger.info("Loaded extension 'moderation.purge'")
bot.logger.info("Loaded extension 'moderation.warnings'")
bot.logger.info("Loaded extension 'moderation.archive'")
bot.logger.info("Loaded extension 'moderation.hackban'")
bot.logger.info("Loaded extension 'moderation.nick'")

View File

@@ -6,10 +6,7 @@ from discord.ext import commands
from discord.ext.commands import Context
class Archive(commands.Cog, name="archive"):
def __init__(self, bot) -> None:
self.bot = bot
def archive_command():
@commands.hybrid_command(
name="archive",
description="Archives in a text file the last messages with a chosen limit of messages.",
@@ -18,7 +15,7 @@ class Archive(commands.Cog, name="archive"):
@app_commands.describe(
limit="The limit of messages that should be archived.",
)
async def archive(self, context: Context, limit: int = 10) -> None:
async def archive(self, context, limit: int = 10):
"""
Archives in a text file the last messages with a chosen limit of messages. This command requires the MESSAGE_CONTENT intent to work properly.
@@ -54,7 +51,5 @@ class Archive(commands.Cog, name="archive"):
f = discord.File(log_file)
await context.send(file=f)
os.remove(log_file)
async def setup(bot) -> None:
await bot.add_cog(Archive(bot))
return archive

View File

@@ -4,10 +4,7 @@ from discord.ext import commands
from discord.ext.commands import Context
class Ban(commands.Cog, name="ban"):
def __init__(self, bot) -> None:
self.bot = bot
def ban_command():
@commands.hybrid_command(
name="ban",
description="Bans a user from the server.",
@@ -27,8 +24,8 @@ class Ban(commands.Cog, name="ban"):
app_commands.Choice(name="Last 7 days", value="7d"),
])
async def ban(
self, context: Context, user: discord.User, *, reason: str = "Not specified", delete_messages: str = "none"
) -> None:
self, context, user: discord.User, *, reason: str = "Not specified", delete_messages: str = "none"
):
try:
member = context.guild.get_member(user.id)
if not member:
@@ -211,7 +208,5 @@ class Ban(commands.Cog, name="ban"):
"7d": "Last 7 days"
}
return time_formats.get(delete_option, "Unknown time period")
async def setup(bot) -> None:
await bot.add_cog(Ban(bot))
return ban

View File

@@ -4,10 +4,7 @@ from discord.ext import commands
from discord.ext.commands import Context
class HackBan(commands.Cog, name="hackban"):
def __init__(self, bot) -> None:
self.bot = bot
def hackban_command():
@commands.hybrid_command(
name="hackban",
description="Bans a user without the user having to be in the server.",
@@ -19,8 +16,8 @@ class HackBan(commands.Cog, name="hackban"):
reason="The reason why the user should be banned.",
)
async def hackban(
self, context: Context, user_id: str, *, reason: str = "Not specified"
) -> None:
self, context, user_id: str, *, reason: str = "Not specified"
):
"""
Bans a user without the user having to be in the server.
@@ -47,7 +44,5 @@ class HackBan(commands.Cog, name="hackban"):
color=0xE02B2B,
).set_author(name="Moderation", icon_url="https://yes.nighty.works/raw/CPKHQd.png")
await context.send(embed=embed)
async def setup(bot) -> None:
await bot.add_cog(HackBan(bot))
return hackban

View File

@@ -4,10 +4,7 @@ from discord.ext import commands
from discord.ext.commands import Context
class Kick(commands.Cog, name="kick"):
def __init__(self, bot) -> None:
self.bot = bot
def kick_command():
@commands.hybrid_command(
name="kick",
description="Kicks a user from the server.",
@@ -17,8 +14,8 @@ class Kick(commands.Cog, name="kick"):
reason="The reason why the user should be kicked.",
)
async def kick(
self, context: Context, user: discord.User, *, reason: str = "Not specified"
) -> None:
self, context, user: discord.User, *, reason: str = "Not specified"
):
try:
member = context.guild.get_member(user.id)
if not member:
@@ -122,7 +119,5 @@ class Kick(commands.Cog, name="kick"):
color=0xE02B2B,
).set_author(name="Moderation", icon_url="https://yes.nighty.works/raw/CPKHQd.png")
await context.send(embed=embed, ephemeral=True)
async def setup(bot) -> None:
await bot.add_cog(Kick(bot))
return kick

View File

@@ -4,10 +4,7 @@ from discord.ext import commands
from discord.ext.commands import Context
class Nick(commands.Cog, name="nick"):
def __init__(self, bot) -> None:
self.bot = bot
def nick_command():
@commands.hybrid_command(
name="nick",
description="Change the nickname of a user on a server.",
@@ -17,8 +14,8 @@ class Nick(commands.Cog, name="nick"):
nickname="The new nickname that should be set.",
)
async def nick(
self, context: Context, user: discord.User, *, nickname: str = None
) -> None:
self, context, user: discord.User, *, nickname: str = None
):
"""
Change the nickname of a user on a server.
@@ -60,7 +57,5 @@ class Nick(commands.Cog, name="nick"):
color=0xE02B2B,
).set_author(name="Moderation", icon_url="https://yes.nighty.works/raw/CPKHQd.png")
await context.send(embed=embed, ephemeral=True)
async def setup(bot) -> None:
await bot.add_cog(Nick(bot))
return nick

View File

@@ -4,10 +4,7 @@ from discord.ext import commands
from discord.ext.commands import Context
class Purge(commands.Cog, name="purge"):
def __init__(self, bot) -> None:
self.bot = bot
def purge_command():
@commands.hybrid_command(
name="purge",
description="Delete a number of messages.",
@@ -15,13 +12,7 @@ class Purge(commands.Cog, name="purge"):
@commands.has_guild_permissions(manage_messages=True)
@commands.bot_has_permissions(manage_messages=True)
@app_commands.describe(amount="The amount of messages that should be deleted.")
async def purge(self, context: Context, amount: int) -> None:
"""
Delete a number of messages.
:param context: The hybrid command context.
:param amount: The number of messages that should be deleted.
"""
async def purge(self, context, amount: int):
await context.send("Deleting messages...")
purged_messages = await context.channel.purge(limit=amount + 1)
embed = discord.Embed(
@@ -31,7 +22,5 @@ class Purge(commands.Cog, name="purge"):
)
embed.set_author(name="Moderation", icon_url="https://yes.nighty.works/raw/CPKHQd.png")
await context.channel.send(embed=embed)
async def setup(bot) -> None:
await bot.add_cog(Purge(bot))
return purge

View File

@@ -4,11 +4,8 @@ from discord.ext import commands
from discord.ext.commands import Context
class Warnings(commands.Cog, name="warnings"):
def __init__(self, bot) -> None:
self.bot = bot
async def send_embed(self, context: Context, embed: discord.Embed, *, ephemeral: bool = False) -> None:
def warnings_command():
async def send_embed(context, embed: discord.Embed, *, ephemeral: bool = False) -> None:
interaction = getattr(context, "interaction", None)
if interaction is not None:
if interaction.response.is_done():
@@ -22,7 +19,7 @@ class Warnings(commands.Cog, name="warnings"):
name="warning",
description="Manage warnings of a user on a server.",
)
async def warning(self, context: Context) -> None:
async def warning(self, context) -> None:
"""
Manage warnings of a user on a server.
@@ -170,5 +167,4 @@ class Warnings(commands.Cog, name="warnings"):
async def setup(bot) -> None:
await bot.add_cog(Warnings(bot))
return warning