Skip to content

Commit

Permalink
Automatically delete automatic warning messages (#193)
Browse files Browse the repository at this point in the history
* 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 <erisa@erisa.uk>
  • Loading branch information
FloatingMilkshake and Erisa authored May 19, 2024
1 parent 09d7efc commit 89d8f10
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Commands/Debug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
4 changes: 4 additions & 0 deletions Helpers/WarningHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ public static async Task<UserWarning> 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()
Expand Down
1 change: 1 addition & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 3 additions & 0 deletions Structs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ public class ConfigJson

[JsonProperty("tqsMuteDurationHours")]
public int TqsMuteDurationHours { get; private set; }

[JsonProperty("autoWarnMsgAutoDeleteDays")]
public int AutoWarnMsgAutoDeleteDays { get; private set; }

}

Expand Down
32 changes: 32 additions & 0 deletions Tasks/PunishmentTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,38 @@ public static async Task<bool> CheckMutesAsync()
return success;
}
}

public static async Task<bool> CheckAutomaticWarningsAsync()
{
if (Program.cfgjson.AutoWarnMsgAutoDeleteDays == 0)
return false;

Dictionary<string, UserWarning> warnList = Program.db.HashGetAll("automaticWarnings").ToDictionary(
x => x.Name.ToString(),
x => JsonConvert.DeserializeObject<UserWarning>(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<string, UserWarning> 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;
}
}
}

}
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -320,5 +320,6 @@
"autoDeleteEmptyThreads": true,
"insiderCanaryThread": 1082394217168523315,
"tqsMutedRole": 752821045408563230,
"tqsMuteDurationHours": 2
"tqsMuteDurationHours": 2,
"autoWarnMsgAutoDeleteDays": 3
}

0 comments on commit 89d8f10

Please sign in to comment.