Skip to content

Commit

Permalink
Override persistence: Do not rely on cache
Browse files Browse the repository at this point in the history
  • Loading branch information
FloatingMilkshake committed Nov 30, 2024
1 parent ee0f31e commit d8f564a
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions Tasks/EventTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public static async Task<bool> HandlePendingChannelUpdateEventsAsync()

// Try to fetch member. If it fails, they are not in the guild. If this is a voice channel, remove the override.
// (if they are not in the guild & this is not a voice channel, skip; otherwise, code below handles removal)
if (!e.Guild.Members.ContainsKey((ulong)userOverwrites.Name) &&
e.ChannelAfter.Type != DiscordChannelType.Voice)
bool isMemberInServer = await IsMemberInServer((ulong)userOverwrites.Name, e.Guild);
if (!isMemberInServer && e.ChannelAfter.Type != DiscordChannelType.Voice)
continue;

// User could be fetched, so they are in the server and their override was removed. Remove from db.
Expand Down Expand Up @@ -241,5 +241,28 @@ private static bool CompareOverwrites(DiscordOverwrite a, DiscordOverwrite b)

return a.Allowed == b.Allowed && a.Denied == b.Denied && a.Id == b.Id && a.Type == b.Type && a.CreationTimestamp == b.CreationTimestamp;
}

private static async Task<bool> IsMemberInServer(ulong userId, DiscordGuild guild)
{
bool isMemberInServer = false;

// Check cache first
if (guild.Members.ContainsKey(userId))
return true;

// If the user isn't cached, try fetching them to confirm
try
{
await guild.GetMemberAsync(userId);
isMemberInServer = true;
}
catch (DSharpPlus.Exceptions.NotFoundException)
{
// Member is not in the server
// isMemberInServer is already false
}

return isMemberInServer;
}
}
}

0 comments on commit d8f564a

Please sign in to comment.