Open your hosting company

Enzonic for Resellers

Open your hosting company

How to host a discord.js bot for your community

Run a discord.js bot around the clock: project layout, package.json, keeping the token secret, intents, crash loops, logs, updates and memory limits.

Enzonic teamUpdated 7 minute read

Your community bot works perfectly on your computer. Then you close the laptop, the bot goes offline, and someone pings you asking why the ticket button is dead. Moving a discord.js bot onto a server that runs all the time is not hard, but there are a handful of things that trip up nearly everyone the first time: the token, intents, slash command registration, crash loops and memory.

This guide walks through running a discord.js bot for your own server or project, from the folder layout to updates. It assumes you already have a working bot. If you are thinking about selling bot hosting to other people instead, see how to start a discord.js bot hosting business.

A project layout that survives the move

Bots written from tutorials often end up as one huge file. That works, but a little structure makes hosting and updates much easier. A common layout:

my-bot/
  package.json
  package-lock.json
  .env              (never committed, never shared)
  .gitignore
  src/
    index.js        (creates the client, loads commands and events, logs in)
    deploy-commands.js
    commands/
      ping.js
      ticket.js
    events/
      ready.js
      interactionCreate.js
  data/             (SQLite file or JSON storage, if you use one)

The point of this split is that index.js only wires things together, each command is one file, and the script that registers slash commands is separate from the bot itself. That last part matters on a host, as you will see below.

package.json is your install recipe

The server you move to will not have your node_modules folder, and you should not upload it. It is large, and native modules compiled on your machine may not run on a Linux server. Instead, the server installs dependencies from package.json and package-lock.json.

{
  "name": "my-bot",
  "version": "1.0.0",
  "main": "src/index.js",
  "type": "commonjs",
  "scripts": {
    "start": "node src/index.js",
    "deploy": "node src/deploy-commands.js"
  },
  "engines": {
    "node": ">=20"
  },
  "dependencies": {
    "discord.js": "^14.0.0"
  }
}

A few habits pay off:

  • Commit the lockfile. package-lock.json pins the exact versions you tested. On the server, npm ci installs exactly those versions and refuses to run if the lockfile and package.json disagree, which is what you want for a bot that should behave the same everywhere.
  • **Set engines.** It documents which Node.js version you need. discord.js publishes a minimum Node.js version for each release, so check the docs at discord.js.org and match your server to it.
  • **Have a start script.** Whatever runs your bot on the server should be the same command you use at home.

Keep the token out of your code

The bot token is a password. Anyone who has it can log in as your bot, read what it can read and do what it can do in every server it is in. Tokens leak through public GitHub repos, screenshots of code and copy-pasted config files more often than through anything clever.

Keep the token in an environment variable and read it at startup:

const { Client, GatewayIntentBits } = require('discord.js');

const token = process.env.DISCORD_TOKEN;
if (!token) {
  console.error('DISCORD_TOKEN is not set, refusing to start');
  process.exit(1);
}

const client = new Client({
  intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
});

client.login(token);

On a server with a file manager, a simple pattern is a .env file next to package.json, listed in .gitignore. Recent Node.js versions can load it themselves with node --env-file=.env src/index.js, or you can use the dotenv package. Either way, the token never appears in your repository.

If a token does leak, go to the Developer Portal at discord.com/developers, reset it immediately, then put the new one on the server. Deleting the leaked message or commit is not enough: assume it was copied.

Intents and slash commands

Two Discord-side settings cause most "it worked locally" confusion.

Intents. Your bot only receives the events it asks for. Message Content, Server Members and Presence are privileged intents: you have to switch them on in the Developer Portal and also request them in code. If your bot is online but ignores message commands, the Message Content intent is the first thing to check. Only request what you actually use, since every intent means more events and often more memory.

Slash commands. Commands must be registered with Discord before they show up. Guild commands (registered to one server) appear right away, which is ideal for a community bot. Global commands work everywhere but can take a while to propagate. Registration is a one-time action per change, so run your deploy script when you add or change a command, not on every bot start. Registering on every boot is a common way to hit rate limits during a crash loop.

Restarts and crash loops

A hosted bot will restart: after updates, after the host restarts the machine, after you press restart. Your bot should come back cleanly every time.

