From f6a80dc57846c7299e6dc672a545942fdb15d693 Mon Sep 17 00:00:00 2001 From: Yuzu Date: Mon, 6 Jun 2022 20:44:39 +0700 Subject: [PATCH] Support localization --- src/discord/Stats.ts | 46 +++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/src/discord/Stats.ts b/src/discord/Stats.ts index f742efb..dc3d524 100644 --- a/src/discord/Stats.ts +++ b/src/discord/Stats.ts @@ -1,3 +1,4 @@ +import { I18n } from 'i18n'; import { Message, CommandInteraction, Interaction } from 'discord.js'; import os from 'os-utils'; @@ -5,30 +6,35 @@ import DiscordProvider from '../providers/Discord'; import DiscordModule, { HybridInteractionMessage } from '../utils/DiscordModule'; import { sendHybridInteractionMessageResponse, makeInfoEmbed } from '../utils/DiscordMessage'; +import Locale from '../providers/Locale'; +import Cache from '../providers/Cache'; const EMBEDS = { - STATS_INFO: (data: Message | Interaction) => { + STATS_INFO: (data: Message | Interaction, locale: I18n) => { + const totalServers = DiscordProvider.client.guilds.cache.size; + const totalUsers = DiscordProvider.client.guilds.cache.map((g) => g.memberCount).reduce((a, c) => a + c); + const systemUptime = Math.round(new Date().getTime() / 1000) - Math.round(os.sysUptime()); + const processUptime = Math.round(new Date().getTime() / 1000) - Math.round(os.processUptime()); + return makeInfoEmbed({ - title: `Stats`, + title: locale.__('stats.title'), icon: 'πŸ“Š', - description: 'Stats of the bot', + description: locale.__('stats.description'), fields: [ { - name: 'πŸŒβ€‚Users', - value: `${ - DiscordProvider.client.guilds.cache.size - } servers\n${DiscordProvider.client.guilds.cache - .map((g) => g.memberCount) - .reduce((a, c) => a + c)} users`, + name: `πŸŒβ€‚${locale.__('stats.users')}`, + value: `${locale.__('stats.x_servers', totalServers.toString())}\n${locale.__( + 'stats.x_users', + totalUsers.toString() + )}`, inline: true }, { - name: 'πŸŸ’β€‚Uptime since', - value: `System: \nProcess: `, + name: `πŸŸ’β€‚${locale.__('stats.uptime_since')}`, + value: `${locale.__('stats.server_uptime_x', ``)}\n${locale.__( + 'stats.process_uptime_x', + `` + )}`, inline: true } ], @@ -51,8 +57,16 @@ export default class Stats extends DiscordModule { } async run(data: HybridInteractionMessage, args: any) { + const guild = data.getGuild(); + if (!guild) return; + + const cachedGuild = await Cache.getCachedGuild(guild.id); + if (!cachedGuild) return; + + const LocaleProvider = Locale.getLocaleProvider(cachedGuild.locale); + return await sendHybridInteractionMessageResponse(data, { - embeds: [EMBEDS.STATS_INFO(data.getRaw())] + embeds: [EMBEDS.STATS_INFO(data.getRaw(), LocaleProvider)] }); } }