Skip to content

Commit

Permalink
Defer interaction when retrieving emoji statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
Rian8337 committed Aug 17, 2024
1 parent 54d540e commit 0402510
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/interactions/commands/Tools/emojistatistics/emojistatistics.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { DatabaseManager } from "@alice-database/DatabaseManager";
import { EmojiStatistics } from "@alice-database/utils/aliceDb/EmojiStatistics";
import { ApplicationCommandOptionType, bold } from "discord.js";
import { CommandCategory } from "@alice-enums/core/CommandCategory";
import { SlashCommand } from "structures/core/SlashCommand";
import { OnButtonPageChange } from "@alice-structures/utils/OnButtonPageChange";
import { EmbedCreator } from "@alice-utils/creators/EmbedCreator";
import { MessageButtonCreator } from "@alice-utils/creators/MessageButtonCreator";
import { MessageCreator } from "@alice-utils/creators/MessageCreator";
import { GuildEmoji, GuildMember, EmbedBuilder } from "discord.js";
import { GuildEmoji, GuildMember } from "discord.js";
import { EmojistatisticsLocalization } from "@alice-localization/interactions/commands/Tools/emojistatistics/EmojistatisticsLocalization";
import { CommandHelper } from "@alice-utils/helpers/CommandHelper";
import { StringHelper } from "@alice-utils/helpers/StringHelper";
Expand All @@ -16,12 +15,19 @@ import { LocaleHelper } from "@alice-utils/helpers/LocaleHelper";
import { InteractionHelper } from "@alice-utils/helpers/InteractionHelper";

export const run: SlashCommand["run"] = async (_, interaction) => {
const localization: EmojistatisticsLocalization =
new EmojistatisticsLocalization(CommandHelper.getLocale(interaction));
if (!interaction.inCachedGuild()) {
return;
}

const localization = new EmojistatisticsLocalization(
CommandHelper.getLocale(interaction),
);

await InteractionHelper.deferReply(interaction);

const stats: EmojiStatistics | null =
const stats =
await DatabaseManager.aliceDb.collections.emojiStatistics.getGuildStatistics(
interaction.guild!,
interaction.guild,
);

if (!stats) {
Expand All @@ -38,19 +44,19 @@ export const run: SlashCommand["run"] = async (_, interaction) => {
averagePerMonth: number;
}[] = [];

const currentDate: Date = new Date();
const currentDate = new Date();

for (const emoji of stats.emojiStats.values()) {
const actualEmoji: GuildEmoji | null = await interaction
.guild!.emojis.fetch(emoji.id)
const actualEmoji = await interaction.guild.emojis
.fetch(emoji.id)
.catch(() => null);

if (!actualEmoji) {
continue;
}

const dateCreation: Date = actualEmoji.createdAt;
const months: number = Math.max(
const dateCreation = actualEmoji.createdAt;
const months = Math.max(
1,
(currentDate.getUTCFullYear() - dateCreation.getUTCFullYear()) *
12 +
Expand All @@ -73,16 +79,15 @@ export const run: SlashCommand["run"] = async (_, interaction) => {
});
}

const sortOption: string =
interaction.options.getString("sortoption") ?? "overall";
const sortOption = interaction.options.getString("sortoption") ?? "overall";

validEmojis.sort((a, b) =>
sortOption === "overall"
? b.count - a.count
: b.averagePerMonth - a.averagePerMonth,
);

const embed: EmbedBuilder = EmbedCreator.createNormalEmbed({
const embed = EmbedCreator.createNormalEmbed({
color: (<GuildMember>interaction.member).displayColor,
});

Expand All @@ -102,7 +107,7 @@ export const run: SlashCommand["run"] = async (_, interaction) => {
)}`,
);

const BCP47: string = LocaleHelper.convertToBCP47(localization.language);
const BCP47 = LocaleHelper.convertToBCP47(localization.language);

const onPageChange: OnButtonPageChange = async (_, page) => {
for (
Expand Down

0 comments on commit 0402510

Please sign in to comment.