Support localization

This commit is contained in:
2022-06-06 20:44:39 +07:00
parent 95dae53187
commit f6a80dc578
+30 -16
View File
@@ -1,3 +1,4 @@
import { I18n } from 'i18n';
import { Message, CommandInteraction, Interaction } from 'discord.js'; import { Message, CommandInteraction, Interaction } from 'discord.js';
import os from 'os-utils'; import os from 'os-utils';
@@ -5,30 +6,35 @@ import DiscordProvider from '../providers/Discord';
import DiscordModule, { HybridInteractionMessage } from '../utils/DiscordModule'; import DiscordModule, { HybridInteractionMessage } from '../utils/DiscordModule';
import { sendHybridInteractionMessageResponse, makeInfoEmbed } from '../utils/DiscordMessage'; import { sendHybridInteractionMessageResponse, makeInfoEmbed } from '../utils/DiscordMessage';
import Locale from '../providers/Locale';
import Cache from '../providers/Cache';
const EMBEDS = { 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({ return makeInfoEmbed({
title: `Stats`, title: locale.__('stats.title'),
icon: '📊', icon: '📊',
description: 'Stats of the bot', description: locale.__('stats.description'),
fields: [ fields: [
{ {
name: '🌐 Users', name: `🌐 ${locale.__('stats.users')}`,
value: `${ value: `${locale.__('stats.x_servers', totalServers.toString())}\n${locale.__(
DiscordProvider.client.guilds.cache.size 'stats.x_users',
} servers\n${DiscordProvider.client.guilds.cache totalUsers.toString()
.map((g) => g.memberCount) )}`,
.reduce((a, c) => a + c)} users`,
inline: true inline: true
}, },
{ {
name: '🟢 Uptime since', name: `🟢 ${locale.__('stats.uptime_since')}`,
value: `System: <t:${ value: `${locale.__('stats.server_uptime_x', `<t:${systemUptime.toString()}:R>`)}\n${locale.__(
Math.round(new Date().getTime() / 1000) - Math.round(os.sysUptime()) 'stats.process_uptime_x',
}:R>\nProcess: <t:${ `<t:${processUptime.toString()}:R>`
Math.round(new Date().getTime() / 1000) - Math.round(os.processUptime()) )}`,
}:R>`,
inline: true inline: true
} }
], ],
@@ -51,8 +57,16 @@ export default class Stats extends DiscordModule {
} }
async run(data: HybridInteractionMessage, args: any) { 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, { return await sendHybridInteractionMessageResponse(data, {
embeds: [EMBEDS.STATS_INFO(data.getRaw())] embeds: [EMBEDS.STATS_INFO(data.getRaw(), LocaleProvider)]
}); });
} }
} }