Skip to content

Commit

Permalink
exponentially faster /about with object lookup
Browse files Browse the repository at this point in the history
also much simpler lol
  • Loading branch information
3vorp committed Feb 28, 2024
1 parent 8e95117 commit 464c508
Showing 1 changed file with 29 additions and 63 deletions.
92 changes: 29 additions & 63 deletions src/commands/faithful/about.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,6 @@ export const command: SlashCommand = {
async execute(interaction) {
await interaction.deferReply();

const loading: string = (
await axios.get(`${interaction.client.tokens.apiUrl}settings/images.loading`)
).data;

const loadingEmbed = new EmbedBuilder()
.setTitle("Searching for contributions...")
.setDescription("This can take some time, please wait...")
.setThumbnail(loading);

await interaction
.editReply({ embeds: [loadingEmbed] })
.then((message: Message) => message.deleteButton());

// if nobody to search up is provided, defaults to the person who asked
const user = interaction.options.getUser("user") ?? interaction.user;
let contributionData: Contribution[] = [];
Expand All @@ -40,58 +27,35 @@ export const command: SlashCommand = {
await axios.get(`${interaction.client.tokens.apiUrl}users/${user.id}/contributions`)
).data;
} catch {
const finalEmbed = new EmbedBuilder()
.setTitle(
interaction
.strings()
.command.about.missing_profile.title.replace("%USER%", user.displayName),
)
.setDescription(
// only tell user to register if they actually can register
interaction.strings().command.about.missing_profile[
user.id === interaction.user.id ? "register" : "description"
],
)
.setColor(colors.red);

return interaction.ephemeralReply({ embeds: [finalEmbed] });
return interaction.ephemeralReply({
embeds: [
new EmbedBuilder()
.setTitle(
interaction
.strings()
.command.about.missing_profile.title.replace("%USER%", user.displayName),
)
.setDescription(
// only tell user to register if they actually can register
interaction.strings().command.about.missing_profile[
user.id === interaction.user.id ? "register" : "description"
],
)
.setColor(colors.red),
],
});
}

// group ids into 30-long nested arrays
const groupedIDs = contributionData
.map((contribution) => contribution.texture)
.filter((v, i, a) => a.indexOf(v) === i) // remove duplicates
.reduce((acc: string[][], cur, index) => {
if (index % 30 === 0) acc.push([]);
acc.at(-1).push(cur);
return acc;
}, []);

const textureData: Texture[] =
// faster to resolve all promises at once than to wait for each one to finish
(
await Promise.all(
groupedIDs.map((ids: string[]) =>
// get texture data in batches of 30
axios
.get(`${interaction.client.tokens.apiUrl}textures/${ids.join(",")}`)
.catch(() => ({ data: null })),
),
)
)
.map((res) => res.data)
.flat();
const textures = (await axios.get(`${interaction.client.tokens.apiUrl}textures/raw`)).data;

// merge the two objects by id
const finalData: (Contribution & Texture)[] = contributionData.map(
(contribution: Contribution) => ({
...contribution,
...textureData.find((texture) => texture.id == contribution.texture),
}),
);
// merge the two objects by id (faster than fetching individually)
const finalData: (Contribution & Texture)[] = contributionData.map((contribution) => ({
...contribution,
...textures[contribution.texture],
}));

const packCount = {};
let files: AttachmentBuilder[] | undefined;
let files: AttachmentBuilder[];
if (finalData.length) {
const textBuf = Buffer.from(
finalData
Expand All @@ -106,7 +70,7 @@ export const command: SlashCommand = {
files = [new AttachmentBuilder(textBuf, { name: "about.txt" })];
}

const finalEmbed = new EmbedBuilder().setTitle(
const embed = new EmbedBuilder().setTitle(
`${user.displayName} has ${finalData.length} ${
finalData.length == 1 ? "contribution" : "contributions"
}!`,
Expand All @@ -116,8 +80,10 @@ export const command: SlashCommand = {
.map((i) => i.join(": "))
.join("\n");

if (finalPackData) finalEmbed.setDescription(finalPackData);
if (finalPackData) embed.setDescription(finalPackData);

await interaction.editReply({ embeds: [finalEmbed], files }).catch(() => {});
await interaction
.editReply({ embeds: [embed], files })
.then((message: Message) => message.deleteButton());
},
};

0 comments on commit 464c508

Please sign in to comment.