App hosting

Discord bot with Node.js

Build a bot with discord.js and keep it online around the clock on RHQ App Hosting.

This guide starts from zero: you create the bot in Discord, write a small bot that answers !ping and start it on your RHQ Node.js server.

  1. Create the application in Discord

    Go to the Discord Developer Portal, click New Application, give it a name and confirm.

  2. Get the bot token

    Open the Bot tab and click Reset Token. Copy the token and keep it to yourself: anyone who has it can control the bot.

    On the same page, under Privileged Gateway Intents, turn on Message Content Intent so the bot can read messages.

  3. Invite the bot to your Discord server

    Open OAuth2 → URL Generator, tick the bot scope and the Send Messages and Read Message History permissions. Open the generated link and pick your server.

  4. Prepare the project

    On your computer, create a folder with three files.

    package.json
    json
    {
      "name": "my-bot",
      "main": "index.js",
      "dependencies": {
        "discord.js": "^14.16.0",
        "dotenv": "^16.4.0"
      }
    }
    index.js
    js
    require('dotenv').config();
    const { Client, Events, GatewayIntentBits } = require('discord.js');
     
    const client = new Client({
      intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
    });
     
    client.once(Events.ClientReady, (c) => {
      console.log(`Logged in as ${c.user.tag}`);
    });
     
    client.on(Events.MessageCreate, (message) => {
      if (message.author.bot) return;
      if (message.content === '!ping') message.reply('Pong!');
    });
     
    client.login(process.env.DISCORD_TOKEN);
    .env
    ini
    DISCORD_TOKEN=paste_your_token_here
  5. Upload the files to the panel

    In the RHQ panel open the Files tab and upload package.json, index.js and .env. Do not upload the node_modules folder.

  6. Set the main file and start

    In the Startup tab set the main file to index.js, then click Start. Dependencies install on the first start. When Console shows Logged in as ..., type !ping in Discord.

Troubleshooting

The bot is online but does not answer !ping

Message Content Intent is off in the Bot tab of the Developer Portal, or GatewayIntentBits.MessageContent is missing from the code.

The console shows "An invalid token was provided"

The token in .env is wrong or was reset. Copy a new one from the Bot tab and restart the server.