How to host a Discord bot in Node.js (step by step)

Published on 4 min read

Create the bot in the Discord Developer Portal, upload the ZIP to MyBotCloud and set the token safely. A real bot online in a few minutes.

In this tutorial you put a Discord bot online, end to end, without your own server and without configuring any infrastructure. We will use an example bot that builds the channel structure of your server (information, community, support and developers categories) and stays online afterwards.

What you will need:

  • a MyBotCloud account (sign-in is with Google);
  • a Discord account and a server of your own, preferably empty, to test with;
  • about 10 minutes.

1. Create the bot on Discord

  1. Open the Discord Developer Portal and click New Application. Give it a name, for example MyBot.
  2. In the left menu, open Bot.
  3. Click Reset Token and copy the token that appears. It is your bot's password: do not paste it in code, screenshots or chats. If it leaks, come back here and generate another one.

You do not need to enable any Privileged Gateway Intent: this bot only uses the basic guilds intent.

2. Invite the bot to your server

A token alone does not put the bot inside the server. You have to invite it:

  1. In the portal, open OAuth2 → URL Generator.
  2. Under Scopes, tick bot.
  3. Under Bot Permissions, tick Manage Channels and Manage Roles.
  4. Copy the Generated URL at the bottom of the page, open it in your browser, choose your server and click Authorize.

The bot shows up in the server's member list with a BOT tag.

3. Copy the server ID

The ID tells the bot which server to work on (without it, the bot acts on every server it is in):

  1. In Discord, open User Settings → Advanced and turn on Developer Mode.
  2. Right-click your server's icon and choose Copy Server ID. It is a 17 to 19 digit number.

4. Send the bot to MyBotCloud

  1. Download the example bot (botstartup.zip).
  2. In the MyBotCloud dashboard, click New application, give it a name, keep Node.js and choose the botstartup.zip file.
  3. Under Environment variables, click Add variable twice and fill in:
NameValue
DISCORD_TOKENthe token from step 1
GUILD_IDthe ID from step 3

The filled-in form looks like this. The arrows show 1) the ZIP file, 2) the variables and 3) the button that creates the application (values are masked while you type):

MyBotCloud New application form with numbered arrows pointing to the botstartup.zip file, the DISCORD_TOKEN and GUILD_ID variables and the Create application button

  1. Click Create application and wait about 40 seconds: the platform installs the dependencies and starts the bot.

Variables are write-only: once saved, they are never shown again on screen or in logs. They are available from the very first run, so the bot does not crash for lack of a token.

5. Check that it worked

Open Manage → Logs. The bot's messages are in Portuguese, and the expected result is:

Conectado como MyBot#1234
Servidor: Your server name
  + categoria 📢 INFORMATION
  + canal 👋・welcome
  ...
  pronto: 20 criados, 0 já existiam.
Estrutura verificada. O bot continua online.

(criados means created and já existiam means already existed.)

In Discord, the server gains 5 categories and 15 channels. The INFORMATION channels are read-only for members; only staff and the bot can write in them. The 🔒 STAFF category (with the 🚨・alerts channel) is invisible to members: it is where you can receive internal notices, for example from a webhook.

The bot can run again as many times as you like: it only creates what is missing.

How the bot works

The code is small. The structure lives in a list you can edit:

const STRUCTURE = [
  {
    category: "📢 INFORMATION",
    readOnly: true,
    channels: ["👋・welcome", "📢・announcements", "🟢・status", "📝・changelog"],
  },
  // COMMUNITY, SUPPORT, DEVELOPERS...
];

On start the bot connects with the Guilds intent, reads the server and creates what is missing. If you invite it after it is already running, it notices the join (the guildCreate event) and builds the structure right away. To change categories or channels, edit structure.js and use Update on MyBotCloud: your variables are kept.

Common problems

What the log showsWhat to do
Falta a variável DISCORD_TOKENAdd the variable under Manage → Environment variables. The bot restarts by itself.
An invalid token was providedThe token was pasted with a space or was reset. Generate another with Reset Token and save it again.
O bot ainda não está no servidorThe invite (step 2) is missing, or the GUILD_ID belongs to another server.
Missing PermissionsThe bot joined without Manage Channels and Manage Roles. Invite it again with those permissions, or drag the bot's role to the top of the role list.

How many resources it needs

The example bot runs on the Start (free) plan and uses about 30 MB of the 100 MB of memory. There is room left for you to grow the code.

Security in one sentence

Never put the token inside the ZIP or the code: always use MyBotCloud's environment variables.

Next steps

Now that you have seen the full path, the same flow works for any Node.js bot: prepare the project as a .zip, pick the language, fill in the variables and upload. If you get stuck on any step, ask on our Discord.

  • discord
  • node.js
  • bots
  • hosting
  • tutorial

← Back to the blog