feat: Require group prefix for hybrid commands

Added a _require_group_prefix check to all hybrid commands in each cog to ensure commands are only executed when invoked with the appropriate group prefix. Also updated error handling in bot.py to silently ignore CheckFailure errors. This improves command organization and prevents accidental command execution outside their intended context.
This commit is contained in:
neoarz
2025-09-29 09:59:56 -04:00
parent f8123b43a2
commit 3f4254dcdc
8 changed files with 154 additions and 0 deletions

View File

@@ -15,6 +15,62 @@ class Moderation(commands.GroupCog, name="moderation"):
self.bot = bot
super().__init__()
@commands.group(name="moderation", invoke_without_command=True)
async def moderation_group(self, context: Context):
embed = discord.Embed(
title="Moderation Commands",
description="Use `.moderation <subcommand>` or `/moderation <subcommand>`.",
color=0x7289DA
)
embed.add_field(name="Available", value="ban, kick, purge, warnings, archive, hackban, nick", inline=False)
await context.send(embed=embed)
async def _invoke_hybrid(self, context: Context, name: str, **kwargs):
command = self.bot.get_command(name)
if command is not None:
await context.invoke(command, **kwargs)
else:
await context.send(f"Unknown moderation command: {name}")
def _require_group_prefix(context: Context) -> bool:
if getattr(context, "interaction", None):
return True
group = getattr(getattr(context, "cog", None), "qualified_name", "").lower()
if not group:
return True
prefix = context.prefix or ""
content = context.message.content.strip().lower()
return content.startswith(f"{prefix}{group} ")
@moderation_group.command(name="ban")
async def moderation_group_ban(self, context: Context, user: discord.User, *, reason: str = "Not specified", delete_messages: str = "none"):
await self._invoke_hybrid(context, "ban", user=user, reason=reason, delete_messages=delete_messages)
@moderation_group.command(name="kick")
async def moderation_group_kick(self, context: Context, user: discord.User, *, reason: str = "Not specified"):
await self._invoke_hybrid(context, "kick", user=user, reason=reason)
@moderation_group.command(name="purge")
async def moderation_group_purge(self, context: Context, amount: int):
await self._invoke_hybrid(context, "purge", amount=amount)
@moderation_group.command(name="warnings")
async def moderation_group_warnings(self, context: Context):
await self._invoke_hybrid(context, "warnings")
@moderation_group.command(name="archive")
async def moderation_group_archive(self, context: Context, limit: int = 10):
await self._invoke_hybrid(context, "archive", limit=limit)
@moderation_group.command(name="hackban")
async def moderation_group_hackban(self, context: Context, user_id: int, *, reason: str = "Not specified"):
await self._invoke_hybrid(context, "hackban", user_id=user_id, reason=reason)
@moderation_group.command(name="nick")
async def moderation_group_nick(self, context: Context, user: discord.User, *, nickname: str = None):
await self._invoke_hybrid(context, "nick", user=user, nickname=nickname)
@commands.check(_require_group_prefix)
@commands.hybrid_command(
name="ban",
description="Bans a user from the server."
@@ -22,6 +78,7 @@ class Moderation(commands.GroupCog, name="moderation"):
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.check(_require_group_prefix)
@commands.hybrid_command(
name="kick",
description="Kicks a user from the server."
@@ -29,6 +86,7 @@ class Moderation(commands.GroupCog, name="moderation"):
async def kick(self, context, user: discord.User, *, reason: str = "Not specified"):
return await kick_command()(self, context, user=user, reason=reason)
@commands.check(_require_group_prefix)
@commands.hybrid_command(
name="purge",
description="Delete a number of messages."
@@ -36,6 +94,7 @@ class Moderation(commands.GroupCog, name="moderation"):
async def purge(self, context, amount: int):
return await purge_command()(self, context, amount=amount)
@commands.check(_require_group_prefix)
@commands.hybrid_command(
name="warnings",
description="Manage warnings of a user on a server."
@@ -43,6 +102,7 @@ class Moderation(commands.GroupCog, name="moderation"):
async def warnings(self, context):
return await warnings_command()(self, context)
@commands.check(_require_group_prefix)
@commands.hybrid_command(
name="archive",
description="Archives in a text file the last messages with a chosen limit of messages."
@@ -50,6 +110,7 @@ class Moderation(commands.GroupCog, name="moderation"):
async def archive(self, context, limit: int = 10):
return await archive_command()(self, context, limit=limit)
@commands.check(_require_group_prefix)
@commands.hybrid_command(
name="hackban",
description="Bans a user without the user having to be in the server."
@@ -57,6 +118,7 @@ class Moderation(commands.GroupCog, name="moderation"):
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.check(_require_group_prefix)
@commands.hybrid_command(
name="nick",
description="Change the nickname of a user on a server."