A basic Discord bot application template using Node.js ≥23.51 and 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.52 is used for the SQLite database.
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.
- Go to
⚙️ Installation
and select both installation contexts.- 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 only be visible to the invoker. 3
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.
Open the VS Code Command Palette with CTRL+SHIFT+P,
and select Developer: Reload Window
.
Expand to see how to run ESLint from the terminal. (OPTIONAL)
# Check for problems in all files.
node --run lint
# Tool for inspecting ESLint configs.
node --run lint:ci
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.
Application 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" action 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 | Integration Types | Tags |
---|---|---|---|---|
/eval |
Chat input | Server | user guild |
owner modal |
/rps |
Chat input | Global | user guild |
fun game |
/guess |
Chat input | Global | guild |
fun game |
/blackjack |
Chat input | Global | user guild |
fun game |
/rolldice |
Chat input | Global | user guild |
fun |
Get avatar |
User context menu | Global | user guild |
embed |
The integration types defines the contexts where the command is available, only for globally-scoped commands:
Integration Type | Description |
---|---|
user |
The command is visible to users who have installed the application on their account. |
guild (Server) |
The command is visible to all members of the server where the application is installed. |
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, submit 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.
Footnotes
-
Node.js ≥23.5 is required for
node:module registerHooks
(register-hooks.js
). ↩ -
Starting with Node.js 23.4, the
node:sqlite
module can be used without the--experimental-sqlite
CLI flag. ↩ -
YouTube — PSA: Discord added a New Raid and Scamming Method... (@NoTextToSpeech) ↩