From 2f76c7c7a7e42c2f066891027df04ee37fa728df Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Fri, 22 Apr 2022 15:11:58 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=90=20locale=20support=20for=20top=20m?= =?UTF-8?q?odule?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/credits/modules/top/index.ts | 78 +++++++++++++++++------- 1 file changed, 57 insertions(+), 21 deletions(-) diff --git a/src/plugins/credits/modules/top/index.ts b/src/plugins/credits/modules/top/index.ts index c27f4df..b580204 100644 --- a/src/plugins/credits/modules/top/index.ts +++ b/src/plugins/credits/modules/top/index.ts @@ -1,48 +1,84 @@ -// Dependencies -import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; +import { + successColor, + errorColor, + footerText, + footerIcon, +} from "@config/embed"; + +import i18next from "i18next"; import { CommandInteraction, MessageEmbed } from "discord.js"; +import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; +import logger from "@logger"; -import userSchema from "@schemas/user"; - -// Configurations -import { successColor, footerText, footerIcon } from "@config/embed"; - -// Helpers -import pluralize from "@helpers/pluralize"; +import userSchema, { IUser } from "@schemas/user"; export default { data: (command: SlashCommandSubcommandBuilder) => { return command.setName("top").setDescription(`View the top users`); }, execute: async (interaction: CommandInteraction) => { - // Get all users in the guild + const { locale, guild } = interaction; - const usersDB = await userSchema.find({ guildId: interaction?.guild?.id }); + const embed = new MessageEmbed() + .setTitle( + i18next.t("credits:modules:top:general:title", { + lng: locale, + ns: "plugins", + }) + ) + .setTimestamp(new Date()) + .setFooter({ text: footerText, iconURL: footerIcon }); + + if (guild === null) { + logger.verbose(`Guild is null`); + + return interaction.editReply({ + embeds: [ + embed + .setDescription( + i18next.t("guildOnly", { + lng: locale, + ns: "errors", + }) + ) + .setColor(errorColor), + ], + }); + } + + const usersDB = await userSchema.find({ guildId: guild.id }); const topTen = usersDB // Sort them after credits amount (ascending) - .sort((a, b) => (a?.credits > b?.credits ? -1 : 1)) + .sort((a, b) => (a.credits > b.credits ? -1 : 1)) // Return the top 10 .slice(0, 10); // Create entry object - const entry = (x: any, index: number) => - `${index + 1}. <@${x?.userId}> - ${pluralize(x?.credits, "credit")}`; + const entry = (x: IUser, index: number) => + i18next.t("credits:modules:top:entry", { + lng: locale, + ns: "plugins", + index: index + 1, + user: x.userId, + amount: x.credits, + }); return interaction.editReply({ embeds: [ - new MessageEmbed() - .setTitle("[:dollar:] Credits (Top)") + embed .setDescription( - `Top 10 users with the most credits. + ` ${i18next.t("credits:modules:top:success01:description", { + lng: locale, + ns: "plugins", + })} - ${topTen.map(entry).join("\n")}` + ${topTen.map(entry).join("\n")} + ` ) - .setTimestamp(new Date()) - .setColor(successColor) - .setFooter({ text: footerText, iconURL: footerIcon }), + .setColor(successColor), ], }); },