npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

discord-nestjs-xzyv

v1.0.5

Published

NestJS package for discord.js

Readme

🧾 Description

NestJS package for discord.js

👨🏻‍💻 Installation

$ npm install discord-nestjs discord.js

OR

$ yarn add discord-nestjs discord.js

📑 Overview

You can use forRoot or forRootAsync to configure your module

  • token * - your discord bot token see
  • commandPrefix * - global prefix for command events
  • allowGuilds - list of Guild IDs that the bot is allowed to work with
  • denyGuilds - list of Guild IDs that the bot is not allowed to work with
  • allowChannels - linking commands to a channel (can also be set through a decorator)
    • commandName * - command name
    • channels * - channel ID list
  • webhook - connecting with webhook
    • webhookId * - webhook id
    • webhookToken * - webhook token
  • you can also set all options as for the client from the "discord.js" library

💡 Example

/*bot.module.ts*/

@Module({
  imports: [
    DiscordModule.forRoot({
      token: 'Njg2MzI2OTMwNTg4NTY1NTQx.XmVlww.EF_bMXRvYgMUCQhg_jYnieoBW-k',
      commandPrefix: '!',
      allowGuilds: ['745366351929016363'],
      denyGuilds: ['520622812742811698'],
      allowChannels: [
        {
          commandName: 'some',
          channels: ['745366352386326572'],
        },
      ],
      webhook: {
        webhookId: 'your_webhook_id',
        webhookToken: 'your_webhook_token',
      },
      // and other discord options
    }),
  ],
  providers: [BotGateway],
})
export class BotModule {}

Or async

/*bot.module.ts*/

@Module({
  imports: [
    DiscordModule.forRootAsync({
      useFactory: () => ({
        token: 'Njg2MzI2OTMwNTg4NTY1NTQx.XmVlww.EF_bMXRvYgMUCQhg_jYnieoBW-k',
        commandPrefix: '!',
        allowGuilds: ['745366351929016363'],
        denyGuilds: ['520622812742811698'],
        allowChannels: [
          {
            commandName: 'some',
            channels: ['745366352386326572'],
          },
        ],
        webhook: {
          webhookId: 'your_webhook_id',
          webhookToken: 'your_webhook_token',
        },
        // and other discord options
      }),
    }),
  ],
  providers: [BotGateway],
})
export class BotModule {}

▶️ Usage

Create your class (e.g. BotGateway), mark it with @Injectable() or @Controller()

💡 Example

/*bot.gateway.ts*/

import { Injectable, Logger } from '@nestjs/common';
import { On, DiscordClient } from 'discord-nestjs';

@Injectable()
export class BotGateway {
  private readonly logger = new Logger(BotGateway.name);

  constructor(private readonly discordClient: DiscordClient) {}

  @On({ event: 'ready' })
  onReady(): void {
    this.logger.log(`Logged in as ${this.discordClient.user.tag}!`);
    this.discordClient.getWebhookClient().send('hello bot is up!');
  }
}

✨ You can use the following decorators:

ℹ️ Decorator @Client

You can get discord client via @Client() decorator instead constructor property

💡 Example

/*bot.gateway.ts*/

import { Injectable, Logger } from '@nestjs/common';
import { On, DiscordClient } from 'discord-nestjs';

@Injectable()
export class BotGateway {
  private readonly logger = new Logger(BotGateway.name);

  @Client()
  discordClient: DiscordClient;

  @On({ event: 'ready' })
  onReady(): void {
    this.logger.log(`Logged in as ${this.discordClient.user.tag}!`);
  }
}

ℹ️ Decorator @Command

Use the @Command decorator to handle incoming commands to the bot

  • name * - command name
  • prefix - override global prefix
  • isRemoveCommandName - remove command name from message
  • isRemovePrefix - remove prefix name from message
  • isIgnoreBotMessage - ignore incoming messages from bots
  • allowChannels - list of channel identifiers on which this command will work
  • isRemoveMessage - remove message from channel after receive

💡 Example

/*bot.gateway.ts*/

@Injectable()
export class BotGateway {
  @OnCommand({ name: 'start' })
  async onCommand(message: Message): Promise<void> {
    await message.reply(`Execute command: ${message.content}`);
  }
}

ℹ️ Decorator @On

Handle discord events see

  • event * - name of the event to listen to

💡 Example

/*bot.gateway.ts*/

@Injectable()
export class BotGateway {
  @On({ event: 'message' })
  async onMessage(message: Message): Promise<void> {
    if (!message.author.bot) {
      await message.reply("I'm watching you");
    }
  }
}

ℹ️ Decorator @Once

Handle discord events (only once) see

  • event * - name of the event to listen to

💡 Example

/*bot.gateway.ts*/

@Injectable()
export class BotGateway {
  @Once({ event: 'message' })
  async onceMessage(message: Message): Promise<void> {
    if (!message.author.bot) {
      await message.reply("I'm watching you");
    }
  }
}

ℹ️ Decorator @UseGuards (Test feature)

To guard incoming messages you can use @UseGuards() decorator

💡 Example

You need to implement DiscordGuard interface

/*bot.guard.ts*/

import { DiscordGuard } from 'discord-nestjs';
import { ClientEvents, Message } from 'discord.js';

export class BotGuard implements DiscordGuard {
  async canActive(
    event: keyof ClientEvents,
    context: Message,
  ): Promise<boolean> {
    if (context.author.id === '766863033789563648') {
      return true;
    } else {
      const embed = new MessageEmbed().setColor().setTitle('Ups! Not allowed!');
      await context.reply(embed);
      return false;
    }
  }
}
/*bot.gateway.ts*/
import { On, UseGuards, OnCommand } from 'discord-nestjs';
import { Message } from 'discord.js';

@Injectable()
export class BotGateway {
  @UseGuards(BotGuard)
  @OnCommand({ name: 'hide' })
  async guardCommand(message: Message): Promise<void> {
    // to do something
  }
}

ℹ️ Decorator @UseInterceptors (Test feature)

To intercept incoming messages you can use @UseInterceptors() decorator

💡 Example

You need to implement DiscordInterceptor interface

/*bot.interceptor.ts*/

import { DiscordInterceptor } from 'discord-nestjs';
import { ClientEvents } from 'discord.js';

export class BotInterceptor implements DiscordInterceptor {
  intercept(event: keyof ClientEvents, context: any): any {
    return 'Some custom value';
  }
}
/*bot.gateway.ts*/
import { On, UseInterceptors } from 'discord-nestjs';

@Injectable()
export class BotGateway {
  @UseInterceptors(BotInterceptor)
  @On({ event: 'message' })
  async onSomeEvent(context: string): Promise<void> {
    // to do something
  }
}

ℹ️ Decorator @Middleware (Test feature)

For handling intermediate requests you can use @Middleware decorator

  • allowEvents - handled events
  • denyEvents - skipped events

💡 Example

You need to implement DiscordMiddleware interface

/*bot.middleware.ts*/

@Middleware()
export class BotMiddleware implements DiscordMiddleware {
  private readonly logger = new Logger(BotMiddleware.name);

  use(
    event: keyof ClientEvents,
    context: ClientEvents[keyof ClientEvents],
  ): void {
    if (event === 'message') {
      this.logger.log('On message event triggered');
    }
  }
}

Don't forget to add to providers

@Module({
  providers: [BotMiddleware],
})
export class BotModule {}

Any questions or suggestions? Discord Федок#3051