A Discord bot built with NestJS and Discord.js that echoes messages and responds to a simple /helloworld slash command.
- Message Echo: Replies to user messages by appending "-bot" to the original message
- Hello World Command: Responds with "Hello World!" when users type
/helloworld - Modular Architecture: Built using NestJS's modular design patterns
- TypeScript Support: Fully typed for better developer experience
- Node.js (v18 or higher)
- pnpm
- Discord account and bot token
- Discord server with admin access
- Clone the repository:
git clone https://github.com/yourusername/discord-bot.git
cd discord-bot- Install dependencies:
pnpm install- Create a .env file in the root directory:
BOT_TOKEN=your_discord_bot_token
CLIENT_ID=your_bot_client_id
- Go to the Discord Developer Portal
- Create a new application and add bot
- Enable the following Privileged Gateway Intents:
- Message content intent
- Server member intent
- Copy your bot token and client ID to the
.envfile - Generate an invite URL with the following permissions:
- bot
- applications.commands
- send messages
- Read message History
- Invite the bot to your server using the generated URL
src/
├── config/ # Configuration settings
├── discord/
│ ├── commands/ # Slash command definitions
│ ├── events/ # Event handlers (messages, interactions)
│ ├── discord.module.ts # Discord module definition
│ └── discord.service.ts # Core Discord service
├── app.controller.ts # Main application controller
├── app.module.ts # Main application module
├── app.service.ts # Application service
└── main.ts # Application entry point
# Watch mode
pnpm start:dev# Build
pnpm build
# Run in producation mode
pnpm start:prodThe project includes unit tests and integration tests for all components:
# Run all tests
pnpm test
# Run tests in watch mode
pnpm test:watch
# Generate test coverage
pnpm test:cov- Create a new command class in the
src/discord/commandsdirectory:
import { Injectable } from '@nestjs/common';
import { SlashCommandBuilder } from 'discord.js';
import { Command } from './command.interface';
@Injectable()
export class MyNewCommand implements Command {
data = {
name: 'mycommand',
description: 'Description of my command',
toJSON: () => {
return new SlashCommandBuilder()
.setName('mycommand')
.setDescription('Description of my command')
.toJSON();
}
};
async execute(interaction) {
await interaction.reply('This is my new command!');
}
}- Register the command in
discord.module.ts
@Module({
providers: [
// ... other providers
MyNewCommand,
{
provide: 'DISCORD_COMMANDS',
useFactory: (...commands) => commands,
inject: [
// ... other commands
MyNewCommand,
],
},
],
})The bot handle two main types of events:
- Message Create Event: Triggered when a user sends a message
- Interaction Create Event: Triggered when a user uses a slash command
To add a new event handler, create a class in the src/discord/events directory that implements the Event interface.
This bot can be deployed to any Node.js hosting environment. Just make sure to:
- Set the environment variables (BOT_TOKEN, CLIENT_ID)
- Install dependencies with
pnpm install --production - Build the project with
pnpm build - Start the server with
pnpm start:prod
- NestJS - The progressive Node.js framework
- Discord.js - Node.js module to interact with the Discord API