0.1.5 • Published 1 year ago

nestjs-discord-transporter v0.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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

ParamRequiredDescription
tokentrueThe discord access token access here
intentsfalseThe bot intents access docs here
partialsfalseThe bot partials access docs here
prefixfalseif 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

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago