Skip to content

Commit

Permalink
Combine tasks for auto-warn messages & compromised account ban messages
Browse files Browse the repository at this point in the history
  • Loading branch information
FloatingMilkshake committed Dec 23, 2024
1 parent 42404b0 commit 6cc9b66
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 41 deletions.
4 changes: 2 additions & 2 deletions Commands/Debug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ 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 punishmentMessages = await Tasks.PunishmentTasks.CleanUpPunishmentMessagesAsync();
bool reminders = await Tasks.ReminderTasks.CheckRemindersAsync();
bool raidmode = await Tasks.RaidmodeTasks.CheckRaidmodeAsync(ctx.Guild.Id);
bool unlocks = await Tasks.LockdownTasks.CheckUnlocksAsync();
bool channelUpdateEvents = await Tasks.EventTasks.HandlePendingChannelUpdateEventsAsync();
bool channelDeleteEvents = await Tasks.EventTasks.HandlePendingChannelDeleteEventsAsync();

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}`\nPending Channel Update events check result: `{channelUpdateEvents}`\nPending Channel Delete events check result: `{channelDeleteEvents}`");
await msg.ModifyAsync($"Unban check result: `{bans}`\nUnmute check result: `{mutes}`\nPunishment message cleanup check result: `{punishmentMessages}`\nReminders check result: `{reminders}`\nRaidmode check result: `{raidmode}`\nUnlocks check result: `{unlocks}`\nPending Channel Update events check result: `{channelUpdateEvents}`\nPending Channel Delete events check result: `{channelDeleteEvents}`");
}

[Command("sh")]
Expand Down
3 changes: 1 addition & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,7 @@ static async Task Main(string[] _)
[
Tasks.PunishmentTasks.CheckMutesAsync(),
Tasks.PunishmentTasks.CheckBansAsync(),
Tasks.PunishmentTasks.CheckAutomaticWarningsAsync(),
Tasks.PunishmentTasks.CheckCompromisedAccountBansAsync(),
Tasks.PunishmentTasks.CleanUpPunishmentMessagesAsync(),
Tasks.ReminderTasks.CheckRemindersAsync(),
Tasks.RaidmodeTasks.CheckRaidmodeAsync(cfgjson.ServerID),
Tasks.LockdownTasks.CheckUnlocksAsync(),
Expand Down
63 changes: 26 additions & 37 deletions Tasks/PunishmentTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,26 @@ public static async Task<bool> CheckMutesAsync()
}
}

public static async Task<bool> CheckAutomaticWarningsAsync()
// Cleans up public messages for automatic warnings & bans for compromised accounts
public static async Task<bool> CleanUpPunishmentMessagesAsync()
{
if (Program.cfgjson.AutoWarnMsgAutoDeleteDays == 0)
if (Program.cfgjson.AutoWarnMsgAutoDeleteDays == 0 && Program.cfgjson.CompromisedAccountBanMsgAutoDeleteDays == 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;

if (Program.cfgjson.AutoWarnMsgAutoDeleteDays > 0)
{
// The success value will be changed later if any of the message deletes are successful.
bool success = false;
Dictionary<string, UserWarning> warnList = Program.db.HashGetAll("automaticWarnings").ToDictionary(
x => x.Name.ToString(),
x => JsonConvert.DeserializeObject<UserWarning>(x.Value)
);

foreach (KeyValuePair<string, UserWarning> entry in warnList)
{
UserWarning warn = entry.Value;
if (DateTime.Now > warn.WarnTimestamp.AddDays(Program.cfgjson.AutoWarnMsgAutoDeleteDays))
if (DateTime.Now > warn.WarnTimestamp.AddSeconds(Program.cfgjson.AutoWarnMsgAutoDeleteDays))
{
try
{
Expand All @@ -94,32 +94,20 @@ public static async Task<bool> CheckAutomaticWarningsAsync()
}
}
}
Program.discord.Logger.LogDebug(Program.CliptokEventID, "Checked automatic warnings at {time} with result: {result}", DateTime.Now, success);
return success;
}
}

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

Dictionary<string, MemberPunishment> banList = Program.db.HashGetAll("compromisedAccountBans").ToDictionary(
x => x.Name.ToString(),
x => JsonConvert.DeserializeObject<MemberPunishment>(x.Value)
);

if (banList.Keys.Count == 0)
return false;
else

if (Program.cfgjson.CompromisedAccountBanMsgAutoDeleteDays > 0)
{
// The success value will be changed later if any of the message deletes are successful.
bool success = false;
Dictionary<string, MemberPunishment> banList = Program.db.HashGetAll("compromisedAccountBans").ToDictionary(
x => x.Name.ToString(),
x => JsonConvert.DeserializeObject<MemberPunishment>(x.Value)
);

foreach (KeyValuePair<string, MemberPunishment> entry in banList)
{
MemberPunishment ban = entry.Value;
if (DateTime.Now > ban.ActionTime.Value.AddDays(Program.cfgjson.CompromisedAccountBanMsgAutoDeleteDays))

if (DateTime.Now > ban.ActionTime.Value.AddSeconds(Program.cfgjson.CompromisedAccountBanMsgAutoDeleteDays))
{
try
{
Expand All @@ -136,9 +124,10 @@ public static async Task<bool> CheckCompromisedAccountBansAsync()
}
}
}
Program.discord.Logger.LogDebug(Program.CliptokEventID, "Checked compromised account bans at {time} with result: {result}", DateTime.Now, success);
return success;
}

Program.discord.Logger.LogDebug(Program.CliptokEventID, "Checked for auto-warn and compromised account ban messages at {time} with result: {result}", DateTime.Now, success);
return success;
}
}

Expand Down

0 comments on commit 6cc9b66

Please sign in to comment.