Skip to content

A basic Discord bot application template using DiscordJS.

License

Notifications You must be signed in to change notification settings

flipeador/discord.js-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Discord.js Template

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 of Node.js v22.5.0 is used for the database.
Currently, the module is loaded but the database is not used.

Instructions

Create a Discord bot application, see Discord.js Guide - Setting up a bot application.

Install the bot application in your private test guild and user account:

  • Navigate to Discord Developers - Applications.
  • Select your bot, and go to OAuth2 -> OAuth2 URL Generator.
  • Integration type: Guild Install.
    • Scopes: bot application.commands.
    • Permissions: Send Messages Use Slash Commands.
  • Integration type: User Install.
    • Scopes: application.commands.
  • Visit the generated URLs to authorize both integration types.

Before starting up the bot, you will need to install and configure a few things.

Install pnpm, then run the following command to install the latest version of Node.js:

pnpm env use latest -g

Download or clone the repository, open the root directory with Visual Studio Code.

Open the VS Code Terminal, and run the following command to install all required dependencies:

pnpm install

Install the VS Code ESLint extension, it helps you find and fix problems with your JavaScript code.
Once the extension is installed, the following command can be used to check for problems in all files:

node --run lint

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 console.

Continue reading Application Commands to learn how to create and register app commands.

Application 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) => {
    // Evaluates code in 'src/bot.js'.
    interaction.client.bot.eval(async ({ manager }) => {
        // Evaluates code in 'src/index.js'.
        await manager.broadcastEval(client => client.destroy());
        process.exit(); // terminate the process (src/index.js)
    });
}

License

This project is licensed under the GNU General Public License v3.0. See the license file for details.