Skip to content

Commit

Permalink
feat(localization): migrate from nestjs-i18n to @nestjs/localization
Browse files Browse the repository at this point in the history
remove some specs

BREAKING CHANGE: some commands will not be executed
  • Loading branch information
NedcloarBR committed Apr 13, 2024
1 parent 345afc3 commit 14a9428
Show file tree
Hide file tree
Showing 45 changed files with 2,021 additions and 1,669 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
"topgg",
"TYPEORM",
"Uhlein",
"Undeaf",
"vimeo",
"youshallnotpass",
"youtube",
Expand Down
2 changes: 1 addition & 1 deletion Packages/Client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@fastify/cookie": "^9.3.1",
"@fastify/helmet": "^11.1.1",
"@fastify/static": "^7.0.3",
"@necord/localization": "^3.1.0",
"@necord/pagination": "^1.2.2",
"@nestjs/axios": "^3.0.2",
"@nestjs/common": "^10.3.3",
Expand All @@ -54,7 +55,6 @@
"lavalink-client": "https://github.com/Tomato6966/lavalink-client",
"moment": "^2.30.1",
"necord": "^6.7.0",
"nestjs-i18n": "^10.4.5",
"parse-ms": "=2.1.0",
"passport": "^0.7.0",
"passport-discord": "^0.1.4",
Expand Down
34 changes: 22 additions & 12 deletions Packages/Client/src/common/guards/Args/Embed.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Context } from "@/modules/bot/commands/Commands.context";
import { LegacyCommandOptions } from "@/modules/bot/commands/types";
import type { Ii18nService } from "@/modules/bot/i18n/interfaces/Ii18nService";
import { NestedLocalizationAdapter } from "@necord/localization";
import { Client, EmbedBuilder } from "discord.js";

