Getting Started
In this article, you'll learn about the basics of nestcord, and how it integrates with NestJS!
Many of the concepts seen with nestcord are designed to be used like other components in a NestJS project.
We recommend you to be familiar with the NestJS documentation, especially its overview section, before getting started.
The very first step is to install nestcord and its dependency, Discord.js
- npm
- Yarn
- pnpm
npm install @globalart/nestcord discord.js
yarn add @globalart/nestcord discord.js
pnpm add @globalart/nestcord discord.js
You need to install Node.js v16.0.0 or newer to use NestCord and Discord.js.
Module
NestCord is a module like any others, and can be imported as such within your Nest application.
Not sure what modules are? Catch up by reading about them in NestJS!
import { Module } from '@nestjs/common';
import { AppService } from './app.service';
import { IntentsBitField } from 'discord.js';
@Module({
imports: [
NestCordModule.forRoot({
token: process.env.DISCORD_TOKEN,
intents: [IntentsBitField.Flags.Guilds],
development: [process.env.DISCORD_DEVELOPMENT_GUILD_ID]
})
],
providers: [AppService]
})
export class DiscordModule {}
Make sure to setup the correct intents required by your application!
The module arguments are an extension of discord.js ClientOptions, in addition to 3 nestcord options: token
, prefix
and development
.
export interface NestCordModuleOptions extends DiscordClientOptions {
token: string;
prefix?: string | (message: Message) => string | Promise<string>;
development?: Snowflake[] | false;
skipRegistration?: boolean;
skipGetCommandInfoFromDiscord?: boolean;
}
Property | Type | Description |
---|---|---|
token | string | Your Discord token |
prefix | string or (message: Message) => string | The prefix for your bot |
development | Snowflake[] or false | The development guilds for your bot |
skipRegistration | boolean | Skip automatic registration of application commands |
skipGetCommandInfoFromDiscord | boolean | Skip automic fetch command info from DiscordAPI, DiscordResponse will be null in CommandDiscovery |
If you have commands using the guilds
property, the global development argument will not overwrite it.
Slash Commands
The best way to interact with your users is to use Slash commands! Slash commands allow you to create commands with precise arguments and choices, giving users the best experience.
To create a command with NestCord, you can use the SlashCommand
decorator.
import { Injectable } from '@nestjs/common';
import { Context, SlashCommand, SlashCommandContext } from '@globalart/nestcord';
@Injectable()
export class AppService {
@SlashCommand({
name: 'ping',
description: 'Ping command!'
})
public async onPing(@Context() [interaction]: SlashCommandContext) {
return interaction.reply({ content: 'Pong!' });
}
}
When the client logs in, it will automatically register all of the commands.
Global commands are cached for up to an hour, therefore to avoid the global commands cache, you should use the development
argument on the NestCord module. This will restrict the command to a single guild, preventing it from getting caught by the cache.
Running the application
You can run the following command at your OS command prompt to start the application listening Discord API events:
- npm
- Yarn
- pnpm
$ npm run start
$ yarn run start
$ pnpm run start