How to make your first Discord bot and put it online

To make a Discord bot, register an app in the Developer Portal, write the code with discord.js and invite the bot to your server. Then host the process on a machine that stays on.

Updated 5 min read

Learning objectives

After reading this article you will be able to:

  • Create the app and the bot token in the Discord Developer Portal
  • Write, register and test a slash command with discord.js
  • Put the bot online and confirm it is running
On this page

A Discord bot is a program that connects to Discord with a token and reacts to events: a typed command, a message, a member joining. It does not live inside Discord. It runs on your computer or on a server, so it is only online while that process is running.

Making your first bot has two parts: setting up the app in the Developer Portal and writing the code. This guide covers both and ends with the bot online, using JavaScript with discord.js v14, the most widely used library for the job.

1App and token in the PortalNew Application, Bot tab2Code with discord.jsGuilds intent and /ping3Register the slash commandonce, not on every start4Invite to the serverbot + applications.commands5Host the processon an always-on machinePortalapp + tokenindex.jsin your projectDiscord APIRESTYour serverbot joins offlineGatewayopen WebSocket
Starting the bot neither creates the command nor puts it online: registration hits the API once, online comes from the process on the Gateway.

What do you need before you start?#

  • A current Node.js version. The official discord.js guide recommends the current Active LTS of Node.js; the official release schedule shows which one that is. Check yours with node --version.
  • A Discord account and a server of your own for testing. To invite a bot you need the Manage Server permission there.
  • A text editor and a terminal.

How do you create the app and the token in the Portal?#

  1. Open the Discord Developer Portal and click New Application. Name it and confirm.
  2. On the app page, copy the Application ID (General Information tab). This is your CLIENT_ID.
  3. Open the Bot tab and click Reset Token. Copy the token right away, because it is shown only once. If you lose it, generate another.
  4. Don't confuse the token with the client secret: the secret is a shorter string and won't work for the bot.

The token is the bot's password: whoever has it controls the bot. Never put it in your code or in a repository. The article on the Discord bot token covers how to store it and what to do if it leaks.

For the GUILD_ID, turn on Developer Mode under User Settings, Advanced. Then right-click your server icon and choose the option to copy its ID (Discord support has the full steps).

How do you invite the bot to your server?#

In the Portal, open OAuth2 and use the URL generator:

  1. Select the bot and applications.commands scopes.
  2. Choose the permissions the bot needs. The /ping command in this guide needs none.
  3. Copy the generated URL, open it in your browser and pick your test server.

The bot shows up in the member list as offline. It only goes online once the code is running.

How do you set up the project?#

Create the folder, install the library and enable ES modules in package.json:

bash
mkdir my-bot && cd my-bot
npm init -y
npm pkg set type=module
npm install discord.js

Create a .env file next to package.json and add .env to .gitignore:

bash
DISCORD_TOKEN=paste_the_token_here
CLIENT_ID=application_id
GUILD_ID=test_server_id

Node.js reads this file with the --env-file flag, no extra package needed, as the Node.js documentation shows.

How do you write the bot code?#

Create index.js. The bot uses only the Guilds intent, which is enough for slash commands, and answers /ping:

js
import { Client, Events, GatewayIntentBits } from "discord.js";

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

client.once(Events.ClientReady, (readyClient) => {
  console.log(`Online as ${readyClient.user.tag}`);
});

client.on(Events.InteractionCreate, async (interaction) => {
  if (!interaction.isChatInputCommand()) return;

  if (interaction.commandName === "ping") {
    await interaction.reply("Pong!");
  }
});

client.login(process.env.DISCORD_TOKEN);

The structure follows the official discord.js guide: create the client, log when it connects, listen for interactions and log in with the token.

How do you register and test the slash command?#

Writing the if for /ping does not create the command in Discord. You must send its definition to the API once, and again whenever it changes. Create deploy-commands.js:

js
import { REST, Routes, SlashCommandBuilder } from "discord.js";

const commands = [
  new SlashCommandBuilder().setName("ping").setDescription("Replies with Pong!"),
].map((command) => command.toJSON());

