NOUVEAU 📢 Économisez jusqu’à 30% sur votre premier mois, offre limitée !

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

CRÉEZ VOTRE SERVEUR DE JEU
PRÊT À COMMENCER ?

Économisez jusqu’à 30% sur votre nouveau serveur de jeu.
Des performances premium, une activation instantanée et une fiabilité inégalée à un meilleur prix.

NOS DERNIERS ARTICLES