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.
Before you start coding, you'll need a bot application on Discord:
- Open the Discord Developer Portal and click New Application. Give your bot a name.
- 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.
- On the same page, under Privileged Gateway Intents, enable Message Content Intent if your bot should respond to message content.
- Also note the Application ID under General Information—you'll need it to register slash commands.
- 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.
Create a new Node.js project and install discord.js:
npm init -y
npm install discord.js
Create an index.js file and set up the basic structure of your bot:
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:
client.on("guildMemberAdd", (member) => {
const channel = member.guild.channels.cache.find((c) => c.name === "willkommen");
if (channel) {
channel.send(`Willkommen auf dem Server, ${member}!`);
}
});
- Create a new Discord bot service in the ZapX customer panel if you haven't already.
- In the configuration, select Node.js (current LTS version) as the runtime.
- Upload your
index.jsandpackage.json(for thediscord.jsdependency) to the service directory using either the built-in file manager or the file upload option.
- 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_TOKENenvironment variable.
Bot ist online als ... and appear as "Online" on your Discord server.
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.