NEW 📢 Black Friday: our launch offer ends 30 Nov. Up to 70% off your first month on every game!See plans
Black FridayUp to 70% off your first month, ends 30 NovEnds in 9d 02h 53mBlack FridayUp to 70% off your first month, ends 30 NovEnds in 9d 02h 53mBlack FridayUp to 70% off your first month, ends 30 NovEnds in 9d 02h 53m

How to make a Discord bot with discord.js

Discord bot hostingUpdated Oct 28, 20264 min read

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

  1. 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.
  2. In Files, upload index.js and package.json to the main folder. Do not upload node_modules.
  3. On the Bot page, pane Code, check Entry file is index.js.
  4. On the Discord pane, paste your token and click Connect. Then invite the bot with Add to a Discord server.
  5. Press Start and open Console. The first start runs npm install, so it takes a little longer. Then you see [PlayNHost] Starting your bot and Logged in as ....
  6. In Discord, type /ping.

The Console of a bot server showing [PlayNHost] Starting your bot followed by Logged in as MyBot.

/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

Already hosting your bot somewhere else? Move it to us

OUR LATEST BLOGS