Black Friday: first month free on quarterly plans · until 30 Nov See the plans Conditions
New here? Try a Minecraft server or Discord bot free for 24 hours. Try free
ServerSparkServerSpark

Knowledge base

Make a Discord bot with JavaScript (discord.js)

Build your first Discord bot with a slash command in discord.js and put it online 24/7.

NVMe nodesNightly backupsOnline in minutes

Updated 2 November 2026

You can make your own Discord bot in JavaScript in fifteen minutes. In this guide you build a bot with discord.js and a first slash command, /ping, and then put it online 24/7. You need Node.js 18 or newer on your own computer.

1. Create the bot on Discord

  1. Go to the Discord Developer Portal, log in and click New Application. Give your bot a name.
  2. Go to Bot and click Reset Token. Copy the token. This is your bot's password: never share it.
  3. Go to OAuth2 → URL Generator. Under Scopes tick bot and applications.commands, and under Bot Permissions for example Send Messages.
  4. Open the link at the bottom in your browser and add the bot to your own server.

2. Write the code

Make a folder with three files.

package.json:

{
  "name": "my-discord-bot",
  "main": "index.js",
  "dependencies": {
    "discord.js": "^14.0.0",
    "dotenv": "^16.0.0"
  }
}

.env (replace the text with your own token):

DISCORD_TOKEN=your-token-here

index.js:

require('dotenv').config(); // reads DISCORD_TOKEN from the .env file
const { Client, Events, GatewayIntentBits, SlashCommandBuilder } = require('discord.js');

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

const commands = [
  new SlashCommandBuilder().setName('ping').setDescription('Check if the bot is online'),
];

client.once(Events.ClientReady, async (c) => {
  await c.application.commands.set(commands); // registers the slash commands with Discord
  console.log(`Logged in as ${c.user.tag}`);
});

client.on(Events.InteractionCreate, async (interaction) => {
  if (!interaction.isChatInputCommand()) return;
  if (interaction.commandName === 'ping') {
    await interaction.reply(`Pong! ${client.ws.ping} ms`);
  }
});

client.login(process.env.DISCORD_TOKEN);

3. Test it on your own computer

  1. Install the packages: npm install
  2. Start the bot: node index.js
  3. See "Logged in as ..."? Type /ping in your Discord server. The command can take a few minutes to appear the first time.

Close the window or turn your computer off and the bot goes offline. That's what hosting is for.

4. Put it online 24/7

With ServerSpark bot hosting your bot runs day and night, for €0.69 a month (paid quarterly):

  1. Order Bot hosting (Node.js). Your server already contains this example bot: just put your token in .env under Files and skip to the last step. Using your own code instead?
  2. Upload index.js, package.json and .env in SparkPanel under Files (no need to upload node_modules). The server starts index.js by default and installs the packages from package.json by itself.
  3. Click Start in the Console. If the bot crashes, it's restarted automatically.

More on uploading, Git and the start file in Put your Discord bot online 24/7. Prefer Python? Read Make a Discord bot in Python.

Next steps

  • Add more commands to the commands list and handle them in InteractionCreate.
  • Want to read messages (for example for a moderation bot)? Turn on the Message Content Intent under Bot in the Developer Portal and add GatewayIntentBits.GuildMessages and GatewayIntentBits.MessageContent to the intents.