diff --git a/src/common/decorators/CommandConfig.decorator.ts b/src/common/decorators/CommandConfig.decorator.ts index 0c90b7a4..0918eed0 100644 --- a/src/common/decorators/CommandConfig.decorator.ts +++ b/src/common/decorators/CommandConfig.decorator.ts @@ -1,6 +1,6 @@ import { Reflector } from "@nestjs/core"; -interface CommandConfigOptions { +export interface CommandConfigOptions { category: string; disable: boolean; } diff --git a/src/common/decorators/index.ts b/src/common/decorators/index.ts index 60af606d..9f021435 100644 --- a/src/common/decorators/index.ts +++ b/src/common/decorators/index.ts @@ -1,3 +1,6 @@ -export { CommandPermissions } from "./CommandPermissions.decorator"; -export { CommandConfig } from "./CommandConfig.decorator"; +export { + CommandPermissions, + CommandPermissionsOptions, +} from "./CommandPermissions.decorator"; +export { CommandConfig, CommandConfigOptions } from "./CommandConfig.decorator"; export { ValidatedOptions } from "./ValidatedOptions.decorator"; diff --git a/src/modules/commands/Commands.module.ts b/src/modules/commands/Commands.module.ts old mode 100644 new mode 100755 index f990bfaa..0144880e --- a/src/modules/commands/Commands.module.ts +++ b/src/modules/commands/Commands.module.ts @@ -1,8 +1,9 @@ import { Module } from "@nestjs/common"; -import * as Commands from "./index"; import { CommandsService } from "./Commands.service"; +import * as Commands from "./index"; @Module({ - providers: [...Object.values(Commands), CommandsService], + imports: [...Object.values(Commands)], + providers: [CommandsService], }) export class CommandsModule {} diff --git a/src/modules/commands/Commands.service.ts b/src/modules/commands/Commands.service.ts old mode 100644 new mode 100755 index 50ecf661..cc95b5d2 --- a/src/modules/commands/Commands.service.ts +++ b/src/modules/commands/Commands.service.ts @@ -1,90 +1,117 @@ -import { CommandPermissions } from "@/common/decorators"; -import { Injectable, Logger, OnApplicationBootstrap } from "@nestjs/common"; +import { + CommandPermissions, + type CommandPermissionsOptions, +} from "@/common/decorators"; +import { + Injectable, + Logger, + type OnApplicationBootstrap, +} from "@nestjs/common"; import { ConfigService } from "@nestjs/config"; import { Reflector } from "@nestjs/core"; import { Client } from "discord.js"; -import { CommandsService as _CommandsService, SlashCommand, SlashCommandDiscovery, SlashCommandsService, Subcommand } from "necord"; -import { ExplorerService } from "necord/dist/necord-explorer.service"; -import { Config } from "../config/types"; -import { CommandPermissionsOptions } from "@/common/decorators/CommandPermissions.decorator"; +import { + ExplorerService, + SlashCommand, + type SlashCommandDiscovery, + SlashCommandsService, + Subcommand, + CommandsService as _CommandsService, +} from "necord"; @Injectable() export class CommandsService implements OnApplicationBootstrap { private readonly logger = new Logger(CommandsService.name); - constructor( + public constructor( private readonly slashCommandService: SlashCommandsService, private readonly explorerService: ExplorerService, - private readonly commandService: _CommandsService, + private readonly commandsService: _CommandsService, private readonly reflector: Reflector, private readonly configService: ConfigService, private readonly client: Client, ) {} - async onApplicationBootstrap() { - this.client.once("ready", async (client) => this.commandService.registerAllCommands()); - await this.updateMeta(); - } - - async updateMeta() { + public async onApplicationBootstrap() { this.logger.verbose("Updating metadata for SlashCommands | SubCommands"); - - this.updateSlashCommands(); - this.updateSubCommands(); - + this.client.once("ready", async (client) => + this.commandsService.registerAllCommands(), + ); + await this.updateSlashCommands(); + await this.updateSubCommands(); if (!this.client.isReady()) return; - await this.commandService.registerAllCommands(); + await this.commandsService.registerAllCommands(); } - private updateSlashCommands(): void { - const slashCommands = this.explorerService.explore(SlashCommand.KEY); - this.logger.verbose(`${slashCommands.length} SlashCommand (s) explored`); - for (const command of slashCommands) { - const perms: CommandPermissionsOptions = this.reflector.get(CommandPermissions.KEY, command.getHandler()); - this.slashCommandService.add(command); + private async updateSlashCommands(): Promise { + const slashCommands = this.explorerService.explore(SlashCommand.KEY); + this.logger.verbose(`${slashCommands.length} SlashCommand (s) explored`); + for (const command of slashCommands) { + this.slashCommandService.remove(command.getName()); + + const perms: CommandPermissionsOptions = this.reflector.get( + CommandPermissions.KEY, + command.getHandler(), + ); const guilds = []; if (perms.guildOnly) { - guilds.push(this.configService.getOrThrow("Discord").Servers.NDCommunity); + guilds.push( + this.configService.getOrThrow("Discord.Servers.NDCommunity"), + ); } if (perms.testOnly) { - guilds.push(this.configService.getOrThrow("Discord").Servers.TestGuild); + guilds.push( + this.configService.getOrThrow("Discord.Servers.TestGuild"), + ); + } + if (perms.guilds) { + guilds.push(perms.guilds.values()); } - if(perms.guilds) { - guilds.push(perms.guilds.values()) - } if (!guilds) return; - this.logger.verbose(`Updating metadata for SlashCommand : ${command.getName()}`); + this.logger.verbose( + `Updating metadata for SlashCommand : ${command.getName()}`, + ); - command["meta"]["guilds"] = guilds ?? []; + command.setGuilds(guilds ?? []); this.slashCommandService.add(command); } - } + } - private updateSubCommands(): void { - const subCommands = this.explorerService.explore(Subcommand.KEY); - this.logger.verbose(`${subCommands.length} SubCommand (s) explored`); - for (const command of subCommands) { - const perms: CommandPermissionsOptions = this.reflector.get(CommandPermissions.KEY, command.getHandler()); - this.slashCommandService.add(command); + private async updateSubCommands(): Promise { + const subCommands = this.explorerService.explore(Subcommand.KEY); + this.logger.verbose(`${subCommands.length} SubCommand (s) explored`); + for (const command of subCommands) { + this.slashCommandService.remove(command.getName()); + + const perms: CommandPermissionsOptions = this.reflector.get( + CommandPermissions.KEY, + command.getHandler(), + ); const guilds = []; if (perms.guildOnly) { - guilds.push(this.configService.getOrThrow("Discord").Servers.NDCommunity); + guilds.push( + this.configService.getOrThrow("Discord.Servers.NDCommunity"), + ); } if (perms.testOnly) { - guilds.push(this.configService.getOrThrow("Discord").Servers.TestGuild); + guilds.push( + this.configService.getOrThrow("Discord.Servers.TestGuild"), + ); + } + if (perms.guilds) { + guilds.push(perms.guilds.values()); } - if(perms.guilds) { - guilds.push(perms.guilds.values()) - } if (!guilds) return; - this.logger.verbose(`Updating metadata for SubCommand : ${command.getName()}`); + this.logger.verbose( + `Updating metadata for SubCommand : ${command.getName()}`, + ); - command["meta"]["guilds"] = guilds ?? []; + command.setGuilds(guilds ?? []); this.slashCommandService.addSubCommand(command); } - } + } } diff --git "a/src/modules/commands/\360\237\233\240\357\270\217 Developer Tools/commands/Hello.command.ts" "b/src/modules/commands/\360\237\233\240\357\270\217 Developer Tools/commands/Hello.command.ts" new file mode 100755 index 00000000..42a10664 --- /dev/null +++ "b/src/modules/commands/\360\237\233\240\357\270\217 Developer Tools/commands/Hello.command.ts" @@ -0,0 +1,36 @@ +import { CommandConfig, CommandPermissions } from "@/common/decorators/"; +import { CommandConfigGuard, CommandPermissionsGuard } from "@/common/guards"; +import { CurrentTranslate, type TranslationFn } from "@necord/localization"; +import { Injectable, Logger, UseGuards } from "@nestjs/common"; +import { + Ctx, + SlashCommand, + type SlashCommandContext, + Subcommand, +} from "necord"; +import { DeveloperToolsCommand } from "../DeveloperTools.decorator"; + +@DeveloperToolsCommand() +export class HelloCommand { + private readonly logger = new Logger(HelloCommand.name); + + @Subcommand({ + name: "hello_world", + description: "a simple hello", + }) + @CommandConfig({ category: "🛠️ Developer Tools", disable: false }) + @CommandPermissions({ + user: [], + bot: [], + guildOnly: false, + testOnly: false, + ownerOnly: true, + }) + @UseGuards(CommandConfigGuard, CommandPermissionsGuard) + public async onCommandRun( + @Ctx() [interaction]: SlashCommandContext, + @CurrentTranslate() t: TranslationFn, + ) { + interaction.reply("Hello, World!"); + } +} diff --git "a/src/modules/commands/\360\237\233\240\357\270\217 Developer Tools/commands/index.ts" "b/src/modules/commands/\360\237\233\240\357\270\217 Developer Tools/commands/index.ts" old mode 100644 new mode 100755 index 782c45f0..fa5acca4 --- "a/src/modules/commands/\360\237\233\240\357\270\217 Developer Tools/commands/index.ts" +++ "b/src/modules/commands/\360\237\233\240\357\270\217 Developer Tools/commands/index.ts" @@ -1,2 +1,3 @@ export * from "./test.command"; export * from "./eval/eval.command"; +export * from "./Hello.command"; diff --git "a/src/modules/commands/\360\237\233\240\357\270\217 Developer Tools/commands/test.command.ts" "b/src/modules/commands/\360\237\233\240\357\270\217 Developer Tools/commands/test.command.ts" index a821227b..38e6ddaf 100644 --- "a/src/modules/commands/\360\237\233\240\357\270\217 Developer Tools/commands/test.command.ts" +++ "b/src/modules/commands/\360\237\233\240\357\270\217 Developer Tools/commands/test.command.ts" @@ -1,6 +1,10 @@ import { CommandConfig, CommandPermissions } from "@/common/decorators/"; import { CommandConfigGuard, CommandPermissionsGuard } from "@/common/guards"; -import { CurrentTranslate, TranslationFn, localizationMapByKey } from "@necord/localization"; +import { + CurrentTranslate, + TranslationFn, + localizationMapByKey, +} from "@necord/localization"; import { Logger, UseGuards } from "@nestjs/common"; import { Ctx, SlashCommandContext, Subcommand } from "necord"; import { DeveloperToolsCommand } from "../DeveloperTools.decorator"; @@ -13,7 +17,9 @@ export class TestCommand { name: "test", description: "Command for Testing things", nameLocalizations: localizationMapByKey("DeveloperTools.test.name"), - descriptionLocalizations: localizationMapByKey("DeveloperTools.test.description"), + descriptionLocalizations: localizationMapByKey( + "DeveloperTools.test.description", + ), }) @CommandConfig({ category: "🛠️ Developer Tools", disable: false }) @CommandPermissions({ @@ -24,7 +30,10 @@ export class TestCommand { ownerOnly: true, }) @UseGuards(CommandConfigGuard, CommandPermissionsGuard) - public async onCommandRun(@Ctx() [interaction]: SlashCommandContext, @CurrentTranslate() t: TranslationFn) { + public async onCommandRun( + @Ctx() [interaction]: SlashCommandContext, + @CurrentTranslate() t: TranslationFn, + ) { interaction.reply(t("DeveloperTools.test.message")); } } diff --git a/src/modules/config/NecordConfig.service.ts b/src/modules/config/NecordConfig.service.ts index ca3f55a4..8a47d65b 100644 --- a/src/modules/config/NecordConfig.service.ts +++ b/src/modules/config/NecordConfig.service.ts @@ -1,6 +1,11 @@ import { Injectable } from "@nestjs/common"; import type { ConfigService } from "@nestjs/config"; -import { GatewayIntentBits, GatewayVersion, Options, Partials } from "discord.js"; +import { + GatewayIntentBits, + GatewayVersion, + Options, + Partials, +} from "discord.js"; import type { NecordModuleOptions } from "necord"; import { Config } from "./types"; diff --git a/src/modules/core/NDB.module.ts b/src/modules/core/NDB.module.ts index f9cec279..5a673b77 100644 --- a/src/modules/core/NDB.module.ts +++ b/src/modules/core/NDB.module.ts @@ -1,4 +1,8 @@ -import { GuildResolver, NecordLocalizationModule, NestedLocalizationAdapter } from "@necord/localization"; +import { + GuildResolver, + NecordLocalizationModule, + NestedLocalizationAdapter, +} from "@necord/localization"; import { NecordPaginationModule } from "@necord/pagination"; import { Module } from "@nestjs/common"; import { ConfigModule, ConfigService } from "@nestjs/config"; @@ -29,8 +33,11 @@ import { NDBServiceProvider } from "./provider/NDBService.provider"; inject: [ConfigService], useFactory: async (config: ConfigService) => ({ adapter: new NestedLocalizationAdapter({ - fallbackLocale: config.getOrThrow("FallbackLocale"), - locales: await new JSONLocaleLoader("./src/common/Languages/").loadTranslations(), + fallbackLocale: + config.getOrThrow("FallbackLocale"), + locales: await new JSONLocaleLoader( + "./src/common/Languages/", + ).loadTranslations(), }), resolvers: GuildResolver, }),