Getting Started
This documentation assumes you have a good understanding of Discord API and Discord.js. It won't cover their concepts but will often refer to them.
Installation
npm add discordbox
npm add discordbox
pnpm add discordbox
pnpm add discordbox
yarn add discordbox
yarn add discordbox
bun add discordbox
bun add discordbox
⚠️ DiscordBox is an ESM-only package. Don't use require()
to import it. Your package.json
file must contain "type": "module"
or change file extensions of your project to .mjs
or .mts
.
Requirements
You need Node.js version 18 or higher and a Discord integration with a bot user.
Run a simple bot
Here is a simple example of a bot that registers a slash command called ping
that replies with Pong!
when used.
import { DiscordBox } from "discordbox";
import { SlashCommandBuilder } from "discord.js";
const bot = new DiscordBox({
token: "xxxxxxxxxxxxxxxxxx",
guildId: "xxxxxxxxxxxxxxxxxx",
clientId: "xxxxxxxxxxxxxxxxxx",
supportUserId: "xxxxxxxxxxxxxxxxxx",
});
const ping = {
action: new SlashCommandBuilder()
.setName("ping")
.setDescription("sends Pong!"),
callback: async (interaction) => {
await interaction.reply({ content: "Pong!", ephemeral: true });
},
};
bot.addGenericInteractions([ping]);
await bot.start();
import { DiscordBox } from "discordbox";
import { SlashCommandBuilder } from "discord.js";
const bot = new DiscordBox({
token: "xxxxxxxxxxxxxxxxxx",
guildId: "xxxxxxxxxxxxxxxxxx",
clientId: "xxxxxxxxxxxxxxxxxx",
supportUserId: "xxxxxxxxxxxxxxxxxx",
});
const ping = {
action: new SlashCommandBuilder()
.setName("ping")
.setDescription("sends Pong!"),
callback: async (interaction) => {
await interaction.reply({ content: "Pong!", ephemeral: true });
},
};
bot.addGenericInteractions([ping]);
await bot.start();
token
is the unique identifier of your bot. You can find it in the Discord Developer Portal.clientId
is the unique identifier of your Discord integration, a Snowflake. Needed to deploy Slash commands.guildId
is used during your development to iterate faster, anotherSnowflake
.supportUserId
is a Discord userSnowflake
that will be used in the default error handling system.
ℹ️ You might need to enable Developer Mode in your Discord client those snowflakes.
And you're done! No more code is needed on your end. Slash commands are automatically registered and updated when you start your bot and a custom interaction handler is available by default.
Next steps
Delve into core concepts and discover the default behaviors set up to reduce your need to write boilerplate.
DiscordBox is highly customizable, learn how to configure it to fit your needs. Most parts are either optional or can be replaced.
Check out API documentation, there are plenty of useful methods and classes to help you build your bot.