-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d4b4b1
commit 71ef797
Showing
9 changed files
with
148 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
121
src/modules/commands/Commands.service.ts
100644 → 100755
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/modules/commands/🛠️ Developer Tools/commands/Hello.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
1
src/modules/commands/🛠️ Developer Tools/commands/index.ts
100644 → 100755
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters