Skip to content

Commit

Permalink
Avoid spamming the shit out of Discord's API when we get a DM
Browse files Browse the repository at this point in the history
(Actually use cache instead of making tons of unnecessary API requests)
  • Loading branch information
FloatingMilkshake committed Sep 11, 2024
1 parent 0d9f7bd commit a516c56
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions Events/MessageEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,10 @@ await e.Channel.SendMessageAsync(new DiscordMessageBuilder()
if (targetChannel == default)
{
DiscordGuild mutualServer = default;
foreach (var guildId in client.Guilds)
foreach (var cachedGuild in client.Guilds.Values)
{
var server = await client.GetGuildAsync(guildId.Key);
if (!(await server.GetAllMembersAsync()).Contains(targetUser)) continue;
mutualServer = await client.GetGuildAsync(server.Id);
if (!cachedGuild.Members.Values.Contains(targetUser)) continue;
mutualServer = await client.GetGuildAsync(cachedGuild.Id);
break;
}
Expand Down Expand Up @@ -214,13 +212,15 @@ await e.Message.RespondAsync(
{
try
{
List<DiscordGuild> mutualServers = new();
foreach (var owner in client.CurrentApplication.Owners)
foreach (var guildPair in client.Guilds)
foreach (var guild in client.Guilds.Values)
{
var guild = await client.GetGuildAsync(guildPair.Key);
if (!(await guild.GetAllMembersAsync()).Contains(owner)) continue;
if (!guild.Members.Values.Contains(owner)) continue;
var ownerMember = await guild.GetMemberAsync(owner.Id);
mutualServers.Add(guild);
DiscordEmbedBuilder embed = new()
{
Expand All @@ -245,13 +245,10 @@ await e.Message.RespondAsync(
embed.AddField("Attachments", attachmentUrls, true);
}
var mutualServers = "";
foreach (var guildId in client.Guilds)
var mutualServersResponseList = "";
foreach (var server in mutualServers)
{
var server = await client.GetGuildAsync(guildId.Key);
if ((await server.GetAllMembersAsync()).Contains(e.Author)) mutualServers += $"- `{server}`\n";
mutualServersResponseList += $"- `{server}`\n";
}
DiscordMessageBuilder messageBuilder = new();
Expand Down Expand Up @@ -281,7 +278,7 @@ await e.Message.RespondAsync(
messageBuilder.AddComponents(button);
}
embed.AddField("Mutual Servers", mutualServers);
embed.AddField("Mutual Servers", mutualServersResponseList);
messageBuilder = messageBuilder.AddEmbed(embed.Build());
Expand Down

0 comments on commit a516c56

Please sign in to comment.