Skip to content

Build Your Own Discord Bot with discord.js

Learn how to build your own Discord bot with JavaScript and discord.js, step by step, and deploy it on ZapX.

12 November 20263 min read20 Views0 helpful

Introduction

In this guide, you'll build your own Discord bot from scratch using JavaScript and the discord.js library. You'll register the bot application in the Discord Developer Portal, write a working basic setup with a custom slash command, and then deploy the finished bot on your ZapX Discord Bot Hosting. Basic knowledge of JavaScript/Node.js is helpful, but not required.

Preparation

Before you start coding, you'll need a bot application on Discord:

  1. Open the Discord Developer Portal and click New Application. Give your bot a name.
  2. In the menu on the left, go to the Bot tab. Here, you can find your bot's Token by clicking Reset Token. Copy the token and keep it safe—it gives full access to your bot and must never be included in your code or shared publicly.
  3. On the same page, under Privileged Gateway Intents, enable Message Content Intent if your bot should respond to message content.
  4. Also note the Application ID under General Information—you'll need it to register slash commands.
  5. Go to the OAuth2 → URL Generator tab. Select the bot scope as well as applications.commands, then select the permissions your bot needs and open the generated link to invite the bot to your own Discord server.

Write the Bot in JavaScript

Create a new Node.js project and install discord.js:

bash
npm init -y
npm install discord.js

Create an index.js file and set up the basic structure of your bot:

javascript
const { Client, GatewayIntentBits, REST, Routes, SlashCommandBuilder } = require("discord.js");

const client = new Client({
  intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
});

const commands = [
  new SlashCommandBuilder().setName("hallo").setDescription("Begrüßt dich mit einer kurzen Nachricht"),
].map((command) => command.toJSON());

const rest = new REST({ version: "10" }).setToken(process.env.DISCORD_TOKEN);

client.once("ready", async () => {
  await rest.put(Routes.applicationCommands(client.user.id), { body: commands });
  console.log(`Bot ist online als ${client.user.tag}`);
});

client.on("interactionCreate", async (interaction) => {
  if (!interaction.isChatInputCommand()) return;
  if (interaction.commandName === "hallo") {
    await interaction.reply(`Hallo ${interaction.user}!`);
  }
});

client.login(process.env.DISCORD_TOKEN);

The token is deliberately not stored as plain text in the code. Instead, it's read from the DISCORD_TOKEN environment variable (process.env.DISCORD_TOKEN). This keeps the token out of your code and is the basis for the deployment step below.

To also respond to new server members, for example with a welcome message, add another event handler and the GatewayIntentBits.GuildMembers intent:

javascript
client.on("guildMemberAdd", (member) => {
  const channel = member.guild.channels.cache.find((c) => c.name === "willkommen");
  if (channel) {
    channel.send(`Willkommen auf dem Server, ${member}!`);
  }
});

Deploy the Bot on ZapX Discord Bot Hosting

  1. Create a new Discord bot service in the ZapX customer panel if you haven't already.
  2. In the configuration, select Node.js (current LTS version) as the runtime.
  3. Upload your index.js and package.json (for the discord.js dependency) to the service directory using either the built-in file manager or the file upload option.
  1. Enter the bot token you created earlier in the Settings tab under Bot Configuration → Discord Bot Token, then save your settings. This keeps your token separate from your code, so you won't need to update it when uploading files again. Your code can still access this value as the DISCORD_TOKEN environment variable.
5. Start the service. If everything is working correctly, your bot will log Bot ist online als ... and appear as "Online" on your Discord server.

Troubleshooting

The bot stays offline: Check that the token is correctly set in the DISCORD_TOKEN environment variable and look for any error messages in the service log.

Slash commands aren't showing up: Commands registered globally via Routes.applicationCommands() can take up to an hour to appear everywhere in Discord. To test them, register commands directly for a single server using Routes.applicationGuildCommands() instead. They will then be available immediately.

"Missing Access" or similar permission errors: The bot doesn't have the necessary permissions on the server. Check the bot's role permissions and make sure you selected the appropriate permissions in the OAuth2 URL Generator when inviting it.

Conclusion

You've now built a functional Discord bot with discord.js, added your own slash command, and deployed it on ZapX Discord Bot Hosting. From here, you can extend the bot with any number of additional commands, moderation features, or integrations.

FAQ

Frequently asked questions

Create a bot application in the Discord Developer Portal. There, create a New Application, get your token from the Bot tab using 'Reset Token', note the Application ID under General Information, and generate an invite link for your server in the OAuth2 URL Generator using the 'bot' and 'applications.commands' scopes.
The token is deliberately not stored directly in your code. Instead, it's read from the DISCORD_TOKEN environment variable ('process.env.DISCORD_TOKEN'). In ZapX Discord Bot Hosting, enter it in the Settings tab under Bot Configuration -> Discord Bot Token.
Node.js (current LTS version).

Feedback

Was this article helpful?

Your feedback helps us prioritize and improve content.

0 of 0 people found this article helpful.

Personal support

Need more help than the guide provides?

Our ZAP support team knows your product — briefly describe your issue and we'll help you personally.

Zappy knows this guide and answers you right in the chat.