A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.
👨🏻💻 Installation
$ npm install @discord-nestjs/core discord.js
Or via yarn
$ yarn add @discord-nestjs/core discord.js
🧾 Description
NestJS package for discord.js
This monorepo consists of several packages.
- @discord-nestjs/core - Main package containing decorators, basic types and module declaration.
- @discord-nestjs/common - Contains optional common templates. For example TransformPipe or ValidationPipe.
- Samples
- @discord-nestjs/sample-command - Example of bot with slash commands
- @discord-nestjs/sample-sub-command - Example of bot with slash sub-commands and sub-groups
- @discord-nestjs/sample-validation - Example of bot with slash commands validation
- @discord-nestjs/sample-event - Example of bot with events
❓ Answers on questions
The bot starts up, but the slash commands and events do not work
Click to expand
Check your intent is passed to the discordClientOptions
of the module. More info
TransformPipe
, but when I receive response to the command, the DTO fields are missing
I created DTO and added Click to expand
Check what your target
version from tsconfig.json
. Now is the minimal version of ESNext
.
Also check that the Palyoad
and UsePipes
decorators are imported from @discord-nestjs/core
.
How to inject dependencies into your command, pipe, guard or filter
Click to expand
At the moment, this is only possible if the module is declared using forRootAsync
.
First, you need to declare a separate module and set the necessary providers, as well as set them for export.
import { PlayService } from './play.serivce';
import { Module } from '@nestjs/common';
@Module({
providers: [PlayService],
exports: [PlayService],
})
export class PlayModule {}
And then add this module to the DiscordModule
imports
import { PlayModule } from './services/play.module';
import { DiscordModule } from '@discord-nestjs/core';
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { Intents, Message } from 'discord.js';
@Module({
imports: [
DiscordModule.forRootAsync({
imports: [ConfigModule, PlayModule],
useFactory: (configService: ConfigService) => ({
token: configService.get('TOKEN'),
commands: ['**/*.command.js'],
discordClientOptions: {
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
},
registerCommandOptions: [
{
forGuild: configService.get('GUILD_ID_WITH_COMMANDS'),
allowFactory: (message: Message) =>
!message.author.bot && message.content === '!deploy',
},
],
}),
inject: [ConfigService],
}),
],
})
export class BotModule {}
And then you can inject your dependencies
import { PlayService } from '../services/play.serivce';
import { Command } from '@discord-nestjs/core';
import { DiscordCommand } from '@discord-nestjs/core/src';
import { CommandInteraction } from 'discord.js';
@Command({
name: 'play',
description: 'Plays a song',
})
export class PlayCommand implements DiscordCommand {
constructor(private readonly playService: PlayService) {}
handler(interaction: CommandInteraction): string {
return this.playService.play();
}
}
Any questions or suggestions? Discord Федок#3051