feat(moderation): added ability to edit durations

This commit is contained in:
Seaswimmer 2023-12-16 16:59:48 -05:00
parent 5b8f6823ef
commit e9e2c20a58
Signed by: cswimr
GPG key ID: 1EBC234EEDA901AE

View file

@ -1203,7 +1203,7 @@ class Moderation(commands.Cog):
await interaction.response.send_message(content=f"No case with case number `{case}` found.", ephemeral=True)
@app_commands.command(name="edit")
async def edit(self, interaction: discord.Interaction, case: int, reason: str):
async def edit(self, interaction: discord.Interaction, case: int, reason: str, duration: str = None):
"""Edit the reason of a specific case.
Parameters
@ -1211,19 +1211,43 @@ class Moderation(commands.Cog):
case: int
What case are you editing?
reason: str
What is the new reason?"""
What is the new reason?
duration: str
What is the new duration? Does not reapply the moderation if it has already expired."""
permissions = self.check_permissions(interaction.client.user, ['embed_links'], interaction)
if permissions:
await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True)
return
if case != 0:
parsed_time = None
case_dict = await self.fetch_case(case, interaction.guild.id)
if case_dict:
conf = await self.check_conf(['mysql_database'])
if conf:
raise(LookupError)
if duration:
try:
parsed_time = parse(sval=duration, as_timedelta=True, raise_exception=True)
except ValueError:
await interaction.response.send_message("Please provide a valid duration!", ephemeral=True)
return
end_timestamp = case_dict['timestamp'] + parsed_time.total_seconds()
if case_dict['type'] == 'MUTE':
if (time.time() - case_dict['timestamp']) + parsed_time.total_seconds() > 2419200:
await interaction.response.send_message("Please provide a duration that is less than 28 days from the initial moderation.")
return
try:
member = await interaction.guild.fetch_member(case_dict['target_id'])
await member.timeout(parsed_time, reason=f"Case #{case} edited by {interaction.user.id}")
except discord.NotFound:
pass
changes: list = case_dict['changes']
if len(changes) > 25:
await interaction.response.send_message(content="Due to limitations with Discord's embed system, you cannot edit a case more than 25 times.", ephemeral=True)
@ -1234,24 +1258,44 @@ class Moderation(commands.Cog):
'type': "ORIGINAL",
'timestamp': case_dict['timestamp'],
'reason': case_dict['reason'],
'user_id': case_dict['moderator_id']
'user_id': case_dict['moderator_id'],
'duration': case_dict['duration'],
'end_timestamp': case_dict['end_timestamp']
}
)
if parsed_time:
changes.append(
{
'type': "EDIT",
'timestamp': int(time.time()),
'reason': reason,
'user_id': interaction.user.id,
'duration': parsed_time,
'end_timestamp': end_timestamp
}
)
else:
changes.append(
{
'type': "EDIT",
'timestamp': int(time.time()),
'reason': reason,
'user_id': interaction.user.id,
'duration': case_dict['duration'],
'end_timestamp': case_dict['end_timestamp']
}
)
changes.append(
{
'type': "EDIT",
'timestamp': int(time.time()),
'reason': reason,
'user_id': interaction.user.id
}
)
database = await self.connect()
cursor = database.cursor()
db = await self.config.mysql_database()
update_query = f"UPDATE `{db}`.`moderation_{interaction.guild.id}` SET changes = %s, reason = %s WHERE moderation_id = %s"
cursor.execute(update_query, (json.dumps(changes), reason, case))
if parsed_time:
update_query = f"UPDATE `{db}`.`moderation_{interaction.guild.id}` SET changes = %s, reason = %s, duration = %s, end_timestamp = %s WHERE moderation_id = %s"
cursor.execute(update_query, (json.dumps(changes), reason, parsed_time, end_timestamp, case))
else:
update_query = f"UPDATE `{db}`.`moderation_{interaction.guild.id}` SET changes = %s, reason = %s WHERE moderation_id = %s"
cursor.execute(update_query, (json.dumps(changes), reason, case))
database.commit()
new_case = await self.fetch_case(case, interaction.guild.id)