This guide builds a discord.js bot with one slash command, /ping, and runs it on your bot server. You need a bot token first: How to create a Discord bot and get its token. In a hurry? Create a starter bot in the setup card writes these two files for you: How to create a starter Discord bot in one click.
The two files
package.json
{
"name": "my-discord-bot",
"private": true,
"main": "index.js",
"dependencies": {
"discord.js": "^14"
}
}
index.js
const { Client, Events, GatewayIntentBits, REST, Routes, SlashCommandBuilder } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const commands = [
new SlashCommandBuilder().setName('ping').setDescription('Check that the bot answers').toJSON(),
];
client.once(Events.ClientReady, async () => {
await new REST().setToken(process.env.DISCORD_TOKEN).put(Routes.applicationCommands(client.user.id), { body: commands });
console.log(`Logged in as ${client.user.tag}`);
});
client.on(Events.InteractionCreate, async (interaction) => {
if (interaction.isChatInputCommand() && interaction.commandName === 'ping') {
await interaction.reply(`Pong! ${client.ws.ping} ms`);
}
});
client.login(process.env.DISCORD_TOKEN);
What it does: the bot logs in with the token the panel hands it as DISCORD_TOKEN, registers /ping with Discord when it is ready, and prints Logged in as .... It only uses the Guilds intent, so no privileged intent has to be switched on.
Run it on PlayNHost
- Open your server in the panel. The bot's card on Overview must say Node.js Bot. If not, change the type and keep your files: How to switch your bot to another language.
- In Files, upload
index.jsandpackage.jsonto the main folder. Do not uploadnode_modules. - On the Bot page, pane Code, check Entry file is
index.js. - On the Discord pane, paste your token and click Connect. Then invite the bot with Add to a Discord server.
- Press Start and open Console. The first start runs
npm install, so it takes a little longer. Then you see[PlayNHost] Starting your botandLogged in as .... - In Discord, type
/ping.
![The Console of a bot server showing [PlayNHost] Starting your bot followed by Logged in as MyBot.](/images/kb/discord-bots/bot-console-logged-in.webp?v=20261028a)
/ping does not show up yet
Routes.applicationCommands registers a global command, which can take a little while to appear the first time. While you test, register it on your own server, which is instant:
Routes.applicationGuildCommands(client.user.id, '123456789012345678') // your server's ID
To copy your server's ID, switch on Developer Mode in Discord's Advanced settings, then right-click the server icon and choose Copy Server ID. Restarting Discord (Ctrl+R) also helps.
Reading messages
To react to message text, such as !hello, add GatewayIntentBits.GuildMessages and GatewayIntentBits.MessageContent to the intents and switch on Message Content Intent in the Developer Portal. On the Bot page, pane Discord, tick My code uses this next to Message Content so the panel warns you if it is off.
client.on(Events.MessageCreate, async (message) => {
if (!message.author.bot && message.content === '!hello') {
await message.reply(`Hello ${message.author.displayName}!`);
}
});
Next steps
- Add packages:
npm installthem on your computer so they land inpackage.json, then uploadpackage.jsonagain. How to install packages for your Discord bot. - Keys and passwords: How to set environment variables and secrets.
- Deploy from GitHub: How to deploy and update your Discord bot from GitHub.
- Everything about the Node.js type: How to host a Node.js Discord bot.



