From 4401b0419e465c494bd801e118af383e7dc57700 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Fri, 15 Dec 2023 13:54:58 -0500 Subject: [PATCH] feat(moderation): added check_moddable --- moderation/moderation.py | 129 ++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 77 deletions(-) diff --git a/moderation/moderation.py b/moderation/moderation.py index 8364e17..1148a50 100644 --- a/moderation/moderation.py +++ b/moderation/moderation.py @@ -61,6 +61,7 @@ class Moderation(commands.Cog): mysql_password = " " ) self.config.register_guild( + use_discord_permissions = True, ignore_other_bots = True, dm_users = True, log_channel = " ", @@ -275,6 +276,43 @@ class Moderation(commands.Cog): return False + async def check_moddable(self, target: Union[discord.User, discord.Member], interaction: discord.Interaction, permissions: list): + """Checks if a moderator can moderate a target.""" + if self.check_permissions(interaction.client.user, permissions, guild=interaction.guild): + await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True) + return False + + if self.config.guild(interaction.guild).use_discord_permissions() is True: + if self.check_permissions(interaction.user, permissions, guild=interaction.guild): + await interaction.response.send_message(f"You do not have the `{permissions}` permission, required for this action.", ephemeral=True) + return False + + if interaction.user.id == target.id: + await interaction.response.send_message(content="You cannot moderate yourself!", ephemeral=True) + return False + + if target.bot: + await interaction.response.send_message(content="You cannot moderate bots!", ephemeral=True) + return False + + if isinstance(target, discord.Member): + if interaction.user.top_role <= target.top_role: + await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True) + return False + + if interaction.guild.get_member(interaction.client.user.id).top_role <= target.top_role: + await interaction.response.send_message(content="You cannot moderate members with a role higher than the bot!", ephemeral=True) + return False + + immune_roles = self.config.guild(target.guild).immune_roles() + + for role in target.roles: + if role.id in immune_roles: + await interaction.response.send_message(content="You cannot moderate members with an immune role!", ephemeral=True) + return False + + return True + async def mysql_log(self, guild_id: str, author_id: str, moderation_type: str, target_id: int, role_id: int, duration, reason: str, database: mysql.connector.MySQLConnection = None): timestamp = int(time.time()) @@ -555,14 +593,8 @@ class Moderation(commands.Cog): Why are you noting this user? silent: bool Should the user be messaged?""" - if interaction.guild.get_member(target.id): - target_member = interaction.guild.get_member(target.id) - if interaction.guild.get_member(interaction.client.user.id).top_role <= target_member.top_role: - await interaction.response.send_message(content="You cannot moderate members with a role higher than the bot!", ephemeral=True) - return - if interaction.user.top_role <= target_member.top_role: - await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True) - return + if not await self.check_moddable(target, interaction, ['moderate_members']): + return await interaction.response.send_message(content=f"{target.mention} has recieved a note!\n**Reason** - `{reason}`") @@ -591,14 +623,8 @@ class Moderation(commands.Cog): Why are you warning this user? silent: bool Should the user be messaged?""" - if interaction.guild.get_member(target.id): - target_member = interaction.guild.get_member(target.id) - if interaction.guild.get_member(interaction.client.user.id).top_role <= target_member.top_role: - await interaction.response.send_message(content="You cannot moderate members with a role higher than the bot!", ephemeral=True) - return - if interaction.user.top_role <= target_member.top_role: - await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True) - return + if not await self.check_moddable(target, interaction, ['moderate_members']): + return await interaction.response.send_message(content=f"{target.mention} has been warned!\n**Reason** - `{reason}`") @@ -629,19 +655,9 @@ class Moderation(commands.Cog): Why are you unbanning this user? silent: bool Should the user be messaged?""" - if interaction.guild.get_member(target.id): - target_member = interaction.guild.get_member(target.id) - if interaction.guild.get_member(interaction.client.user.id).top_role <= target_member.top_role: - await interaction.response.send_message(content="You cannot moderate members with a role higher than the bot!", ephemeral=True) - return - if interaction.user.top_role <= target_member.top_role: - await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True) - return - - permissions = self.check_permissions(interaction.client.user, ['moderate_members'], interaction) - if permissions: - await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True) + if not await self.check_moddable(target, interaction, ['moderate_members']): return + if target.is_timed_out() is True: await interaction.response.send_message(f"{target.mention} is already muted!", allowed_mentions=discord.AllowedMentions(users=False), ephemeral=True) return @@ -685,22 +701,13 @@ class Moderation(commands.Cog): Why are you unmuting this user? silent: bool Should the user be messaged?""" - if interaction.guild.get_member(target.id): - target_member = interaction.guild.get_member(target.id) - if interaction.guild.get_member(interaction.client.user.id).top_role <= target_member.top_role: - await interaction.response.send_message(content="You cannot moderate members with a role higher than the bot!", ephemeral=True) - return - if interaction.user.top_role <= target_member.top_role: - await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True) - return - - permissions = self.check_permissions(interaction.client.user, ['moderate_members'], interaction) - if permissions: - await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True) + if not await self.check_moddable(target, interaction, ['moderate_members']): return + if target.is_timed_out() is False: await interaction.response.send_message(f"{target.mention} is not muted!", allowed_mentions=discord.AllowedMentions(users=False), ephemeral=True) return + if reason: await target.timeout(None, reason=f"Unmuted by {interaction.user.id} for: {reason}") else: @@ -734,18 +741,7 @@ class Moderation(commands.Cog): Why are you kicking this user? silent: bool Should the user be messaged?""" - if interaction.guild.get_member(target.id): - target_member = interaction.guild.get_member(target.id) - if interaction.guild.get_member(interaction.client.user.id).top_role <= target_member.top_role: - await interaction.response.send_message(content="You cannot moderate members with a role higher than the bot!", ephemeral=True) - return - if interaction.user.top_role <= target_member.top_role: - await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True) - return - - permissions = self.check_permissions(interaction.client.user, ['kick_members'], interaction) - if permissions: - await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True) + if not await self.check_moddable(target, interaction, ['kick_members']): return await interaction.response.send_message(content=f"{target.mention} has been kicked!\n**Reason** - `{reason}`") @@ -789,20 +785,9 @@ class Moderation(commands.Cog): How many days of messages to delete? silent: bool Should the user be messaged?""" - if interaction.guild.get_member(target.id): - target_member = interaction.guild.get_member(target.id) - if interaction.guild.get_member(interaction.client.user.id).top_role <= target_member.top_role: - await interaction.response.send_message(content="You cannot moderate members with a role higher than the bot!", ephemeral=True) - return - if interaction.user.top_role <= target_member.top_role: - await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True) - return - - permissions = self.check_permissions(interaction.client.user, ['ban_members'], interaction) - - if permissions: - await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True) + if not await self.check_moddable(target, interaction, ['ban_members']): return + try: await interaction.guild.fetch_ban(target) await interaction.response.send_message(content=f"{target.mention} is already banned!", ephemeral=True) @@ -860,19 +845,9 @@ class Moderation(commands.Cog): Why are you unbanning this user? silent: bool Should the user be messaged?""" - if interaction.guild.get_member(target.id): - target_member = interaction.guild.get_member(target.id) - if interaction.guild.get_member(interaction.client.user.id).top_role <= target_member.top_role: - await interaction.response.send_message(content="You cannot moderate members with a role higher than the bot!", ephemeral=True) - return - if interaction.user.top_role <= target_member.top_role: - await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True) - return - - permissions = self.check_permissions(interaction.client.user, ['ban_members'], interaction) - if permissions: - await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True) + if not await self.check_moddable(target, interaction, ['ban_members']): return + try: await interaction.guild.fetch_ban(target) except discord.errors.NotFound: