From 89d8f100d5a4b4258521328f78f71fa45b8e044d Mon Sep 17 00:00:00 2001 From: Milkshake Date: Sun, 19 May 2024 11:13:40 -0400 Subject: [PATCH] Automatically delete automatic warning messages (#193) * Automatically delete automatic warning messages * Skip auto-warn auto-delete check if set to 0 days * Apply suggestions from code review --------- Co-authored-by: Erisa A --- Commands/Debug.cs | 3 ++- Helpers/WarningHelpers.cs | 4 ++++ Program.cs | 1 + Structs.cs | 3 +++ Tasks/PunishmentTasks.cs | 32 ++++++++++++++++++++++++++++++++ config.json | 3 ++- 6 files changed, 44 insertions(+), 2 deletions(-) diff --git a/Commands/Debug.cs b/Commands/Debug.cs index c7d6af0d..202f4fa9 100644 --- a/Commands/Debug.cs +++ b/Commands/Debug.cs @@ -161,11 +161,12 @@ public async Task Refresh(CommandContext ctx) var msg = await ctx.RespondAsync("Checking for pending scheduled tasks..."); bool bans = await Tasks.PunishmentTasks.CheckBansAsync(); bool mutes = await Tasks.PunishmentTasks.CheckMutesAsync(); + bool warns = await Tasks.PunishmentTasks.CheckAutomaticWarningsAsync(); bool reminders = await Tasks.ReminderTasks.CheckRemindersAsync(); bool raidmode = await Tasks.RaidmodeTasks.CheckRaidmodeAsync(ctx.Guild.Id); bool unlocks = await Tasks.LockdownTasks.CheckUnlocksAsync(); - await msg.ModifyAsync($"Unban check result: `{bans}`\nUnmute check result: `{mutes}`\nReminders check result: `{reminders}`\nRaidmode check result: `{raidmode}`\nUnlocks check result: `{unlocks}`"); + await msg.ModifyAsync($"Unban check result: `{bans}`\nUnmute check result: `{mutes}`\nAutomatic warning message check result: `{warns}`\nReminders check result: `{reminders}`\nRaidmode check result: `{raidmode}`\nUnlocks check result: `{unlocks}`"); } [Command("sh")] diff --git a/Helpers/WarningHelpers.cs b/Helpers/WarningHelpers.cs index 004a7656..37eb6777 100644 --- a/Helpers/WarningHelpers.cs +++ b/Helpers/WarningHelpers.cs @@ -220,6 +220,10 @@ public static async Task GiveWarningAsync(DiscordUser targetUser, D }; Program.db.HashSet(targetUser.Id.ToString(), warning.WarningId, JsonConvert.SerializeObject(warning)); + + // If warning is automatic (if responsible moderator is a bot), add to list so the context message can be more-easily deleted later + if (modUser.IsBot) + Program.db.HashSet("automaticWarnings", warningId, JsonConvert.SerializeObject(warning)); LogChannelHelper.LogMessageAsync("mod", new DiscordMessageBuilder() diff --git a/Program.cs b/Program.cs index a7125ffd..e15617fb 100644 --- a/Program.cs +++ b/Program.cs @@ -185,6 +185,7 @@ static async Task Main(string[] _) [ Tasks.PunishmentTasks.CheckMutesAsync(), Tasks.PunishmentTasks.CheckBansAsync(), + Tasks.PunishmentTasks.CheckAutomaticWarningsAsync(), Tasks.ReminderTasks.CheckRemindersAsync(), Tasks.RaidmodeTasks.CheckRaidmodeAsync(cfgjson.ServerID), Tasks.LockdownTasks.CheckUnlocksAsync(), diff --git a/Structs.cs b/Structs.cs index c624c318..7e10f411 100644 --- a/Structs.cs +++ b/Structs.cs @@ -292,6 +292,9 @@ public class ConfigJson [JsonProperty("tqsMuteDurationHours")] public int TqsMuteDurationHours { get; private set; } + + [JsonProperty("autoWarnMsgAutoDeleteDays")] + public int AutoWarnMsgAutoDeleteDays { get; private set; } } diff --git a/Tasks/PunishmentTasks.cs b/Tasks/PunishmentTasks.cs index 6304eb57..62e98ce9 100644 --- a/Tasks/PunishmentTasks.cs +++ b/Tasks/PunishmentTasks.cs @@ -61,6 +61,38 @@ public static async Task CheckMutesAsync() return success; } } + + public static async Task CheckAutomaticWarningsAsync() + { + if (Program.cfgjson.AutoWarnMsgAutoDeleteDays == 0) + return false; + + Dictionary warnList = Program.db.HashGetAll("automaticWarnings").ToDictionary( + x => x.Name.ToString(), + x => JsonConvert.DeserializeObject(x.Value) + ); + + if (warnList is null | warnList.Keys.Count == 0) + return false; + else + { + // The success value will be changed later if any of the message deletes are successful. + bool success = false; + foreach (KeyValuePair entry in warnList) + { + UserWarning warn = entry.Value; + if (DateTime.Now > warn.WarnTimestamp.AddDays(Program.cfgjson.AutoWarnMsgAutoDeleteDays)) + { + var contextMessage = await DiscordHelpers.GetMessageFromReferenceAsync(warn.ContextMessageReference); + await contextMessage.DeleteAsync(); + Program.db.HashDelete("automaticWarnings", warn.WarningId); + success = true; + } + } + Program.discord.Logger.LogDebug(Program.CliptokEventID, "Checked automatic warnings at {time} with result: {result}", DateTime.Now, success); + return success; + } + } } } diff --git a/config.json b/config.json index 63bb109d..25f1c224 100644 --- a/config.json +++ b/config.json @@ -320,5 +320,6 @@ "autoDeleteEmptyThreads": true, "insiderCanaryThread": 1082394217168523315, "tqsMutedRole": 752821045408563230, - "tqsMuteDurationHours": 2 + "tqsMuteDurationHours": 2, + "autoWarnMsgAutoDeleteDays": 3 }