mirror of
https://github.com/neoarz/Syntrel.git
synced 2025-12-25 03:40:11 +01:00
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:
2
bot.py
2
bot.py
@@ -90,7 +90,7 @@ class DiscordBot(commands.Bot):
|
|||||||
if os.path.exists(init_file):
|
if os.path.exists(init_file):
|
||||||
try:
|
try:
|
||||||
await self.load_extension(f"cogs.{folder}")
|
await self.load_extension(f"cogs.{folder}")
|
||||||
if folder not in ["fun", "general", "idevice"]:
|
if folder not in ["fun", "general", "idevice", "miscellaneous", "moderation"]:
|
||||||
self.logger.info(f"Loaded extension '{folder}'")
|
self.logger.info(f"Loaded extension '{folder}'")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
exception = f"{type(e).__name__}: {e}"
|
exception = f"{type(e).__name__}: {e}"
|
||||||
|
|||||||
@@ -79,11 +79,7 @@ class Help(commands.Cog, name="help"):
|
|||||||
"translate": "utilities",
|
"translate": "utilities",
|
||||||
|
|
||||||
# Miscellaneous Commands
|
# Miscellaneous Commands
|
||||||
"keanu": "miscellaneous",
|
"miscellaneous": "miscellaneous",
|
||||||
"labubu": "miscellaneous",
|
|
||||||
"piracy": "miscellaneous",
|
|
||||||
"tryitandsee": "miscellaneous",
|
|
||||||
"rr": "miscellaneous",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
category_descriptions = {
|
category_descriptions = {
|
||||||
@@ -170,7 +166,7 @@ class Help(commands.Cog, name="help"):
|
|||||||
commands_in_category.append((app_command.name, description))
|
commands_in_category.append((app_command.name, description))
|
||||||
seen_names.add(app_command.name)
|
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:
|
for subcommand in app_command.commands:
|
||||||
if subcommand.name in seen_names:
|
if subcommand.name in seen_names:
|
||||||
continue
|
continue
|
||||||
|
|||||||
77
cogs/moderation/__init__.py
Normal file
77
cogs/moderation/__init__.py
Normal 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'")
|
||||||
@@ -6,10 +6,7 @@ from discord.ext import commands
|
|||||||
from discord.ext.commands import Context
|
from discord.ext.commands import Context
|
||||||
|
|
||||||
|
|
||||||
class Archive(commands.Cog, name="archive"):
|
def archive_command():
|
||||||
def __init__(self, bot) -> None:
|
|
||||||
self.bot = bot
|
|
||||||
|
|
||||||
@commands.hybrid_command(
|
@commands.hybrid_command(
|
||||||
name="archive",
|
name="archive",
|
||||||
description="Archives in a text file the last messages with a chosen limit of messages.",
|
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(
|
@app_commands.describe(
|
||||||
limit="The limit of messages that should be archived.",
|
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.
|
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)
|
f = discord.File(log_file)
|
||||||
await context.send(file=f)
|
await context.send(file=f)
|
||||||
os.remove(log_file)
|
os.remove(log_file)
|
||||||
|
|
||||||
|
return archive
|
||||||
async def setup(bot) -> None:
|
|
||||||
await bot.add_cog(Archive(bot))
|
|
||||||
@@ -4,10 +4,7 @@ from discord.ext import commands
|
|||||||
from discord.ext.commands import Context
|
from discord.ext.commands import Context
|
||||||
|
|
||||||
|
|
||||||
class Ban(commands.Cog, name="ban"):
|
def ban_command():
|
||||||
def __init__(self, bot) -> None:
|
|
||||||
self.bot = bot
|
|
||||||
|
|
||||||
@commands.hybrid_command(
|
@commands.hybrid_command(
|
||||||
name="ban",
|
name="ban",
|
||||||
description="Bans a user from the server.",
|
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"),
|
app_commands.Choice(name="Last 7 days", value="7d"),
|
||||||
])
|
])
|
||||||
async def ban(
|
async def ban(
|
||||||
self, context: Context, user: discord.User, *, reason: str = "Not specified", delete_messages: str = "none"
|
self, context, user: discord.User, *, reason: str = "Not specified", delete_messages: str = "none"
|
||||||
) -> None:
|
):
|
||||||
try:
|
try:
|
||||||
member = context.guild.get_member(user.id)
|
member = context.guild.get_member(user.id)
|
||||||
if not member:
|
if not member:
|
||||||
@@ -211,7 +208,5 @@ class Ban(commands.Cog, name="ban"):
|
|||||||
"7d": "Last 7 days"
|
"7d": "Last 7 days"
|
||||||
}
|
}
|
||||||
return time_formats.get(delete_option, "Unknown time period")
|
return time_formats.get(delete_option, "Unknown time period")
|
||||||
|
|
||||||
|
return ban
|
||||||
async def setup(bot) -> None:
|
|
||||||
await bot.add_cog(Ban(bot))
|
|
||||||
@@ -4,10 +4,7 @@ from discord.ext import commands
|
|||||||
from discord.ext.commands import Context
|
from discord.ext.commands import Context
|
||||||
|
|
||||||
|
|
||||||
class HackBan(commands.Cog, name="hackban"):
|
def hackban_command():
|
||||||
def __init__(self, bot) -> None:
|
|
||||||
self.bot = bot
|
|
||||||
|
|
||||||
@commands.hybrid_command(
|
@commands.hybrid_command(
|
||||||
name="hackban",
|
name="hackban",
|
||||||
description="Bans a user without the user having to be in the server.",
|
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.",
|
reason="The reason why the user should be banned.",
|
||||||
)
|
)
|
||||||
async def hackban(
|
async def hackban(
|
||||||
self, context: Context, user_id: str, *, reason: str = "Not specified"
|
self, context, user_id: str, *, reason: str = "Not specified"
|
||||||
) -> None:
|
):
|
||||||
"""
|
"""
|
||||||
Bans a user without the user having to be in the server.
|
Bans a user without the user having to be in the server.
|
||||||
|
|
||||||
@@ -47,7 +44,5 @@ class HackBan(commands.Cog, name="hackban"):
|
|||||||
color=0xE02B2B,
|
color=0xE02B2B,
|
||||||
).set_author(name="Moderation", icon_url="https://yes.nighty.works/raw/CPKHQd.png")
|
).set_author(name="Moderation", icon_url="https://yes.nighty.works/raw/CPKHQd.png")
|
||||||
await context.send(embed=embed)
|
await context.send(embed=embed)
|
||||||
|
|
||||||
|
return hackban
|
||||||
async def setup(bot) -> None:
|
|
||||||
await bot.add_cog(HackBan(bot))
|
|
||||||
|
|||||||
@@ -4,10 +4,7 @@ from discord.ext import commands
|
|||||||
from discord.ext.commands import Context
|
from discord.ext.commands import Context
|
||||||
|
|
||||||
|
|
||||||
class Kick(commands.Cog, name="kick"):
|
def kick_command():
|
||||||
def __init__(self, bot) -> None:
|
|
||||||
self.bot = bot
|
|
||||||
|
|
||||||
@commands.hybrid_command(
|
@commands.hybrid_command(
|
||||||
name="kick",
|
name="kick",
|
||||||
description="Kicks a user from the server.",
|
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.",
|
reason="The reason why the user should be kicked.",
|
||||||
)
|
)
|
||||||
async def kick(
|
async def kick(
|
||||||
self, context: Context, user: discord.User, *, reason: str = "Not specified"
|
self, context, user: discord.User, *, reason: str = "Not specified"
|
||||||
) -> None:
|
):
|
||||||
try:
|
try:
|
||||||
member = context.guild.get_member(user.id)
|
member = context.guild.get_member(user.id)
|
||||||
if not member:
|
if not member:
|
||||||
@@ -122,7 +119,5 @@ class Kick(commands.Cog, name="kick"):
|
|||||||
color=0xE02B2B,
|
color=0xE02B2B,
|
||||||
).set_author(name="Moderation", icon_url="https://yes.nighty.works/raw/CPKHQd.png")
|
).set_author(name="Moderation", icon_url="https://yes.nighty.works/raw/CPKHQd.png")
|
||||||
await context.send(embed=embed, ephemeral=True)
|
await context.send(embed=embed, ephemeral=True)
|
||||||
|
|
||||||
|
return kick
|
||||||
async def setup(bot) -> None:
|
|
||||||
await bot.add_cog(Kick(bot))
|
|
||||||
|
|||||||
@@ -4,10 +4,7 @@ from discord.ext import commands
|
|||||||
from discord.ext.commands import Context
|
from discord.ext.commands import Context
|
||||||
|
|
||||||
|
|
||||||
class Nick(commands.Cog, name="nick"):
|
def nick_command():
|
||||||
def __init__(self, bot) -> None:
|
|
||||||
self.bot = bot
|
|
||||||
|
|
||||||
@commands.hybrid_command(
|
@commands.hybrid_command(
|
||||||
name="nick",
|
name="nick",
|
||||||
description="Change the nickname of a user on a server.",
|
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.",
|
nickname="The new nickname that should be set.",
|
||||||
)
|
)
|
||||||
async def nick(
|
async def nick(
|
||||||
self, context: Context, user: discord.User, *, nickname: str = None
|
self, context, user: discord.User, *, nickname: str = None
|
||||||
) -> None:
|
):
|
||||||
"""
|
"""
|
||||||
Change the nickname of a user on a server.
|
Change the nickname of a user on a server.
|
||||||
|
|
||||||
@@ -60,7 +57,5 @@ class Nick(commands.Cog, name="nick"):
|
|||||||
color=0xE02B2B,
|
color=0xE02B2B,
|
||||||
).set_author(name="Moderation", icon_url="https://yes.nighty.works/raw/CPKHQd.png")
|
).set_author(name="Moderation", icon_url="https://yes.nighty.works/raw/CPKHQd.png")
|
||||||
await context.send(embed=embed, ephemeral=True)
|
await context.send(embed=embed, ephemeral=True)
|
||||||
|
|
||||||
|
return nick
|
||||||
async def setup(bot) -> None:
|
|
||||||
await bot.add_cog(Nick(bot))
|
|
||||||
|
|||||||
@@ -4,10 +4,7 @@ from discord.ext import commands
|
|||||||
from discord.ext.commands import Context
|
from discord.ext.commands import Context
|
||||||
|
|
||||||
|
|
||||||
class Purge(commands.Cog, name="purge"):
|
def purge_command():
|
||||||
def __init__(self, bot) -> None:
|
|
||||||
self.bot = bot
|
|
||||||
|
|
||||||
@commands.hybrid_command(
|
@commands.hybrid_command(
|
||||||
name="purge",
|
name="purge",
|
||||||
description="Delete a number of messages.",
|
description="Delete a number of messages.",
|
||||||
@@ -15,13 +12,7 @@ class Purge(commands.Cog, name="purge"):
|
|||||||
@commands.has_guild_permissions(manage_messages=True)
|
@commands.has_guild_permissions(manage_messages=True)
|
||||||
@commands.bot_has_permissions(manage_messages=True)
|
@commands.bot_has_permissions(manage_messages=True)
|
||||||
@app_commands.describe(amount="The amount of messages that should be deleted.")
|
@app_commands.describe(amount="The amount of messages that should be deleted.")
|
||||||
async def purge(self, context: Context, amount: int) -> None:
|
async def purge(self, context, amount: int):
|
||||||
"""
|
|
||||||
Delete a number of messages.
|
|
||||||
|
|
||||||
:param context: The hybrid command context.
|
|
||||||
:param amount: The number of messages that should be deleted.
|
|
||||||
"""
|
|
||||||
await context.send("Deleting messages...")
|
await context.send("Deleting messages...")
|
||||||
purged_messages = await context.channel.purge(limit=amount + 1)
|
purged_messages = await context.channel.purge(limit=amount + 1)
|
||||||
embed = discord.Embed(
|
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")
|
embed.set_author(name="Moderation", icon_url="https://yes.nighty.works/raw/CPKHQd.png")
|
||||||
await context.channel.send(embed=embed)
|
await context.channel.send(embed=embed)
|
||||||
|
|
||||||
|
return purge
|
||||||
async def setup(bot) -> None:
|
|
||||||
await bot.add_cog(Purge(bot))
|
|
||||||
|
|||||||
@@ -4,11 +4,8 @@ from discord.ext import commands
|
|||||||
from discord.ext.commands import Context
|
from discord.ext.commands import Context
|
||||||
|
|
||||||
|
|
||||||
class Warnings(commands.Cog, name="warnings"):
|
def warnings_command():
|
||||||
def __init__(self, bot) -> None:
|
async def send_embed(context, embed: discord.Embed, *, ephemeral: bool = False) -> None:
|
||||||
self.bot = bot
|
|
||||||
|
|
||||||
async def send_embed(self, context: Context, embed: discord.Embed, *, ephemeral: bool = False) -> None:
|
|
||||||
interaction = getattr(context, "interaction", None)
|
interaction = getattr(context, "interaction", None)
|
||||||
if interaction is not None:
|
if interaction is not None:
|
||||||
if interaction.response.is_done():
|
if interaction.response.is_done():
|
||||||
@@ -22,7 +19,7 @@ class Warnings(commands.Cog, name="warnings"):
|
|||||||
name="warning",
|
name="warning",
|
||||||
description="Manage warnings of a user on a server.",
|
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.
|
Manage warnings of a user on a server.
|
||||||
|
|
||||||
@@ -170,5 +167,4 @@ class Warnings(commands.Cog, name="warnings"):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
async def setup(bot) -> None:
|
return warning
|
||||||
await bot.add_cog(Warnings(bot))
|
|
||||||
|
|||||||
Reference in New Issue
Block a user