From 893cf11e84026f284101ce25d694edd147e20fa8 Mon Sep 17 00:00:00 2001 From: Kaveen Kumarasinghe Date: Mon, 9 Jan 2023 23:24:35 -0500 Subject: [PATCH] improve moderations service --- models/moderations_service_model.py | 87 +++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 18 deletions(-) diff --git a/models/moderations_service_model.py b/models/moderations_service_model.py index 7875e72..5d055fd 100644 --- a/models/moderations_service_model.py +++ b/models/moderations_service_model.py @@ -88,26 +88,32 @@ class Moderation: @staticmethod - def build_admin_moderated_message(moderated_message, response_message): + def build_admin_moderated_message(moderated_message, response_message, user_kicked=None, timed_out=None): + direct_message_object = isinstance(moderated_message, discord.Message) + moderated_message = moderated_message if direct_message_object else moderated_message.message # Create a discord embed to send to the user when their message gets moderated embed = discord.Embed( title="A message was moderated in the " - + moderated_message.message.guild.name + + moderated_message.guild.name + " server", - description=f"Message from {moderated_message.message.author.mention} was moderated: {moderated_message.message.content}", - colour=discord.Colour.yellow(), + description=f"Message from {moderated_message.author.mention} was moderated: {moderated_message.content}", + colour=discord.Colour.red(), ) # Get the link to the moderated message link = f"https://discord.com/channels/{response_message.guild.id}/{response_message.channel.id}/{response_message.id}" # set the link of the embed embed.add_field(name="Moderated message link", value=link, inline=False) + if user_kicked: + embed.add_field(name="User kicked by", value=user_kicked, inline=False) + if timed_out: + embed.add_field(name="User timed out by: ", value=timed_out, inline=False) return embed @staticmethod def determine_moderation_result(text, response): - warn_set = ThresholdSet(0.005,0.05,0.05,0.91,0.1,0.08,0.1) - delete_set = ThresholdSet(0.1,0.1,0.1,0.95,0.2,0.6,0.4) + warn_set = ThresholdSet(0.005,0.05,0.05,0.91,0.1,0.04,0.1) + delete_set = ThresholdSet(0.1,0.1,0.1,0.95,0.03,0.6,0.4) warn_result, flagged_warn = warn_set.moderate(text, response) delete_result, flagged_delete = delete_set.moderate(text, response) @@ -155,11 +161,13 @@ class Moderation: # Send to the moderation alert channel if moderations_alert_channel: - await moderations_alert_channel.send( + response_message = await moderations_alert_channel.send( embed=Moderation.build_admin_moderated_message( to_moderate, response_message ) ) + await response_message.edit(view=ModerationAdminView(to_moderate.message, response_message, True, True, True)) + elif moderation_result == ModerationResult.WARN: response_message = await moderations_alert_channel.send( embed=Moderation.build_admin_warning_message(to_moderate.message), @@ -177,23 +185,32 @@ class Moderation: class ModerationAdminView(discord.ui.View): - def __init__(self, message, moderation_message, nodelete=False): + def __init__(self, message, moderation_message, nodelete=False, deleted_message=False, source_deleted=False): super().__init__(timeout=None) # 1 hour interval to redo. + component_number = 0 self.message = message self.moderation_message = moderation_message, + self.add_item(TimeoutUserButton(self.message, self.moderation_message, component_number, 1, nodelete, source_deleted)) + component_number += 1 + self.add_item(TimeoutUserButton(self.message, self.moderation_message, component_number, 6, nodelete, source_deleted)) + component_number += 1 + self.add_item(TimeoutUserButton(self.message, self.moderation_message, component_number, 12, nodelete, source_deleted)) + component_number += 1 + self.add_item(TimeoutUserButton(self.message, self.moderation_message, component_number, 24, nodelete, source_deleted)) + component_number += 1 if not nodelete: - self.add_item(DeleteMessageButton(self.message, self.moderation_message)) - self.add_item(TimeoutUserButton(self.message, self.moderation_message, 1, nodelete)) - self.add_item(TimeoutUserButton(self.message, self.moderation_message, 6, nodelete)) - self.add_item(TimeoutUserButton(self.message, self.moderation_message, 12, nodelete)) - self.add_item(TimeoutUserButton(self.message, self.moderation_message, 24, nodelete)) + self.add_item(DeleteMessageButton(self.message, self.moderation_message, component_number)) + component_number += 1 + if deleted_message: + self.add_item(KickUserButton(self.message, self.moderation_message, component_number)) class DeleteMessageButton(discord.ui.Button["ModerationAdminView"]): - def __init__(self, message, moderation_message): + def __init__(self, message, moderation_message, current_num): super().__init__(style=discord.ButtonStyle.danger, label="Delete Message") self.message = message self.moderation_message = moderation_message + self.current_num = current_num async def callback(self, interaction: discord.Interaction): @@ -202,17 +219,45 @@ class DeleteMessageButton(discord.ui.Button["ModerationAdminView"]): await interaction.response.send_message( "This message was deleted", ephemeral=True, delete_after=10 ) - await self.moderation_message[0].edit(embed=Moderation.build_admin_warning_message(self.message, deleted_message=interaction.user.mention), + while isinstance(self.moderation_message, tuple): + self.moderation_message = self.moderation_message[0] + await self.moderation_message.edit(embed=Moderation.build_admin_warning_message(self.message, deleted_message=interaction.user.mention), view=ModerationAdminView(self.message, self.moderation_message, nodelete=True)) +class KickUserButton(discord.ui.Button["ModerationAdminView"]): + def __init__(self, message, moderation_message, current_num ): + super().__init__(style=discord.ButtonStyle.danger, label="Kick User") + self.message = message + self.moderation_message = moderation_message + self.current_num = current_num + + async def callback(self, interaction: discord.Interaction): + # Get the user and kick the user + try: + await self.message.author.kick(reason="You broke the server rules. Please rejoin and review the rules.") + except: + pass + await interaction.response.send_message( + "This user was attempted to be kicked", ephemeral=True, delete_after=10 + ) + + while isinstance(self.moderation_message, tuple): + self.moderation_message = self.moderation_message[0] + await self.moderation_message.edit(embed=Moderation.build_admin_moderated_message(self.message, self.moderation_message, user_kicked=interaction.user.mention) + , view=ModerationAdminView(self.message, self.moderation_message, nodelete=True, deleted_message=False)) + + + class TimeoutUserButton(discord.ui.Button["ModerationAdminView"]): - def __init__(self, message, moderation_message, hours, nodelete): + def __init__(self, message, moderation_message,current_num, hours, nodelete, source_deleted): super().__init__(style=discord.ButtonStyle.danger, label=f"Timeout {hours}h") self.message = message self.moderation_message = moderation_message self.hours = hours self.nodelete = nodelete + self.current_num = current_num + self.source_deleted = source_deleted async def callback(self, interaction: discord.Interaction): # Get the user id @@ -230,7 +275,13 @@ class TimeoutUserButton(discord.ui.Button["ModerationAdminView"]): await interaction.response.send_message( f"This user was timed out for {self.hours} hour(s)", ephemeral=True, delete_after=10 ) - moderation_message = self.moderation_message[0][0] if self.nodelete else self.moderation_message[0] - await moderation_message.edit(embed=Moderation.build_admin_warning_message(self.message, deleted_message=interaction.user.mention, timed_out=interaction.user.mention), + while isinstance(self.moderation_message, tuple): + self.moderation_message = self.moderation_message[0] + + if not self.source_deleted: + await self.moderation_message.edit(embed=Moderation.build_admin_warning_message(self.message, deleted_message=interaction.user.mention, timed_out=interaction.user.mention), view=ModerationAdminView(self.message, self.moderation_message, nodelete=True)) + else: + await self.moderation_message.edit(embed=Moderation.build_admin_moderated_message(self.message, self.moderation_message, timed_out=interaction.user.mention), + view=ModerationAdminView(self.message, self.moderation_message, nodelete=True, deleted_message=True, source_deleted=True))