nestjs-discord-framework

Simple Framework to help integrate Discord bots with Nestjs Servers

Usage no npm install needed!

<script type="module">
  import nestjsDiscordFramework from 'https://cdn.skypack.dev/nestjs-discord-framework';
</script>

README

NestJS Bot Platform

Nest LogoplusDiscordJs Logo

A NestJS service wrapper for Discord.js!

Installation

$ npm i --save nestjs-discord-framework

Usage

Version 1.0.0 is not supported and should be upgraded to v2.x immediately.

To use the Framework just import the BotModule in the root AppModule and supply the options. The forRoot() method supports all the Discord.JS ClientOptions. In addition there are some extra configuration properties described below:

Key Description
token The Discord Bot accesstoken
prefix The command prefix (optional)
BotModule.forRoot(options:BotOptions),

Asynchronous loading of the options is also possible. One approach is to use a factory function:

BotModule.forRootAsync({
  useFactory: (): BotOptions => ({
    token: 'your-token',
    prefix: '!',
  }),
});

The factory behaves like any other asynchronous provider (e.g., it can be async and it's able to inject dependencies through inject):

BotModule.forRootAsync({
  imports: [ConfigModule],
  useFactory: (configService: ConfigService): BotOptions => ({
    token: configService.get('bot-token'),
    prefix: configService.get('bot-prefix'),
  }),
  inject: [ConfigService],
});

Alternatively, you can use the useClass or useExisting syntax:

BotModule.forRootAsync({
  useClass: BotConfigService,
});

Registering Events

Make sure that all classes using the @BotEvent() decorator are registered as providers

You can subscribe to Discord events by annotating a public method of a provider with the @BotEvent(event:string) Decorator. The Parameter is the name of the event you want to listen to. You can find a full list of Events here

@Injectable()
export class EventHandler {
  @BotEvent('guildMemberAdd')
  onGuildMemberAdd(...args: any[]): void {}
}

Registering Commands

Make sure that all classes using the @BotCommand() decorator are registered as providers

To register a command annotate a public provider method with the @BotCommand(name:string) Decorator. The Parameter is the first index of the message content split by " " without the prefix.

export class HelpCommand {
  @BotCommand('help')
  help(...args: any[]): void {}
}

Client-/Instance-Injection

You can inject the Client and the instance (BotInstance) into your providers and controllers. Simply use the @InjectBotClient() and @InjectBotInstance() Decorators in the constructor:

export class InjectClient {
  constructor(@InjectBotClient() private client) {}
}

export class InjectInstance {
  constructor(@InjectBotInstance() private instance) {}
}