Skip to content

Commit

Permalink
Channel events: check user ban status before trying to fetch them
Browse files Browse the repository at this point in the history
Reduces 404s on channel create/update
  • Loading branch information
FloatingMilkshake committed Jan 19, 2025
1 parent c9269c6 commit 02e6ccb
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion Tasks/EventTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,39 @@ public class EventTasks
public static Dictionary<DateTime, ChannelCreatedEventArgs> PendingChannelCreateEvents = new();
public static Dictionary<DateTime, ChannelUpdatedEventArgs> PendingChannelUpdateEvents = new();
public static Dictionary<DateTime, ChannelDeletedEventArgs> PendingChannelDeleteEvents = new();

// populated in Channel Create & Update handlers to save API calls in IsMemberInServer method
private static List<MemberPunishment> CurrentBans = new();

// set to true if the last attempt to populate CurrentBans failed, to suppress warnings in case of repeated failures
private static bool LastBanListPopulationFailed = false;

// todo(milkshake): combine create & update handlers to reduce duplicate code
public static async Task<bool> HandlePendingChannelCreateEventsAsync()
{
bool success = false;

// populate CurrentBans list
try
{
Dictionary<string, MemberPunishment> bans = (await Program.db.HashGetAllAsync("bans")).ToDictionary(
x => x.Name.ToString(),
x => JsonConvert.DeserializeObject<MemberPunishment>(x.Value)
);

CurrentBans = bans.Values.ToList();

LastBanListPopulationFailed = false;
}
catch (Exception ex)
{
if (!LastBanListPopulationFailed)
Program.discord.Logger.LogWarning(ex, "Failed to populate list of current bans during override persistence checks! This warning will be suppressed until the next success!");

// Since this is likely caused by corrupt or otherwise unreadable data in the db, set a flag so that this warning is not spammed
// The flag will be reset on the next successful attempt to populate the CurrentBans list
LastBanListPopulationFailed = true;
}

try
{
Expand Down Expand Up @@ -163,6 +191,28 @@ await Program.db.HashSetAsync("overrides",
public static async Task<bool> HandlePendingChannelUpdateEventsAsync()
{
bool success = false;

// populate CurrentBans list
try
{
Dictionary<string, MemberPunishment> bans = (await Program.db.HashGetAllAsync("bans")).ToDictionary(
x => x.Name.ToString(),
x => JsonConvert.DeserializeObject<MemberPunishment>(x.Value)
);

CurrentBans = bans.Values.ToList();

LastBanListPopulationFailed = false;
}
catch (Exception ex)
{
if (!LastBanListPopulationFailed)
Program.discord.Logger.LogWarning(ex, "Failed to populate list of current bans during override persistence checks! This warning will be suppressed until the next success!");

// Since this is likely caused by corrupt or otherwise unreadable data in the db, set a flag so that this warning is not spammed
// The flag will be reset on the next successful attempt to populate the CurrentBans list
LastBanListPopulationFailed = true;
}

try
{
Expand Down Expand Up @@ -405,7 +455,11 @@ private static async Task<bool> IsMemberInServer(ulong userId, DiscordGuild guil
if (guild.Members.ContainsKey(userId))
return true;

// If the user isn't cached, try fetching them to confirm
// If the user isn't cached, maybe they are banned? Check before making any API calls.
if (CurrentBans.Any(b => b.MemberId == userId))
return false;

// If the user isn't cached or banned, try fetching them to confirm
try
{
await guild.GetMemberAsync(userId);
Expand Down

0 comments on commit 02e6ccb

Please sign in to comment.