A crash loop is when the process dies during startup, gets started again, dies again, and so on. The usual causes:

  1. The token variable is empty or wrong, so login fails.
  2. A required file or folder is missing (a JSON database you forgot to upload).
  3. A dependency was not installed, so require throws.
  4. A syntax error from a half-finished edit.

When you see a loop, stop the server, read the first error printed after startup and fix that one thing. The last lines on the console are usually the restart, not the cause.

Inside the bot, make sure one bad interaction cannot kill the process. Wrap command handlers in try/catch and reply with an error message to the user. As a last line of defense, log rejected promises instead of letting them disappear:

process.on('unhandledRejection', (err) => {
  console.error('Unhandled promise rejection:', err);
});

Do not use that as a reason to ignore errors. It is a safety net that keeps the error visible in the logs.

Logs you can actually read

On a hosted panel, the console shows whatever your bot writes to standard output and standard error. That is your log. Make it useful:

  • Log one clear line when the bot is ready, including its tag and the number of servers it is in.
  • Log every command that fails, with the command name and the error.
  • Do not log message contents from your community. You do not need it to debug, and your members did not agree to it.

If you want logs on disk too, write them into a logs folder and rotate them, or the folder will slowly fill your disk allowance.

Memory and resource limits

A hosted bot runs inside a memory limit. discord.js caches data it receives (guilds, channels, roles, members, messages) so it can answer quickly. For a single community this is small. For a bot in many servers, caching everything adds up fast.

Ways to keep memory in check:

  • Request only the intents you need. Without the members intent, there is far less member data to cache.
  • discord.js lets you limit caches and sweep old entries through client options. If memory climbs over days, look at message and member caches first.
  • Watch the memory graph for a week. Steady growth that never drops points to a leak in your own code, often an array or Map that only ever gets new entries.

If your bot grows past 2,500 servers, Discord requires sharding, and discord.js includes a ShardingManager for that. For a community bot you will never need it.

Updating the bot safely

Updates are where community bots break. A routine that works:

  1. Make a backup on the server before you change anything, including your data folder.
  2. Upload the changed files (or pull from your repository).
  3. If package.json changed, reinstall dependencies.
  4. If commands changed, run the deploy script once.
  5. Restart and watch the console until the ready line appears.

Be careful with major discord.js versions. Major releases change APIs, and code written for an older version will often fail on startup. Read the upgrade guide and test on a second bot application before you update the one your community depends on.

Running it through a branded panel with Enzonic for Resellers

If you run several bots, or bots plus game servers for a community, you can host them through your own branded panel with Enzonic for Resellers. Opening a site is free, and panel-only mode skips the storefront entirely: your address serves only sign-in and the customer panel, which suits a community that does not want to sell anything.

The panel gives you a live console with power controls and CPU, memory and disk stats, a file manager and SFTP for uploading your project, backups and scheduled backups, scheduled tasks and subusers, so your bot developer can get access without your password. With Enzonic for Resellers, discord.js bots runs on Enzonic nodes in Europe, North America and Asia and on your own Pterodactyl or Pelican Wings nodes, so you can sell it without running any hardware. You can also connect your own machine running Pterodactyl Wings or Pelican Wings and run the bot there. If you give a server to your own community on Enzonic nodes as a grant, its base price is charged monthly from your credits.

The same panel works for plain Node.js projects, covered in how to host a Node.js app for your community, and the panel features guide explains what each panel section is for. Open a site to try it.

Frequently asked questions

Why is my discord.js bot online but not responding?

Most often the Message Content intent is missing, either in the Developer Portal or in the client's intents list. Check both, then check the console for errors thrown inside your message or interaction handler.

Should I upload node_modules to the server?

No. Upload package.json and package-lock.json and install on the server. Folders built on your own computer can contain native modules for the wrong operating system, and they waste disk space.

How do I keep my bot token safe on a shared panel?

Store it in an environment variable or a .env file that is never committed, and give other admins subuser access instead of sharing files over chat. If it ever leaks, reset it in the Developer Portal right away.

Do I need sharding for a community bot?

No. Discord requires sharding once a bot is in 2,500 servers. A bot for one community or a few partner servers runs fine as a single process.

#discord#discord-js#bots#nodejs