🌐 locale support for top module
This commit is contained in:
parent
4fb241bc97
commit
2f76c7c7a7
1 changed files with 57 additions and 21 deletions
|
@ -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),
|
||||
],
|
||||
});
|
||||
},
|
||||
|
|
Loading…
Add table
Reference in a new issue