-
-
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.
now the CommandsService will update the metadata from all commands(slash|sub) using the `CommandsPermissionsDecorator` values to determine what command will be posted in specific guilds Thanks @wolffparkinson https://github.com/wolffparkinson/necord-playground/tree/dynamic-guilds
- Loading branch information
1 parent
5ede6cf
commit 5c72e16
Showing
22 changed files
with
117 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { Reflector } from "@nestjs/core"; | ||
import { PermissionResolvable } from "discord.js"; | ||
|
||
interface CommandPermissionsOptions { | ||
export interface CommandPermissionsOptions { | ||
user: PermissionResolvable[]; | ||
bot: PermissionResolvable[]; | ||
guildOnly?: boolean; | ||
ownerOnly?: boolean; | ||
guildOnly: boolean; | ||
testOnly: boolean; | ||
ownerOnly: boolean; | ||
guilds?: string[]; | ||
} | ||
|
||
export const CommandPermissions = Reflector.createDecorator<CommandPermissionsOptions>(); |
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,7 +1,8 @@ | ||
import { Module } from "@nestjs/common"; | ||
import * as Commands from "./index"; | ||
import { CommandsService } from "./Commands.service"; | ||
|
||
@Module({ | ||
imports: [...Object.values(Commands)], | ||
providers: [...Object.values(Commands), CommandsService], | ||
}) | ||
export class CommandsModule {} |
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,90 @@ | ||
import { CommandPermissions } from "@/common/decorators"; | ||
import { Injectable, Logger, 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"; | ||
|
||
@Injectable() | ||
export class CommandsService implements OnApplicationBootstrap { | ||
private readonly logger = new Logger(CommandsService.name); | ||
|
||
constructor( | ||
private readonly slashCommandService: SlashCommandsService, | ||
private readonly explorerService: ExplorerService<SlashCommandDiscovery>, | ||
private readonly commandService: _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() { | ||
this.logger.verbose("Updating metadata for SlashCommands | SubCommands"); | ||
|
||
this.updateSlashCommands(); | ||
this.updateSubCommands(); | ||
|
||
if (!this.client.isReady()) return; | ||
await this.commandService.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); | ||
const guilds = []; | ||
if (perms.guildOnly) { | ||
guilds.push(this.configService.getOrThrow<Config["Discord"]>("Discord").Servers.NDCommunity); | ||
} | ||
if (perms.testOnly) { | ||
guilds.push(this.configService.getOrThrow<Config["Discord"]>("Discord").Servers.TestGuild); | ||
} | ||
if(perms.guilds) { | ||
guilds.push(perms.guilds.values()) | ||
} | ||
|
||
if (!guilds) return; | ||
|
||
this.logger.verbose(`Updating metadata for SlashCommand : ${command.getName()}`); | ||
|
||
command["meta"]["guilds"] = 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); | ||
const guilds = []; | ||
if (perms.guildOnly) { | ||
guilds.push(this.configService.getOrThrow<Config["Discord"]>("Discord").Servers.NDCommunity); | ||
} | ||
if (perms.testOnly) { | ||
guilds.push(this.configService.getOrThrow<Config["Discord"]>("Discord").Servers.TestGuild); | ||
} | ||
if(perms.guilds) { | ||
guilds.push(perms.guilds.values()) | ||
} | ||
|
||
if (!guilds) return; | ||
|
||
this.logger.verbose(`Updating metadata for SubCommand : ${command.getName()}`); | ||
|
||
command["meta"]["guilds"] = guilds ?? []; | ||
this.slashCommandService.addSubCommand(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
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
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
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
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
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
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