const rest = new REST().setToken(process.env.DISCORD_TOKEN);

await rest.put(
  Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID),
  { body: commands },
);
console.log(`${commands.length} command(s) registered.`);

For testing, register commands on your server only. The discord.js guide notes there is a daily limit on command creation, so do not register on every bot start. Once the commands are final, switch the route to Routes.applicationCommands(process.env.CLIENT_ID) so they apply to every server.

How do you test the bot locally?#

Register the command and start the bot:

bash
node --env-file=.env deploy-commands.js
node --env-file=.env index.js

The terminal should print Online as BotName, and the bot goes online in your server. Type /ping and check for the Pong! reply.

What errors show up with a first bot?#

  • Error [TokenInvalid]: An invalid token was provided.: the token is empty or wrong. Check that .env is in the right folder, that you passed --env-file=.env and that you did not copy the client secret.
  • The command does not show in Discord: you did not run deploy-commands.js, the GUILD_ID belongs to another server, or the invite lacked the applications.commands scope.
  • Integration requires code grant (older guides show Bot requires a code grant): turn off the Requires OAuth2 Code Grant option in the Portal's Bot tab.
  • Used disallowed intents: the code asks for a privileged intent that is not enabled in the Portal's Bot tab.
  • The bot goes offline when you close the terminal: that is expected. The process must run on a machine that stays on.

If the bot is online but does not answer, see Discord bot not responding.

How do you keep the bot online all the time?#

Your computer is fine for development, but the bot goes down whenever the computer shuts off or loses its connection. There are two paths to a 24/7 bot:

  • VPS: you install Node.js, set up a supervisor such as pm2 or systemd, and handle updates and firewall. The steps are in how to keep a bot online 24/7.
  • Bot hosting platform: you upload the code and set environment variables; the Node.js environment already exists.

To estimate your bot's memory, see how much RAM a bot needs. The full hosting process is covered in how to host a discord.js bot.

How to do this on Square Cloud#

On Square Cloud, the bot runs in an isolated, always-on container, with automatic restart (on by default for dashboard deploys) when the process exits with an error. You deploy it with a ZIP upload in the dashboard, the CLI, GitHub or the VS Code extension.

  1. Zip the project with index.js and package.json at the root of the ZIP, without .env and without node_modules (at start, the platform runs npm install).
  2. Upload it in the dashboard. The squarecloud.app file is optional: the dashboard generates it with the runtime, memory and main file, or you can write it at the project root.
  3. Choose the Node.js runtime and index.js as the main file. Each background application, such as a bot, uses at least 256 MB from the plan's RAM pool.
  4. Set DISCORD_TOKEN in the application's environment variables. The change takes effect on the next restart.
  5. Run deploy-commands.js once on your computer, as before, and watch the live logs until Online as ... appears.

Plans are paid: Hobby, at R$12.49 per month, includes 1 GB of RAM and 1 vCPU. See the details in Discord bot hosting on Square Cloud.

Bot hosting

Keep your bot online 24/7 with automatic restarts

Learn more

FAQs

Do I need to know how to code to make a Discord bot?

Yes, the basics. This guide uses JavaScript with Node.js and about 30 lines of code across two files. If you can run a file with Node.js and edit text, you can follow along.

Which language should I use for a first bot?

JavaScript with discord.js and Python with discord.py are the most common choices, with the most complete documentation. Pick the one you already know, since the Portal steps are the same.

Why is my bot online but the command does not show up?

Starting the bot does not create slash commands. They must be registered with the Discord API using a separate script, and the bot must have been invited with the applications.commands scope.

Does the bot need privileged intents?

Not for slash commands. The Guilds intent is enough. Privileged ones, like MessageContent, are only needed to read the text of regular messages and must be enabled in the Portal.

Next step · Step 2 of 5Discord bot token: where to find it and how to keep it safeA Discord bot token lives on the Bot tab of the Developer Portal and works as the bot's password. Learn where to find it, how to store it and what to do if it leaks.