NEU 📢 Spare bis zu 30 % im ersten Monat – nur für kurze Zeit!

How to make a Discord bot with discord.js

Discord bot hostingUpdated Oct 6, 20263 min read

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

  1. Open your bot server in the panel. A new bot server starts as Node.js Bot.
  2. In Files, upload index.js and package.json. Do not upload node_modules; the server installs packages for you.
  3. In Settings, Game Settings, check Main file is index.js and paste your token into Discord bot token.
  4. 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.
  5. In Discord, type /ping. If the command does not show, see Discord bot online but not responding.

Next steps

ERSTELLE DEINEN SPIELESERVER
BEREIT, LOSZULEGEN?

Spare bis zu 30% auf deinen neuen Gameserver.
Premium-Performance, sofortige Aktivierung und unschlagbare Zuverlässigkeit. Alles zum besseren Preis.

UNSERE NEUESTEN BLOGS