basic js logic question

I have x2 arrays: QuestBotGuildIds = an array of Id’s from my db. UserGuilds = an array basic guild information for every guild. I want to get all of the UserGuilds with an id === one of the guilds in QuestBotGuildId’s. Without a for loop as the QuestBotGuildId’s is an api request, so is UserGuilds.
4 Replies
John
John3y ago
userGuilds.filter(guild=> questBotIds.includes(guild.Id))
userGuilds.filter(guild=> questBotIds.includes(guild.Id))
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
John
John3y ago
Yeah the thing I wrote is basically O(n^2)
mac
macOP3y ago
Ok appreciate it Ty
import { DiscordGuild } from "next-auth";
import { fixGuildPFP } from "src/utils/discord/fixGuildPFP";
import { getQuestBotGuildIds } from "../questBot/guildBackend";

export async function getDiscordGuilds(
accessToken: string | undefined,
tokenType: string | undefined
) {
const response = await fetch("https://discord.com/api/users/@me/guilds", {
headers: {
Authorization: `${tokenType} ${accessToken}`,
},
});
const allGuilds = await response.json();
const questBotGuildIds = await getQuestBotGuildIds();

// Get all guilds that the user is in that also have QuestBot installed or owns the guild

for (let i = 0; i < allGuilds.length; i++) {
await fixGuildPFP(allGuilds[i]);
if (allGuilds[i].owner === true) {
allGuilds[i].memberType = "admin";
} else {
allGuilds[i].memberType = "user";
}
}

const guildsWithQuestBotAndOwned = allGuilds.filter(
(guild: { id: string; owner: boolean }) =>
questBotGuildIds.includes(guild.id) && guild.owner === true
);

const guildsOwned = allGuilds.filter(
(guild: { id: string; owner: boolean }) =>
guild.owner === true && !questBotGuildIds.includes(guild.id)
);

const guildsWithQuestBot = allGuilds.filter(
(guild: { id: string; owner: boolean }) =>
questBotGuildIds.includes(guild.id) && guild.owner === false
);
const guilds: DiscordGuild[] = guildsWithQuestBotAndOwned.concat(guildsOwned, guildsWithQuestBot);

console.log(guilds)
return guilds;
}
import { DiscordGuild } from "next-auth";
import { fixGuildPFP } from "src/utils/discord/fixGuildPFP";
import { getQuestBotGuildIds } from "../questBot/guildBackend";

export async function getDiscordGuilds(
accessToken: string | undefined,
tokenType: string | undefined
) {
const response = await fetch("https://discord.com/api/users/@me/guilds", {
headers: {
Authorization: `${tokenType} ${accessToken}`,
},
});
const allGuilds = await response.json();
const questBotGuildIds = await getQuestBotGuildIds();

// Get all guilds that the user is in that also have QuestBot installed or owns the guild

for (let i = 0; i < allGuilds.length; i++) {
await fixGuildPFP(allGuilds[i]);
if (allGuilds[i].owner === true) {
allGuilds[i].memberType = "admin";
} else {
allGuilds[i].memberType = "user";
}
}

const guildsWithQuestBotAndOwned = allGuilds.filter(
(guild: { id: string; owner: boolean }) =>
questBotGuildIds.includes(guild.id) && guild.owner === true
);

const guildsOwned = allGuilds.filter(
(guild: { id: string; owner: boolean }) =>
guild.owner === true && !questBotGuildIds.includes(guild.id)
);

const guildsWithQuestBot = allGuilds.filter(
(guild: { id: string; owner: boolean }) =>
questBotGuildIds.includes(guild.id) && guild.owner === false
);
const guilds: DiscordGuild[] = guildsWithQuestBotAndOwned.concat(guildsOwned, guildsWithQuestBot);

console.log(guilds)
return guilds;
}
so you reckon this will fetch either one of the api's multiple times ? idk how to test it
Want results from more Discord servers?
Add your server