A basic Discord bot application template using Discord.js v14.
The template uses the Discord.js built-in ShardingManager
to run shards in separate processes.
Although sharding is only required at 2500 guilds, doing so in advance should not be a problem.
The native node:sqlite
module added in Node.js 22.5 is used for the SQLite database.
Starting with Node.js 23.4, the module can be used without the --experimental-sqlite
CLI flag.
Currently, the module is loaded but the database is not used.
Create a Discord bot application, see Discord.js Guide - Setting up a bot application.
🤖 Generate an invite link for your application.
- Navigate to Discord Developers - Applications.
- Scroll down and select your app, copy the Application ID.
- Replace
000000000000000000
with your app ID in the URLs below.Add the bot application to your private test server:
https://discord.com/oauth2/authorize?client_id=000000000000000000&permissions=2147485696&integration_type=0&scope=bot+applications.commands
Add the bot application to your user account to use it anywhere:
https://discord.com/oauth2/authorize?client_id=000000000000000000&integration_type=1&scope=applications.commands
Adding the bot application to your user account allows its use even on servers where it is not installed.
If the server has theUse External Apps
permission disabled, the bot's messages will be visible only to you. 1
Before starting up the bot, you will need to install and configure a few things.
Install pnpm, then run the following command in the Terminal to install Node.js:
# Install the latest version of NodeJS.
pnpm env use latest --global
# Once NodeJS is installed, you can close the terminal.
# We will use the VS Code integrated terminal from now on.
Clone this repository to your local computer, open the root directory with Visual Studio Code.
Alternatively, you can create a new repository using this template, then clone your own repository.
Run the following command in the VS Code Terminal to install all required dependencies:
# Install all dependencies for the project.
pnpm install
# Optionally, update all dependencies to their latest versions.
pnpm update --latest
Install the VS Code ESLint extension, it helps you find and fix problems with your JavaScript code.
# Check for problems in all files.
node --run lint
# Tool for inspecting ESLint configs.
node --run lint:ci
Open the VS Code Command Palette with CTRL+SHIFT+P,
and select Developer: Reload Window
.
Rename .sample.env
to .env
, and configure the required fields:
- Set the bot token, application id and owner id.
- Set a test server id in which the bot is in for testing purposes.
Run the following commands to register application commands and start the bot:
# Register all commands in the test server.
node --run reg -- bulk ALL TEST
# Start the bot and log in to Discord.
node --run bot
The bot should be up and running by now, check the information displayed in the terminal.
Continue reading Application Commands to learn how to create and register app commands.
Commands can be scoped either globally or to a specific server.
- Global commands are available in all of the servers where your app is installed, and in DMs if the bot shares a mutual server with the user. Register global commands when they're ready for public use.
- Guild commands are only available in the servers you explicitly add them, making them useful for features available only to a subset of servers. Register guild commands for quick testing, as they are updated instantly.
# Register all global commands.
# The "ALL" keyword references all commands in 'src/commands'.
node --run reg -- create ALL
# Register all commands in the test server.
# The "TEST" keyword references "TEST_GUILD_ID" in the '.env' file.
node --run reg -- create ALL TEST
# Delete the "rolldice" command in a specific server.
node --run reg -- delete rolldice 1234567890123456789
# Multiple commands can be specified by separating them with a comma.
# The 'src/commands/log.json' file stores a log with all the commands.
# Use the "bulk" command instead of "create" to bulk-overwrite all commands.
# Registering a command with an already-used name will update the existing command.
You must create the application commands in src/commands
before registering them.
The following table specifies all available application commands that serve as examples:
Command | Type | Scope | Tags |
---|---|---|---|
/eval |
Chat input | Server | owner modal |
/rolldice |
Chat input | Global | |
Get avatar |
User context menu | Global | embed |
Tip
To close all connections gracefully and terminate the main process:
- Invoke the
/eval
slash command from within the Discord client. - Once the modal or pop-up form appears, provide the following code:
(interaction) => {
// This code is evaluated in 'src/bot.js'.
interaction.client.bot.eval(async ({ manager }) => {
// This code is evaluated in 'src/index.js'.
await manager.broadcastEval(client => client.destroy());
process.exit(); // terminate the process (src/index.js)
});
}
This project is licensed under the GNU General Public License v3.0. See the license file for details.