From a65f96c22435459e1048fc331cad193bccb2153e Mon Sep 17 00:00:00 2001 From: NedcloarBR Date: Thu, 30 Jan 2025 15:21:29 -0300 Subject: [PATCH 1/8] feat: add dynamic example --- 09-dynamic-guilds/.editorconfig | 19 ++++++++ 09-dynamic-guilds/.gitignore | 36 ++++++++++++++ 09-dynamic-guilds/.prettierrc | 8 +++ 09-dynamic-guilds/README.md | 3 ++ 09-dynamic-guilds/eslint.config.js | 46 ++++++++++++++++++ 09-dynamic-guilds/package.json | 37 ++++++++++++++ 09-dynamic-guilds/src/app.module.ts | 18 +++++++ 09-dynamic-guilds/src/app.service.ts | 11 +++++ 09-dynamic-guilds/src/command.service.ts | 62 ++++++++++++++++++++++++ 09-dynamic-guilds/src/dynamic.command.ts | 13 +++++ 09-dynamic-guilds/src/main.ts | 4 ++ 09-dynamic-guilds/src/ping.command.ts | 10 ++++ 09-dynamic-guilds/tsconfig.build.json | 5 ++ 09-dynamic-guilds/tsconfig.json | 18 +++++++ 14 files changed, 290 insertions(+) create mode 100644 09-dynamic-guilds/.editorconfig create mode 100644 09-dynamic-guilds/.gitignore create mode 100644 09-dynamic-guilds/.prettierrc create mode 100644 09-dynamic-guilds/README.md create mode 100644 09-dynamic-guilds/eslint.config.js create mode 100644 09-dynamic-guilds/package.json create mode 100644 09-dynamic-guilds/src/app.module.ts create mode 100644 09-dynamic-guilds/src/app.service.ts create mode 100644 09-dynamic-guilds/src/command.service.ts create mode 100644 09-dynamic-guilds/src/dynamic.command.ts create mode 100644 09-dynamic-guilds/src/main.ts create mode 100644 09-dynamic-guilds/src/ping.command.ts create mode 100644 09-dynamic-guilds/tsconfig.build.json create mode 100644 09-dynamic-guilds/tsconfig.json diff --git a/09-dynamic-guilds/.editorconfig b/09-dynamic-guilds/.editorconfig new file mode 100644 index 000000000..ea6cddee4 --- /dev/null +++ b/09-dynamic-guilds/.editorconfig @@ -0,0 +1,19 @@ +# editorconfig.org +root = true + +[*] +end_of_line = lf +indent_style = tab +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true +indent_size = 4 +max_line_length = 140 + +[*.{yaml, yml}] +indent_style = space +indent_size = 2 + +[*.md] +indent_style = space +trim_trailing_whitespace = false diff --git a/09-dynamic-guilds/.gitignore b/09-dynamic-guilds/.gitignore new file mode 100644 index 000000000..29ee43717 --- /dev/null +++ b/09-dynamic-guilds/.gitignore @@ -0,0 +1,36 @@ +# compiled output +/dist +/node_modules +yarn.lock +yarn-error.log + +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# OS +.DS_Store + +# Tests +/coverage +/.nyc_output + +# IDEs and editors +/.idea +.project +.classpath +.c9/ +*.launch +.settings/ +*.sublime-workspace + +# IDE - VSCode +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json diff --git a/09-dynamic-guilds/.prettierrc b/09-dynamic-guilds/.prettierrc new file mode 100644 index 000000000..415f2c457 --- /dev/null +++ b/09-dynamic-guilds/.prettierrc @@ -0,0 +1,8 @@ +{ + "parser": "typescript", + "trailingComma": "none", + "singleQuote": true, + "arrowParens": "avoid", + "endOfLine": "auto", + "printWidth": 120 +} diff --git a/09-dynamic-guilds/README.md b/09-dynamic-guilds/README.md new file mode 100644 index 000000000..9bfb7f13c --- /dev/null +++ b/09-dynamic-guilds/README.md @@ -0,0 +1,3 @@ +# 09 - Dynamic Guilds + +This is an updated version of an example created by [@WolffParkinson](https://github.com/wolffparkinson). You can find the original example [here](https://github.com/wolffparkinson/necord-playground/tree/dynamic-guilds) diff --git a/09-dynamic-guilds/eslint.config.js b/09-dynamic-guilds/eslint.config.js new file mode 100644 index 000000000..bbd28044d --- /dev/null +++ b/09-dynamic-guilds/eslint.config.js @@ -0,0 +1,46 @@ +const typescriptEslintEslintPlugin = require("@typescript-eslint/eslint-plugin"); +const globals = require("globals"); +const tsParser = require("@typescript-eslint/parser"); +const js = require("@eslint/js"); +const { FlatCompat } = require("@eslint/eslintrc"); + +const compat = new FlatCompat({ + baseDirectory: __dirname, + recommendedConfig: js.configs.recommended, + allConfig: js.configs.all +}); + +module.exports = [...compat.extends( + "plugin:@typescript-eslint/eslint-recommended", + "plugin:@typescript-eslint/recommended", + "prettier", +), { + plugins: { + "@typescript-eslint": typescriptEslintEslintPlugin, + }, + + languageOptions: { + globals: { + ...globals.node, + }, + + parser: tsParser, + ecmaVersion: 5, + sourceType: "module", + + parserOptions: { + project: "tsconfig.json", + }, + }, + + rules: { + "@typescript-eslint/interface-name-prefix": "off", + "@typescript-eslint/explicit-function-return-type": "off", + "@typescript-eslint/no-explicit-any": "off", + "@typescript-eslint/explicit-module-boundary-types": "off", + "@typescript-eslint/no-unused-vars": "off", + "@typescript-eslint/ban-types": "off", + }, + + ignores: ["*.config.js"] +}]; \ No newline at end of file diff --git a/09-dynamic-guilds/package.json b/09-dynamic-guilds/package.json new file mode 100644 index 000000000..55ae6fb31 --- /dev/null +++ b/09-dynamic-guilds/package.json @@ -0,0 +1,37 @@ +{ + "name": "09-dynamic-guilds", + "version": "1.0.0", + "license": "MIT", + "scripts": { + "prebuild": "rimraf dist", + "build": "nest build", + "format": "prettier --write \"src/**/*.ts\"", + "start": "nest start", + "start:dev": "nest start --watch", + "start:debug": "nest start --debug --watch", + "start:prod": "node dist/main", + "lint": "eslint src/**/*.ts --fix" + }, + "dependencies": { + "@nestjs/common": "10.2.0", + "@nestjs/core": "10.2.0", + "discord.js": "14.17.3", + "necord": "6.8.7", + "reflect-metadata": "0.2.1", + "rimraf": "4.1.2", + "rxjs": "7.8.0" + }, + "devDependencies": { + "@eslint/eslintrc": "^3.1.0", + "@eslint/js": "^9.10.0", + "@types/node": "18.14.2", + "@typescript-eslint/eslint-plugin": "5.54.0", + "@typescript-eslint/parser": "5.54.0", + "eslint": "9.10.0", + "eslint-config-prettier": "8.6.0", + "eslint-plugin-prettier": "4.2.1", + "globals": "^15.9.0", + "prettier": "2.8.4", + "typescript": "5.7.3" + } +} diff --git a/09-dynamic-guilds/src/app.module.ts b/09-dynamic-guilds/src/app.module.ts new file mode 100644 index 000000000..3820bcd56 --- /dev/null +++ b/09-dynamic-guilds/src/app.module.ts @@ -0,0 +1,18 @@ +import { Module } from '@nestjs/common'; +import { NecordModule } from 'necord'; +import { CommandService } from './command.service'; +import { DynamicCommand } from './dynamic.command'; +import { AppService } from './app.service'; +import { PingCommand } from './ping.command'; + +@Module({ + imports: [ + NecordModule.forRoot({ + intents: ['Guilds'], + token: process.env.DISCORD_TOKEN, + skipRegistration: true, + }), + ], + providers: [CommandService, AppService, DynamicCommand, PingCommand], +}) +export class AppModule {} diff --git a/09-dynamic-guilds/src/app.service.ts b/09-dynamic-guilds/src/app.service.ts new file mode 100644 index 000000000..7da57caf7 --- /dev/null +++ b/09-dynamic-guilds/src/app.service.ts @@ -0,0 +1,11 @@ +import { Injectable, Logger } from '@nestjs/common'; +import { ContextOf, Ctx, Once } from 'necord'; + +@Injectable() +export class AppService { + private readonly logger = new Logger(AppService.name); + @Once('ready') + public onReady(@Ctx() [client]: ContextOf<'ready'>) { + this.logger.log(`Bot is ready! Logged in as ${client.user.tag}`); + } +} diff --git a/09-dynamic-guilds/src/command.service.ts b/09-dynamic-guilds/src/command.service.ts new file mode 100644 index 000000000..3becde76e --- /dev/null +++ b/09-dynamic-guilds/src/command.service.ts @@ -0,0 +1,62 @@ +import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common'; +import { + CommandsService, + ExplorerService, + SlashCommandDiscovery, + SlashCommandsService, + SlashCommand, +} from 'necord'; +import { Client } from 'discord.js'; + +@Injectable() +export class CommandService implements OnApplicationBootstrap { + private readonly logger = new Logger(); + + constructor( + private readonly slashCommandService: SlashCommandsService, + private readonly explorerService: ExplorerService, + private readonly commandService: CommandsService, + private readonly client: Client + ) {} + + async onApplicationBootstrap() { + this.client.once('ready', async () => + await this.commandService.registerAllCommands() + ); + await this.updateMeta(); + if (!this.client.isReady()) return; + await this.commandService.registerAllCommands(); + } + + // Fetch guild ids from API + async fetchGuildIds() { + return [{ id: 1, name: 'dynamic', guildIds: [process.env.DB_GUILD_ID] }]; + } + + async updateMeta() { + this.logger.verbose('Updating metadata for SlashCommands'); + + const slashCommands = this.explorerService.explore(SlashCommand.KEY); + this.logger.verbose(`${slashCommands.length} SlashCommand (s) explored`); + + const db = await this.fetchGuildIds(); + slashCommands.forEach((command) => { + this.slashCommandService.remove(command.getName()); + const data = db.find((d) => d.name === command.getName()); + if (!data) { + this.logger.warn( + `No metadata found for SlashCommand : ${command.getName()}` + ); + this.slashCommandService.add(command); + return; + } + + this.logger.verbose( + `Updating metadata for SlashCommand : ${command.getName()}` + ); + + command.setGuilds(data.guildIds ?? []); + this.slashCommandService.add(command); + }); + } +} diff --git a/09-dynamic-guilds/src/dynamic.command.ts b/09-dynamic-guilds/src/dynamic.command.ts new file mode 100644 index 000000000..6bfe393f7 --- /dev/null +++ b/09-dynamic-guilds/src/dynamic.command.ts @@ -0,0 +1,13 @@ +import { Injectable } from '@nestjs/common'; +import { Ctx, SlashCommand, SlashCommandContext } from 'necord'; + +@Injectable() +export class DynamicCommand { + @SlashCommand({ + name: 'dynamic', + description: 'This is a dynamic command', + }) + async run(@Ctx() [i]: SlashCommandContext) { + return i.reply({ ephemeral: true, content: 'I am so dynamic !! 😎' }); + } +} diff --git a/09-dynamic-guilds/src/main.ts b/09-dynamic-guilds/src/main.ts new file mode 100644 index 000000000..5627acd50 --- /dev/null +++ b/09-dynamic-guilds/src/main.ts @@ -0,0 +1,4 @@ +import { NestFactory } from '@nestjs/core'; +import { AppModule } from './app.module'; + +NestFactory.createApplicationContext(AppModule); diff --git a/09-dynamic-guilds/src/ping.command.ts b/09-dynamic-guilds/src/ping.command.ts new file mode 100644 index 000000000..98229b83a --- /dev/null +++ b/09-dynamic-guilds/src/ping.command.ts @@ -0,0 +1,10 @@ +import { Injectable } from '@nestjs/common'; +import { Ctx, SlashCommand, SlashCommandContext } from 'necord'; + +@Injectable() +export class PingCommand { + @SlashCommand({ name: 'ping', description: 'Bot status' }) + async run(@Ctx() [i]: SlashCommandContext) { + return i.reply({ ephemeral: true, content: 'Pong !' }); + } +} diff --git a/09-dynamic-guilds/tsconfig.build.json b/09-dynamic-guilds/tsconfig.build.json new file mode 100644 index 000000000..c151bb5dd --- /dev/null +++ b/09-dynamic-guilds/tsconfig.build.json @@ -0,0 +1,5 @@ +{ + "extends": "./tsconfig.json", + "exclude": ["node_modules", "dist", "test", "**/*spec.ts"] +} + \ No newline at end of file diff --git a/09-dynamic-guilds/tsconfig.json b/09-dynamic-guilds/tsconfig.json new file mode 100644 index 000000000..1939a73a0 --- /dev/null +++ b/09-dynamic-guilds/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "module": "commonjs", + "declaration": true, + "removeComments": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "allowSyntheticDefaultImports": true, + "target": "ES2021", + "sourceMap": true, + "outDir": "./dist", + "baseUrl": "./", + "incremental": true, + "skipLibCheck": true + }, + "include": ["src/**/*"] +} + \ No newline at end of file From 237349b53b6e44ad5634efe80129c8504831a3c0 Mon Sep 17 00:00:00 2001 From: Alexey Filippov Date: Thu, 30 Jan 2025 21:52:41 +0300 Subject: [PATCH 2/8] Update 09-dynamic-guilds/src/app.service.ts --- 09-dynamic-guilds/src/app.service.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/09-dynamic-guilds/src/app.service.ts b/09-dynamic-guilds/src/app.service.ts index 7da57caf7..83e9c51c4 100644 --- a/09-dynamic-guilds/src/app.service.ts +++ b/09-dynamic-guilds/src/app.service.ts @@ -4,6 +4,7 @@ import { ContextOf, Ctx, Once } from 'necord'; @Injectable() export class AppService { private readonly logger = new Logger(AppService.name); + @Once('ready') public onReady(@Ctx() [client]: ContextOf<'ready'>) { this.logger.log(`Bot is ready! Logged in as ${client.user.tag}`); From b9008d07f083d3f5f015a1c14515fa32aa528167 Mon Sep 17 00:00:00 2001 From: Miguel Alexandre Uhlein Date: Thu, 30 Jan 2025 15:54:15 -0300 Subject: [PATCH 3/8] Update 09-dynamic-guilds/src/command.service.ts Co-authored-by: Alexey Filippov --- 09-dynamic-guilds/src/command.service.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/09-dynamic-guilds/src/command.service.ts b/09-dynamic-guilds/src/command.service.ts index 3becde76e..2dc26f487 100644 --- a/09-dynamic-guilds/src/command.service.ts +++ b/09-dynamic-guilds/src/command.service.ts @@ -20,9 +20,7 @@ export class CommandService implements OnApplicationBootstrap { ) {} async onApplicationBootstrap() { - this.client.once('ready', async () => - await this.commandService.registerAllCommands() - ); + this.client.once('ready', () => this.commandService.registerAllCommands()); await this.updateMeta(); if (!this.client.isReady()) return; await this.commandService.registerAllCommands(); From 9c5efd674f522e2142484e88fe2f7dceaa49367d Mon Sep 17 00:00:00 2001 From: Miguel Alexandre Uhlein Date: Thu, 30 Jan 2025 15:57:10 -0300 Subject: [PATCH 4/8] Update 09-dynamic-guilds/src/command.service.ts Co-authored-by: Alexey Filippov --- 09-dynamic-guilds/src/command.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/09-dynamic-guilds/src/command.service.ts b/09-dynamic-guilds/src/command.service.ts index 2dc26f487..6ae80d53a 100644 --- a/09-dynamic-guilds/src/command.service.ts +++ b/09-dynamic-guilds/src/command.service.ts @@ -38,7 +38,7 @@ export class CommandService implements OnApplicationBootstrap { this.logger.verbose(`${slashCommands.length} SlashCommand (s) explored`); const db = await this.fetchGuildIds(); - slashCommands.forEach((command) => { +for (const command of slashCommands) { this.slashCommandService.remove(command.getName()); const data = db.find((d) => d.name === command.getName()); if (!data) { From 49999350e7d7877fe857ef3266390ff2a98424bd Mon Sep 17 00:00:00 2001 From: Miguel Alexandre Uhlein Date: Thu, 30 Jan 2025 15:57:17 -0300 Subject: [PATCH 5/8] Update 09-dynamic-guilds/src/main.ts Co-authored-by: Alexey Filippov --- 09-dynamic-guilds/src/main.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/09-dynamic-guilds/src/main.ts b/09-dynamic-guilds/src/main.ts index 5627acd50..a3a900493 100644 --- a/09-dynamic-guilds/src/main.ts +++ b/09-dynamic-guilds/src/main.ts @@ -1,4 +1,9 @@ import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; -NestFactory.createApplicationContext(AppModule); +async bootstrap() { + const app = NestFactory.createApplicationContext(AppModule); + await app.init(); +} + +bootstrap(); From 26cc3a34e2f46c395d0b5739078fe12d16dbc9e9 Mon Sep 17 00:00:00 2001 From: NedcloarBR Date: Thu, 30 Jan 2025 16:10:17 -0300 Subject: [PATCH 6/8] style: lint --- 09-dynamic-guilds/src/app.module.ts | 16 ++-- 09-dynamic-guilds/src/command.service.ts | 98 +++++++++++------------- 09-dynamic-guilds/src/dynamic.command.ts | 14 ++-- 09-dynamic-guilds/src/main.ts | 6 +- 09-dynamic-guilds/src/ping.command.ts | 8 +- 5 files changed, 66 insertions(+), 76 deletions(-) diff --git a/09-dynamic-guilds/src/app.module.ts b/09-dynamic-guilds/src/app.module.ts index 3820bcd56..e0ab50430 100644 --- a/09-dynamic-guilds/src/app.module.ts +++ b/09-dynamic-guilds/src/app.module.ts @@ -6,13 +6,13 @@ import { AppService } from './app.service'; import { PingCommand } from './ping.command'; @Module({ - imports: [ - NecordModule.forRoot({ - intents: ['Guilds'], - token: process.env.DISCORD_TOKEN, - skipRegistration: true, - }), - ], - providers: [CommandService, AppService, DynamicCommand, PingCommand], + imports: [ + NecordModule.forRoot({ + intents: ['Guilds'], + token: process.env.DISCORD_TOKEN, + skipRegistration: true + }) + ], + providers: [CommandService, AppService, DynamicCommand, PingCommand] }) export class AppModule {} diff --git a/09-dynamic-guilds/src/command.service.ts b/09-dynamic-guilds/src/command.service.ts index 6ae80d53a..f28709ae4 100644 --- a/09-dynamic-guilds/src/command.service.ts +++ b/09-dynamic-guilds/src/command.service.ts @@ -1,60 +1,50 @@ import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common'; -import { - CommandsService, - ExplorerService, - SlashCommandDiscovery, - SlashCommandsService, - SlashCommand, -} from 'necord'; +import { CommandsService, ExplorerService, SlashCommandDiscovery, SlashCommandsService, SlashCommand } from 'necord'; import { Client } from 'discord.js'; @Injectable() export class CommandService implements OnApplicationBootstrap { - private readonly logger = new Logger(); - - constructor( - private readonly slashCommandService: SlashCommandsService, - private readonly explorerService: ExplorerService, - private readonly commandService: CommandsService, - private readonly client: Client - ) {} - - async onApplicationBootstrap() { - this.client.once('ready', () => this.commandService.registerAllCommands()); - await this.updateMeta(); - if (!this.client.isReady()) return; - await this.commandService.registerAllCommands(); - } - - // Fetch guild ids from API - async fetchGuildIds() { - return [{ id: 1, name: 'dynamic', guildIds: [process.env.DB_GUILD_ID] }]; - } - - async updateMeta() { - this.logger.verbose('Updating metadata for SlashCommands'); - - const slashCommands = this.explorerService.explore(SlashCommand.KEY); - this.logger.verbose(`${slashCommands.length} SlashCommand (s) explored`); - - const db = await this.fetchGuildIds(); -for (const command of slashCommands) { - this.slashCommandService.remove(command.getName()); - const data = db.find((d) => d.name === command.getName()); - if (!data) { - this.logger.warn( - `No metadata found for SlashCommand : ${command.getName()}` - ); - this.slashCommandService.add(command); - return; - } - - this.logger.verbose( - `Updating metadata for SlashCommand : ${command.getName()}` - ); - - command.setGuilds(data.guildIds ?? []); - this.slashCommandService.add(command); - }); - } + private readonly logger = new Logger(); + + constructor( + private readonly slashCommandService: SlashCommandsService, + private readonly explorerService: ExplorerService, + private readonly commandService: CommandsService, + private readonly client: Client + ) {} + + async onApplicationBootstrap() { + this.client.once('ready', () => this.commandService.registerAllCommands()); + await this.updateMeta(); + if (!this.client.isReady()) return; + await this.commandService.registerAllCommands(); + } + + // Fetch guild ids from API + async fetchGuildIds() { + return [{ id: 1, name: 'dynamic', guildIds: [process.env.DB_GUILD_ID] }]; + } + + async updateMeta() { + this.logger.verbose('Updating metadata for SlashCommands'); + + const slashCommands = this.explorerService.explore(SlashCommand.KEY); + this.logger.verbose(`${slashCommands.length} SlashCommand (s) explored`); + + const db = await this.fetchGuildIds(); + for (const command of slashCommands) { + this.slashCommandService.remove(command.getName()); + const data = db.find(d => d.name === command.getName()); + if (!data) { + this.logger.warn(`No metadata found for SlashCommand : ${command.getName()}`); + this.slashCommandService.add(command); + return; + } + + this.logger.verbose(`Updating metadata for SlashCommand : ${command.getName()}`); + + command.setGuilds(data.guildIds ?? []); + this.slashCommandService.add(command); + } + } } diff --git a/09-dynamic-guilds/src/dynamic.command.ts b/09-dynamic-guilds/src/dynamic.command.ts index 6bfe393f7..8661e3f39 100644 --- a/09-dynamic-guilds/src/dynamic.command.ts +++ b/09-dynamic-guilds/src/dynamic.command.ts @@ -3,11 +3,11 @@ import { Ctx, SlashCommand, SlashCommandContext } from 'necord'; @Injectable() export class DynamicCommand { - @SlashCommand({ - name: 'dynamic', - description: 'This is a dynamic command', - }) - async run(@Ctx() [i]: SlashCommandContext) { - return i.reply({ ephemeral: true, content: 'I am so dynamic !! 😎' }); - } + @SlashCommand({ + name: 'dynamic', + description: 'This is a dynamic command' + }) + async run(@Ctx() [i]: SlashCommandContext) { + return i.reply({ ephemeral: true, content: 'I am so dynamic !! 😎' }); + } } diff --git a/09-dynamic-guilds/src/main.ts b/09-dynamic-guilds/src/main.ts index a3a900493..c1cd26f2f 100644 --- a/09-dynamic-guilds/src/main.ts +++ b/09-dynamic-guilds/src/main.ts @@ -1,9 +1,9 @@ import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; -async bootstrap() { - const app = NestFactory.createApplicationContext(AppModule); - await app.init(); +async function bootstrap() { + const app = await NestFactory.createApplicationContext(AppModule); + await app.init(); } bootstrap(); diff --git a/09-dynamic-guilds/src/ping.command.ts b/09-dynamic-guilds/src/ping.command.ts index 98229b83a..3e679dae5 100644 --- a/09-dynamic-guilds/src/ping.command.ts +++ b/09-dynamic-guilds/src/ping.command.ts @@ -3,8 +3,8 @@ import { Ctx, SlashCommand, SlashCommandContext } from 'necord'; @Injectable() export class PingCommand { - @SlashCommand({ name: 'ping', description: 'Bot status' }) - async run(@Ctx() [i]: SlashCommandContext) { - return i.reply({ ephemeral: true, content: 'Pong !' }); - } + @SlashCommand({ name: 'ping', description: 'Bot status' }) + async run(@Ctx() [i]: SlashCommandContext) { + return i.reply({ ephemeral: true, content: 'Pong !' }); + } } From bb2f41600ccbaac5515e011f77777bbc445ac362 Mon Sep 17 00:00:00 2001 From: NedcloarBR Date: Thu, 30 Jan 2025 16:31:18 -0300 Subject: [PATCH 7/8] feat: rename file `ping.*` -> `simple.*` --- 09-dynamic-guilds/src/app.module.ts | 4 ++-- 09-dynamic-guilds/src/{ping.command.ts => simple.command.ts} | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename 09-dynamic-guilds/src/{ping.command.ts => simple.command.ts} (91%) diff --git a/09-dynamic-guilds/src/app.module.ts b/09-dynamic-guilds/src/app.module.ts index e0ab50430..9bce5b4d0 100644 --- a/09-dynamic-guilds/src/app.module.ts +++ b/09-dynamic-guilds/src/app.module.ts @@ -3,7 +3,7 @@ import { NecordModule } from 'necord'; import { CommandService } from './command.service'; import { DynamicCommand } from './dynamic.command'; import { AppService } from './app.service'; -import { PingCommand } from './ping.command'; +import { SimpleCommand } from './simple.command'; @Module({ imports: [ @@ -13,6 +13,6 @@ import { PingCommand } from './ping.command'; skipRegistration: true }) ], - providers: [CommandService, AppService, DynamicCommand, PingCommand] + providers: [CommandService, AppService, DynamicCommand, SimpleCommand] }) export class AppModule {} diff --git a/09-dynamic-guilds/src/ping.command.ts b/09-dynamic-guilds/src/simple.command.ts similarity index 91% rename from 09-dynamic-guilds/src/ping.command.ts rename to 09-dynamic-guilds/src/simple.command.ts index 3e679dae5..08e8613f1 100644 --- a/09-dynamic-guilds/src/ping.command.ts +++ b/09-dynamic-guilds/src/simple.command.ts @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common'; import { Ctx, SlashCommand, SlashCommandContext } from 'necord'; @Injectable() -export class PingCommand { +export class SimpleCommand { @SlashCommand({ name: 'ping', description: 'Bot status' }) async run(@Ctx() [i]: SlashCommandContext) { return i.reply({ ephemeral: true, content: 'Pong !' }); From 1b5e0f9ccf8805e8f7b5f7850cdccbc0a8e677f3 Mon Sep 17 00:00:00 2001 From: Alexey Filippov Date: Thu, 30 Jan 2025 22:32:16 +0300 Subject: [PATCH 8/8] Update 09-dynamic-guilds/src/command.service.ts --- 09-dynamic-guilds/src/command.service.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/09-dynamic-guilds/src/command.service.ts b/09-dynamic-guilds/src/command.service.ts index f28709ae4..629d687a0 100644 --- a/09-dynamic-guilds/src/command.service.ts +++ b/09-dynamic-guilds/src/command.service.ts @@ -14,10 +14,10 @@ export class CommandService implements OnApplicationBootstrap { ) {} async onApplicationBootstrap() { - this.client.once('ready', () => this.commandService.registerAllCommands()); - await this.updateMeta(); - if (!this.client.isReady()) return; - await this.commandService.registerAllCommands(); + this.client.once('ready', async () => { + await this.updateCommandsMeta(); + await this.commandService.registerAllCommands() + }); } // Fetch guild ids from API @@ -25,7 +25,7 @@ export class CommandService implements OnApplicationBootstrap { return [{ id: 1, name: 'dynamic', guildIds: [process.env.DB_GUILD_ID] }]; } - async updateMeta() { + async updateCommandsMeta() { this.logger.verbose('Updating metadata for SlashCommands'); const slashCommands = this.explorerService.explore(SlashCommand.KEY);