npm.io
0.1.5 • Published 3 years ago

nestjs-discord-transporter

Licence
MIT
Version
0.1.5
Deps
0
Size
29 kB
Vulns
0
Weekly
0

damiaoterto - nestjs-discord-transporter GitHub tag License stars - nestjs-discord-transporter forks - nestjs-discord-transporter

Description

A Discord event listener for Nest.js based on discord.js package.

Installation

Warning Assuming you have installed the @nestjs/microservices package.

Using NPM:

npm install discord.js nestjs-discord-transporter

Using Yarn:

yarn add discord.js nestjs-discord-transporter

Quick Start

Overview

To use the Discord transporter, pass the following options object to the createMicroservice() method on main.ts file:

import { NestFactory } from '@nestjs/core';
import { MicroserviceOptions } from '@nestjs/microservices';
import { DiscordTransporter } from 'nestjs-discord-transporter';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(
    AppModule,
    {
      strategy: new DiscordTransporter({
        token: '<you-token>', // or set the environment var DISCORD_TOKEN
        intents: [], // optional param
        partials: [], // optional param
        prefix: 'command:', // optional if receive commands
      }),
    },
  );
  await app.listen();
}
bootstrap();

Transporter options

Param Required Description
token true The discord access token access here
intents false The bot intents access docs here
partials false The bot partials access docs here
prefix false if receive specific commands
Request-response

To create a message handler based on the request-response paradigm use the @MessagePattern() decorator, which is imported from the @nestjs/microservices package. This decorator should be used only within the controller classes since they are the entry points for your application. Using them inside providers won't have any effect as they are simply ignored by Nest runtime.

app.controller.ts

import { Controller } from '@nestjs/common';
import { Message } from 'discord.js';
import { MessagePattern } from '@nestjs/microservices';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @MessagePattern('messageCreate')
  async getHello(message: Message) {
    message.channel.sendTyping();
    const response = await this.appService.handleMessage(message.content);
    message.channel.send(response);
  }
}

for more information on the message object click here

Dependencies

discord.js: Documentation

Nest.js: Documentation

Keywords