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
- Go to the Discord Developer Portal, log in and click New Application. Give your bot a name.
- Go to Bot and click Reset Token. Copy the token. This is your bot's password: never share it.
- Go to OAuth2 → URL Generator. Under Scopes tick bot and applications.commands, and under Bot Permissions for example Send Messages.
- 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
- Install the packages:
npm install - Start the bot:
node index.js - See "Logged in as ..."? Type
/pingin 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):
- Order Bot hosting (Node.js). Your server already contains this example bot: just put your token in
.envunder Files and skip to the last step. Using your own code instead? - Upload
index.js,package.jsonand.envin SparkPanel under Files (no need to uploadnode_modules). The server startsindex.jsby default and installs the packages frompackage.jsonby itself. - 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
commandslist and handle them inInteractionCreate. - 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.GuildMessagesandGatewayIntentBits.MessageContentto the intents.