Skip to content

Commit

Permalink
refactor: update http-framework-i18n
Browse files Browse the repository at this point in the history
BREAKING CHANGE: refactor: Updated `@skyra/http-framework-i18n` to `2.0.0`
BREAKING CHANGE: refactor: Removed `LanguageKeys` in favour of `@skyra/i18next-type-generator` and `i18next` v23's enhanced type support
  • Loading branch information
kyranet committed Aug 20, 2023
1 parent 2e39517 commit 2da9eb9
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/shared-http-pieces/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The shared commands used for ArchId Network's HTTP-only bots.

## Setup

- Define `locales/{{lng}}/commands/shared:infoEmbedDescription` as a string. This is the content displayed in `/info`'s embed description.
- Define `locales/{{lng}}/commands/shared:infoEmbedDescription` as a string. This is the content displayed in `/info`'s embed description, no augmentation is required for this key.
- Define the information variables.

## Usage
Expand Down
32 changes: 16 additions & 16 deletions packages/shared-http-pieces/src/commands/info.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EmbedBuilder, time, TimestampStyles } from '@discordjs/builders';
import { Command, RegisterCommand } from '@skyra/http-framework';
import { applyLocalizedBuilder, getSupportedUserLanguageT, type TFunction } from '@skyra/http-framework-i18n';
import { applyLocalizedBuilder, getSupportedUserLanguageName, getT, type TFunction } from '@skyra/http-framework-i18n';
import {
ButtonStyle,
ComponentType,
Expand All @@ -10,16 +10,16 @@ import {
type APIMessageActionRowComponent
} from 'discord-api-types/v10';
import { cpus, uptime, type CpuInfo } from 'node:os';
import { LanguageKeys } from '../lib/i18n/LanguageKeys.js';
import { getInvite, getRepository } from '../lib/information.js';

@RegisterCommand((builder) => applyLocalizedBuilder(builder, 'commands/shared:info'))
export class UserCommand extends Command {
public override chatInputRun(interaction: Command.ChatInputInteraction) {
const t = getSupportedUserLanguageT(interaction);
const lng = getSupportedUserLanguageName(interaction);
const t = getT(lng);
const embed = new EmbedBuilder()
.setDescription(t(LanguageKeys.Commands.Shared.InfoEmbedDescription))
.addFields(this.getUptimeStatistics(t), this.getServerUsageStatistics(t));
.setDescription(t('commands/shared:infoEmbedDescription'))
.addFields(this.getUptimeStatistics(t), this.getServerUsageStatistics(t, lng));
const components = this.getComponents(t);

return interaction.reply({ embeds: [embed.toJSON()], components, flags: MessageFlags.Ephemeral });
Expand All @@ -30,23 +30,23 @@ export class UserCommand extends Command {
const nowSeconds = Math.round(now / 1000);

return {
name: t(LanguageKeys.Commands.Shared.InfoFieldUptimeTitle),
value: t(LanguageKeys.Commands.Shared.InfoFieldUptimeValue, {
name: t('commands/shared:infoFieldUptimeTitle'),
value: t('commands/shared:infoFieldUptimeValue', {
host: time(Math.round(nowSeconds - uptime()), TimestampStyles.RelativeTime),
client: time(Math.round(nowSeconds - process.uptime()), TimestampStyles.RelativeTime)
})
};
}

private getServerUsageStatistics(t: TFunction): APIEmbedField {
private getServerUsageStatistics(t: TFunction, lng: string): APIEmbedField {
const usage = process.memoryUsage();

return {
name: t(LanguageKeys.Commands.Shared.InfoFieldServerUsageTitle),
value: t(LanguageKeys.Commands.Shared.InfoFieldServerUsageValue, {
name: t('commands/shared:infoFieldServerUsageTitle'),
value: t('commands/shared:infoFieldServerUsageValue', {
cpu: cpus().map(UserCommand.formatCpuInfo.bind(null)).join(' | '),
heapUsed: (usage.heapUsed / 1048576).toLocaleString(t.lng, { maximumFractionDigits: 2 }),
heapTotal: (usage.heapTotal / 1048576).toLocaleString(t.lng, { maximumFractionDigits: 2 })
heapUsed: (usage.heapUsed / 1048576).toLocaleString(lng, { maximumFractionDigits: 2 }),
heapTotal: (usage.heapTotal / 1048576).toLocaleString(lng, { maximumFractionDigits: 2 })
})
};
}
Expand All @@ -71,7 +71,7 @@ export class UserCommand extends Command {
return {
type: ComponentType.Button,
style: ButtonStyle.Link,
label: t(LanguageKeys.Commands.Shared.InfoButtonSupport),
label: t('commands/shared:infoButtonSupport'),
emoji: { name: '🆘' },
url: 'https://discord.gg/6gakFR2'
};
Expand All @@ -81,7 +81,7 @@ export class UserCommand extends Command {
return {
type: ComponentType.Button,
style: ButtonStyle.Link,
label: t(LanguageKeys.Commands.Shared.InfoButtonInvite),
label: t('commands/shared:infoButtonInvite'),
emoji: { name: '🎉' },
url
};
Expand All @@ -91,7 +91,7 @@ export class UserCommand extends Command {
return {
type: ComponentType.Button,
style: ButtonStyle.Link,
label: t(LanguageKeys.Commands.Shared.InfoButtonGitHub),
label: t('commands/shared:infoButtonGitHub'),
emoji: { id: '950888087188283422', name: 'github2' },
url: getRepository()
};
Expand All @@ -101,7 +101,7 @@ export class UserCommand extends Command {
return {
type: ComponentType.Button,
style: ButtonStyle.Link,
label: t(LanguageKeys.Commands.Shared.InfoButtonDonate),
label: t('commands/shared:infoButtonDonate'),
emoji: { name: '🧡' },
url: 'https://donate.skyra.pw'
};
Expand Down
1 change: 0 additions & 1 deletion packages/shared-http-pieces/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
/// <reference lib="dom" />

export * as Sentry from '@sentry/node';
export * from './lib/i18n/LanguageKeys.js';
export * from './lib/information.js';
export * from './lib/sentry.js';
1 change: 0 additions & 1 deletion packages/shared-http-pieces/src/lib/i18n/LanguageKeys.ts

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

20 changes: 20 additions & 0 deletions packages/shared-http-pieces/src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,23 @@ import { load } from '@skyra/http-framework-i18n';

container.stores.registerPath(new URL('.', import.meta.url));
await load(new URL('../src/locales', import.meta.url));

declare module 'i18next' {
interface CustomTypeOptions {
resources: {
'commands/shared': {
infoName: 'info';
infoDescription: 'Provides information about me, and links for adding the bot and joining the support server';
infoEmbedDescription: string;
infoFieldUptimeTitle: 'Uptime';
infoFieldUptimeValue: '• **Host**: {{host}}\n• **Client**: {{client}}';
infoFieldServerUsageTitle: 'Server Usage';
infoFieldServerUsageValue: '• **CPU Usage**: {{cpu}}\n• **Memory**: {{heapUsed}}MB (Total: {{heapTotal}}MB)';
infoButtonInvite: 'Add me to your server!';
infoButtonSupport: 'Support server';
infoButtonGitHub: 'GitHub Repository';
infoButtonDonate: 'Donate';
};
};
}
}

0 comments on commit 2da9eb9

Please sign in to comment.