This guide builds a bot with one slash command, /ping. You need a Discord application and a bot token first: How to create a Discord bot and get its token.
The files
Create these files on your computer.
package.json
``json { "name": "my-bot", "version": "1.0.0", "main": "index.js", "dependencies": { "discord.js": "^14.16.0" } } ``
index.js
```js const { Client, GatewayIntentBits, REST, Routes, SlashCommandBuilder } = require('discord.js');
const token = process.env.DISCORD_TOKEN; const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const commands = [new SlashCommandBuilder().setName('ping').setDescription('Replies with pong')];
client.once('ready', async () => { const rest = new REST({ version: '10' }).setToken(token); await rest.put(Routes.applicationCommands(client.user.id), { body: commands.map(c => c.toJSON()) }); console.log(Logged in as ${client.user.tag}); });
client.on('interactionCreate', async (interaction) => { if (!interaction.isChatInputCommand()) return; if (interaction.commandName === 'ping') await interaction.reply('pong'); });
client.login(token); ```
The bot only needs the Guilds intent, so no privileged intent has to be switched on.
Run it on PlayNHost
- Open your bot server in the panel. A new bot server starts as Node.js Bot.
- In Files, upload
index.jsandpackage.json. Do not uploadnode_modules; the server installs packages for you. - In Settings, Game Settings, check Main file is
index.jsand paste your token into Discord bot token. - Open Console and start the server. The first start installs discord.js, so it takes a little longer. When you see
Logged in as ..., the bot is online. - In Discord, type
/ping. If the command does not show, see Discord bot online but not responding.
Next steps
- Keep secrets in the panel field, not in the file: How to keep your Discord bot token safe.
- Save data with a database: How to use a database with your Discord bot.
- Deploy from GitHub and update on every restart: How to deploy and update your Discord bot from GitHub.





