Skip to content

Commit

Permalink
fix(commands): registration
Browse files Browse the repository at this point in the history
  • Loading branch information
NedcloarBR committed Sep 1, 2024
1 parent 4d4b4b1 commit 71ef797
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/common/decorators/CommandConfig.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Reflector } from "@nestjs/core";

interface CommandConfigOptions {
export interface CommandConfigOptions {
category: string;
disable: boolean;
}
Expand Down
7 changes: 5 additions & 2 deletions src/common/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -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";
5 changes: 3 additions & 2 deletions src/modules/commands/Commands.module.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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 {}
121 changes: 74 additions & 47 deletions src/modules/commands/Commands.service.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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<SlashCommandDiscovery>,
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<void> {
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<Config["Discord"]>("Discord").Servers.NDCommunity);
guilds.push(
this.configService.getOrThrow<string>("Discord.Servers.NDCommunity"),
);
}
if (perms.testOnly) {
guilds.push(this.configService.getOrThrow<Config["Discord"]>("Discord").Servers.TestGuild);
guilds.push(
this.configService.getOrThrow<string>("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<void> {
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<Config["Discord"]>("Discord").Servers.NDCommunity);
guilds.push(
this.configService.getOrThrow<string>("Discord.Servers.NDCommunity"),
);
}
if (perms.testOnly) {
guilds.push(this.configService.getOrThrow<Config["Discord"]>("Discord").Servers.TestGuild);
guilds.push(
this.configService.getOrThrow<string>("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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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!");
}
}
1 change: 1 addition & 0 deletions src/modules/commands/🛠️ Developer Tools/commands/index.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./test.command";
export * from "./eval/eval.command";
export * from "./Hello.command";
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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({
Expand All @@ -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"));
}
}
7 changes: 6 additions & 1 deletion src/modules/config/NecordConfig.service.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
13 changes: 10 additions & 3 deletions src/modules/core/NDB.module.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -29,8 +33,11 @@ import { NDBServiceProvider } from "./provider/NDBService.provider";
inject: [ConfigService],
useFactory: async (config: ConfigService) => ({
adapter: new NestedLocalizationAdapter({
fallbackLocale: config.getOrThrow<Config["FallbackLocale"]>("FallbackLocale"),
locales: await new JSONLocaleLoader("./src/common/Languages/").loadTranslations(),
fallbackLocale:
config.getOrThrow<Config["FallbackLocale"]>("FallbackLocale"),
locales: await new JSONLocaleLoader(
"./src/common/Languages/",
).loadTranslations(),
}),
resolvers: GuildResolver,
}),
Expand Down

0 comments on commit 71ef797

Please sign in to comment.