A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.
$ npm install @discord-nestjs/core discord.js
Or via yarn
$ yarn add @discord-nestjs/core discord.js
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 - Bot example with slash commands
- @discord-nestjs/sample-sub-command - Bot example with slash sub-commands and sub-groups
- @discord-nestjs/sample-validation - Bot example with slash commands validation
- @discord-nestjs/sample-event - Bot example with events
- @discord-nestjs/sample-dependency-injection - Bot example with dependency injection
- @discord-nestjs/sample-reaction-collector - Bot example with reaction collector
- @discord-nestjs/sample-message-collector - Bot example with message collector
- @discord-nestjs/sample-interaction-collector - Bot example with interaction collector
Click to expand
Check your intent is passed to the discordClientOptions
of the module. More info
I created DTO and added TransformPipe
, but when I receive response to the command, the DTO fields are missing
Click to expand
Set useDefineForClassFields
to true
in your tsconfig.json
.
Also check that the Palyoad
and UsePipes
decorators are imported from @discord-nestjs/core
.
Click to expand
Add the required providers to the extraProviders
option.
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],
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',
},
],
}),
extraProviders: [PlayService],
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