Skip to content

AWAS666/discord-nestjs

 
 

Repository files navigation

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.

Package License

👨🏻‍💻 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.

❓ 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

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.

How to inject dependencies into your command, pipe, guard or filter

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

About

👾 NestJS package for discord.js

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 99.6%
  • Other 0.4%