export async function embed(
client: Client,
Translate: Ii18nService,
translate: NestedLocalizationAdapter,
type: "TooManyArgs" | "NoMinArgs",
context: Context,
commandOptions: LegacyCommandOptions,
Expand All @@ -15,21 +15,31 @@ export async function embed(
name: context.author.tag,
iconURL: context.author.displayAvatarURL(),
})
.setTitle(await Translate.TFunction(context, `Tools/Command:Checker:${type}:Title`))
.setTitle(translate.getTranslation(`Tools/Command:Checker:${type}:Title`, context.interaction.guildLocale))
.setColor("#c20e00")
.setDescription(await Translate.TFunction(context, "Tools/Command:Checker:NoMinArgs:Description"))
.setDescription(
translate.getTranslation("Tools/Command:Checker:NoMinArgs:Description", context.interaction.guildLocale),
)
.addFields([
{
name: await Translate.TFunction(context, "Tools/Command:Checker:NoMinArgs:Fields:1"),
value: await Translate.TFunction(context, "Tools/Command:Checker:NoMinArgs:Fields:Content:1", {
Args: commandOptions.args.min,
}),
name: translate.getTranslation("Tools/Command:Checker:NoMinArgs:Fields:1", context.interaction.guildLocale),
value: translate.getTranslation(
"Tools/Command:Checker:NoMinArgs:Fields:Content:1",
context.interaction.guildLocale,
{
Args: String(commandOptions.args.min),
},
),
},
{
name: await Translate.TFunction(context, "Tools/Command:Checker:NoMinArgs:Fields:2"),
value: await Translate.TFunction(context, "Tools/Command:Checker:NoMinArgs:Fields:Content:2", {
Usage: `${context.prefix}${commandOptions.name} ${commandOptions.usage}`,
}),
name: translate.getTranslation("Tools/Command:Checker:NoMinArgs:Fields:2", context.interaction.guildLocale),
value: translate.getTranslation(
"Tools/Command:Checker:NoMinArgs:Fields:Content:2",
context.interaction.guildLocale,
{
Usage: `${context.prefix}${commandOptions.name} ${commandOptions.usage}`,
},
),
},
])
.setFooter({
Expand Down
7 changes: 3 additions & 4 deletions Packages/Client/src/common/guards/Args/Max.guard.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import type { Ii18nService } from "@/modules/bot/i18n/interfaces/Ii18nService";
import { Extends } from "@/types/Constants";
import { LOCALIZATION_ADAPTER, NestedLocalizationAdapter } from "@necord/localization";
import { CanActivate, ExecutionContext, Inject, Injectable } from "@nestjs/common";
import { Utils } from "../Utils";
import { embed } from "./Embed";

@Injectable()
export class MaxArgsGuard implements CanActivate {
public constructor(@Inject(Extends.Translate) private readonly Translate: Ii18nService) {}
public constructor(@Inject(LOCALIZATION_ADAPTER) private readonly translate: NestedLocalizationAdapter) {}

public async canActivate(executionContext: ExecutionContext): Promise<boolean> {
const { client, context, legacyCommandOptions } = Utils.context(executionContext);
const args = context.getArg("", -1);

if (!context.isSlash) {
if (args.length > legacyCommandOptions.args.max) {
Utils.SendFunction(context, await embed(client, this.Translate, "TooManyArgs", context, legacyCommandOptions));
Utils.SendFunction(context, await embed(client, this.translate, "TooManyArgs", context, legacyCommandOptions));
return false;
}
}
Expand Down
5 changes: 2 additions & 3 deletions Packages/Client/src/common/guards/Args/Min.guard.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { Ii18nService } from "@/modules/bot/i18n/interfaces/Ii18nService";
import { Extends } from "@/types/Constants";
import { LOCALIZATION_ADAPTER, NestedLocalizationAdapter } from "@necord/localization";
import { CanActivate, ExecutionContext, Inject, Injectable } from "@nestjs/common";
import { Utils } from "../Utils";
import { embed } from "./Embed";

@Injectable()
export class MinArgsGuard implements CanActivate {
public constructor(@Inject(Extends.Translate) private readonly Translate: Ii18nService) {}
public constructor(@Inject(LOCALIZATION_ADAPTER) private readonly Translate: NestedLocalizationAdapter) {}

public async canActivate(executionContext: ExecutionContext): Promise<boolean> {
const { client, context, legacyCommandOptions } = Utils.context(executionContext);
Expand Down
10 changes: 6 additions & 4 deletions Packages/Client/src/common/guards/Enable.guard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Ii18nService } from "@/modules/bot/i18n/interfaces/Ii18nService";
import { Extends } from "@/types/Constants";
import { LOCALIZATION_ADAPTER, NestedLocalizationAdapter } from "@necord/localization";
import { CanActivate, ExecutionContext, Inject, Injectable } from "@nestjs/common";
import { Reflector } from "@nestjs/core";
import { CommandConfig } from "../decorators";
Expand All @@ -8,7 +7,7 @@ import { Utils } from "./Utils";
@Injectable()
export class EnableGuard implements CanActivate {
public constructor(
@Inject(Extends.Translate) private readonly Translate: Ii18nService,
@Inject(LOCALIZATION_ADAPTER) private readonly translate: NestedLocalizationAdapter,
private readonly reflector: Reflector,
) {}

Expand All @@ -18,7 +17,10 @@ export class EnableGuard implements CanActivate {
const commandConfig = this.reflector.get(CommandConfig.KEY, executionContext.getHandler());

if (commandConfig.disable) {
Utils.SendFunction(context, await this.Translate.TFunction(context, "Tools/Command:Checker:Disable"));
Utils.SendFunction(
context,
this.translate.getTranslation("Tools/Command:Checker:Disable", context.interaction.guildLocale),
);
return false;
}

Expand Down
7 changes: 3 additions & 4 deletions Packages/Client/src/common/guards/Permissions/Bot.guard.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { CommandPermissions } from "@/common/decorators";
import type { Ii18nService } from "@/modules/bot/i18n/interfaces/Ii18nService";
import { Extends } from "@/types/Constants";
import { LOCALIZATION_ADAPTER, NestedLocalizationAdapter } from "@necord/localization";
import { CanActivate, ExecutionContext, Inject, Injectable } from "@nestjs/common";
import { Reflector } from "@nestjs/core";
import { Utils } from "../Utils";

@Injectable()
export class BotPermissionGuard implements CanActivate {
public constructor(
@Inject(Extends.Translate) private readonly Translate: Ii18nService,
@Inject(LOCALIZATION_ADAPTER) private readonly translate: NestedLocalizationAdapter,
private readonly reflector: Reflector,
) {}

Expand All @@ -20,7 +19,7 @@ export class BotPermissionGuard implements CanActivate {
if (!context.guild.members.me.permissions.has(permissions.bot)) {
Utils.SendFunction(
context,
await this.Translate.TFunction(context, "Tools/Commands:Permission:Bot", {
this.translate.getTranslation("Tools/Commands:Permission:Bot", context.interaction.guildLocale, {
PERMS: Utils.formatArray(permissions.bot as Array<string>),
}),
);
Expand Down
10 changes: 6 additions & 4 deletions Packages/Client/src/common/guards/Permissions/Guild.Guard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CommandPermissions } from "@/common/decorators";
import type { Ii18nService } from "@/modules/bot/i18n/interfaces/Ii18nService";
import { Config } from "@/modules/shared/config/types";
import { Extends } from "@/types/Constants";
import { LOCALIZATION_ADAPTER, NestedLocalizationAdapter } from "@necord/localization";
import { CanActivate, ExecutionContext, Inject, Injectable } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { Reflector } from "@nestjs/core";
Expand All @@ -10,7 +9,7 @@ import { Utils } from "../Utils";
@Injectable()
export class GuildPermissionGuard implements CanActivate {
public constructor(
@Inject(Extends.Translate) private readonly Translate: Ii18nService,
@Inject(LOCALIZATION_ADAPTER) private readonly translate: NestedLocalizationAdapter,
private readonly config: ConfigService<Config>,
private readonly reflector: Reflector,
) {}
Expand All @@ -20,7 +19,10 @@ export class GuildPermissionGuard implements CanActivate {
const { context, slashCommandOptions } = Utils.context(executionContext);

if ((permissions.guildOnly || slashCommandOptions?.deployMode === "Guild") && !this.checkGuild(context.guild.id)) {
Utils.SendFunction(context, await this.Translate.TFunction(context, "Tools/Command:Checker:GuildOnly"));
Utils.SendFunction(
context,
this.translate.getTranslation("Tools/Command:Checker:GuildOnly", context.interaction.guildLocale),
);
return false;
}

Expand Down
10 changes: 6 additions & 4 deletions Packages/Client/src/common/guards/Permissions/Owner.Guard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CommandPermissions } from "@/common/decorators";
import type { Ii18nService } from "@/modules/bot/i18n/interfaces/Ii18nService";
import { Config } from "@/modules/shared/config/types";
import { Extends } from "@/types/Constants";
import { LOCALIZATION_ADAPTER, NestedLocalizationAdapter } from "@necord/localization";
import { CanActivate, ExecutionContext, Inject, Injectable } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { Reflector } from "@nestjs/core";
Expand All @@ -10,7 +9,7 @@ import { Utils } from "../Utils";
@Injectable()
export class OwnerPermissionGuard implements CanActivate {
public constructor(
@Inject(Extends.Translate) private readonly Translate: Ii18nService,
@Inject(LOCALIZATION_ADAPTER) private readonly translate: NestedLocalizationAdapter,
private readonly config: ConfigService<Config>,
private readonly reflector: Reflector,
) {}
Expand All @@ -20,7 +19,10 @@ export class OwnerPermissionGuard implements CanActivate {
const { context } = Utils.context(executionContext);

if (permissions.ownerOnly && !this.checkOwner(context.author.id)) {
Utils.SendFunction(context, await this.Translate.TFunction(context, "Tools/Command:Checker:OwnerOnly"));
Utils.SendFunction(
context,
this.translate.getTranslation("Tools/Command:Checker:OwnerOnly", context.interaction.guildLocale),
);
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions Packages/Client/src/common/guards/Permissions/User.Guard.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { CommandPermissions } from "@/common/decorators";
import type { Ii18nService } from "@/modules/bot/i18n/interfaces/Ii18nService";
import { Extends } from "@/types/Constants";
import { LOCALIZATION_ADAPTER, NestedLocalizationAdapter } from "@necord/localization";
import { CanActivate, ExecutionContext, Inject, Injectable } from "@nestjs/common";
import { Reflector } from "@nestjs/core";
import { Utils } from "../Utils";

@Injectable()
export class UserPermissionGuard implements CanActivate {
public constructor(
@Inject(Extends.Translate) private readonly Translate: Ii18nService,
@Inject(LOCALIZATION_ADAPTER) private readonly translate: NestedLocalizationAdapter,
private readonly reflector: Reflector,
) {}

Expand All @@ -20,7 +20,7 @@ export class UserPermissionGuard implements CanActivate {
if (!(await context.guild.members.fetch(context.author.id)).permissions.has(permissions.user)) {
Utils.SendFunction(
context,
await this.Translate.TFunction(context, "Tools/Commands:Permission:User", {
this.translate.getTranslation("Tools/Commands:Permission:User", context.interaction.guildLocale, {
PERMS: Utils.formatArray(permissions.user as Array<string>),
}),
);
Expand Down
23 changes: 15 additions & 8 deletions Packages/Client/src/modules/bot/commands/Commands.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { LegacyCommand, SlashCommand } from "@/common/decorators";
import type { Ii18nService } from "@/modules/bot/i18n/interfaces/Ii18nService";
import { GuildEntity, UserEntity } from "@/modules/shared/database/entities";
import type { IDatabaseService } from "@/modules/shared/database/interfaces/IDatabaseService";
import { Extends, Services } from "@/types/Constants";
import { WAIT } from "@/utils/Tools";
import { LOCALIZATION_ADAPTER, NestedLocalizationAdapter } from "@necord/localization";
import { Global, Inject, Logger, Module, OnApplicationBootstrap, OnModuleInit, Provider } from "@nestjs/common";
import { EventEmitter2 } from "@nestjs/event-emitter";
import {
Expand Down Expand Up @@ -35,7 +35,7 @@ const provider: Provider<CommandsService> = {
export class CommandsModule implements OnModuleInit, OnApplicationBootstrap {
public constructor(
@Inject(Services.Database) private readonly database: IDatabaseService,
@Inject(Extends.Translate) private readonly Translate: Ii18nService,
@Inject(LOCALIZATION_ADAPTER) private readonly translate: NestedLocalizationAdapter,
@Inject(Extends.Command) private readonly commandsService: CommandsService,
private readonly client: Client,
private readonly eventEmitter: EventEmitter2,
Expand Down Expand Up @@ -75,14 +75,14 @@ export class CommandsModule implements OnModuleInit, OnApplicationBootstrap {
const Prefix = mentionRegexPrefix.exec(message.content)
? mentionRegexPrefix.exec(message.content)[0]
: message.channel.type !== ChannelType.DM
? GuildPrefix
: UserPrefix;
? GuildPrefix
: UserPrefix;

if (message.content === userMention(this.client.user.id)) {
message.delete();
const PrefixMessage = await MessageTools.send(
message.author,
await this.Translate.Guild(message, "Events/MessageCreate:MyPrefix", {
await this.translate.getTranslation("Events/MessageCreate:MyPrefix", message.guild.preferredLocale, {
Guild: message.guild.name,
User: userMention(message.author.id),
Prefix,
Expand Down Expand Up @@ -152,10 +152,17 @@ export class CommandsModule implements OnModuleInit, OnApplicationBootstrap {
name: _name,
iconURL: _icon,
})
.setTitle(await this.Translate.TFunction(context, "Events/MessageCreate:ConfigurationCreated:Title"))
.setTitle(
await this.translate.getTranslation(
"Events/MessageCreate:ConfigurationCreated:Title",
message.guild.preferredLocale,
),
)
.setDescription(
(await this.Translate.TFunction(context, "Events/MessageCreate:ConfigurationCreated:Description")) +
codeBlock("JSON", JSON.stringify(config, null, 3)),
(await this.translate.getTranslation(
"Events/MessageCreate:ConfigurationCreated:Description",
message.guild.preferredLocale,
)) + codeBlock("JSON", JSON.stringify(config, null, 3)),
)
.setColor("#00c26f")
.setTimestamp(),
Expand Down
12 changes: 5 additions & 7 deletions Packages/Client/src/modules/bot/components/Buttons.component.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import type { Ii18nService } from "@/modules/bot/i18n/interfaces/Ii18nService";
import { Extends } from "@/types/Constants";
import { LOCALIZATION_ADAPTER, NestedLocalizationAdapter } from "@necord/localization";
import { Inject, Injectable } from "@nestjs/common";
import { ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js";
import { Context } from "../commands/Commands.context";

@Injectable()
export class Buttons {
public constructor(@Inject(Extends.Translate) private readonly Translate: Ii18nService) {}
public constructor(@Inject(LOCALIZATION_ADAPTER) private readonly translate: NestedLocalizationAdapter) {}

public async Confirm(context: Context): Promise<ActionRowBuilder<ButtonBuilder>> {
public async Confirm(locale: string): Promise<ActionRowBuilder<ButtonBuilder>> {
return new ActionRowBuilder<ButtonBuilder>().addComponents([
new ButtonBuilder()
.setCustomId("confirm/yes")
.setLabel(await this.Translate.Guild(context, "Tools/Buttons:Labels:Confirm:YES"))
.setLabel(this.translate.getTranslation("Tools/Buttons:Labels:Confirm:YES", locale))
.setEmoji("719710630881525881")
.setStyle(ButtonStyle.Success),
new ButtonBuilder()
.setCustomId("confirm/no")
.setLabel(await this.Translate.Guild(context, "Tools/Buttons:Labels:Confirm:NO"))
.setLabel(this.translate.getTranslation("Tools/Buttons:Labels:Confirm:NO", locale))
.setEmoji("719710607405875321")
.setStyle(ButtonStyle.Danger),
]);
Expand Down
15 changes: 13 additions & 2 deletions Packages/Client/src/modules/bot/core/NDB.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { JSONLocaleLoader } from "@/modules/shared/config/JSONLocale.loader";
import { NecordConfigService } from "@/modules/shared/config/NecordConfig.service";
import { Config } from "@/modules/shared/config/types";
import { SharedModule } from "@/modules/shared/shared.module";
import { GuildResolver, NecordLocalizationModule, NestedLocalizationAdapter } from "@necord/localization";
import { NecordPaginationModule } from "@necord/pagination";
import { Module } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
Expand All @@ -8,7 +11,6 @@ import { CommandsModule } from "../commands/Commands.module";
import { ComponentsModule } from "../components/Components.module";
import { DeveloperToolsModule } from "../developerTools/DeveloperTools.module";
import { EventsModule } from "../events/Events.module";
import { I18nModule } from "../i18n/i18n.module";
import { MusicModule } from "../music/Music.module";
import { ReactionRolesModule } from "../reactionRoles/ReactionRoles.module";
import { NDBServiceProvider } from "./provider/NDBService.provider";
Expand All @@ -25,8 +27,17 @@ import { NDBServiceProvider } from "./provider/NDBService.provider";
allowSkip: false,
allowTraversal: false,
}),
NecordLocalizationModule.forRootAsync({
inject: [ConfigService],
useFactory: async (config: ConfigService) => ({
adapter: new NestedLocalizationAdapter({
fallbackLocale: config.getOrThrow<Config["FallbackLocale"]>("FallbackLocale"),
locales: await new JSONLocaleLoader("./src/common/Languages/i18n").loadTranslations(),
}),
resolvers: GuildResolver,
}),
}),
SharedModule,
I18nModule,
ComponentsModule,
EventsModule,
CommandsModule,
Expand Down
Loading

0 comments on commit 14a9428

Please sign in to comment.