Skip to content

Getting Started

Head to the Discord Developer Portal and press on “New Application”. Give it whatever name you’d like, then press “Create”. You should then be redirected to your application’s page. The application is basically like a “container” for your bot.

Head to the “Installation” tab in the sidebar, then disable “User Install” and enable “Guild Install” to ensure the bot can just be added to servers. Make sure to set “Install Link” to “None”.

In the “Bot” section of the sidebar, then disable “Public Bot” and enable the following intents: “Server Members Intent” and “Message Content Intent”. You can learn more about privileged intents here.

Head to the “OAuth2” section in the sidebar and scroll down to “OAuth2 URL Generator”. Select bot and applications.commands, then select the Administrator permission so that the bot doesn’t run into permission issues — you can update these permissions later based on what your bot’s main functions are.

Your bot’s token allows you to access your bot and perform actions on its behalf. While in the “Bot” section, press “Reset Token” and confirm the action to generate a new token. Copy this token, since it won’t be revealed to you again unless you reset it once more.

Before writing our bot’s code, make sure you have Node.js installed. Install the latest LTS (“long term support”) version of Node.js from the official website. This should also install npm, which we’ll use to manage packages.

You can check whether Node.js and npm are installed by running node -v and npm -v respectively. guilds.js requires Node.js version 20 or higher.

Then, create a new directory and open it in your code editor (such as Visual Studio Code). This will be our bot’s main folder.

Initialize a new npm project in that folder:

Terminal window
npm init -y

Afterwards, we can install guilds.js (and dotenv for environment variables):

Terminal window
npm install guilds.js dotenv

Create a file named .env in the root directory (the same one as your package.json) and structure it like this:

# add your discord bot's token where "..." is
DISCORD_TOKEN="..."

Then, we’ll create the src directory which will contain our bot’s code, and then create an index.js file in that folder. This will be the main file that runs our bot. Don’t forget to update the main field in your package.json to src/index.js, and set type to module.

Your bot’s folder should look something like this:

example-bot
├── node_modules
├── .env
├── package-lock.json
├── package.json
└── src
└── index.js

In the src/index.js file, we’ll:

  • Setup dotenv to read the token from the .env file
  • Import the Client class from guilds.js
  • Initialize our client and connect it to Discord
  • Listen for any potential errors and log them to the console
import { Client, GatewayIntents } from "guilds.js";
import "dotenv/config";
const client = new Client({
token: process.env.DISCORD_TOKEN,
intents: [
GatewayIntents.Guilds,
GatewayIntents.GuildMembers,
GatewayIntents.GuildMessages,
],
});
client.once("ready", (c) => {
console.log(`Logged in as ${c.user.tag}!`);
});
client.on("error", console.error);
client.connect();

Run node . in the terminal to start the bot.

Your terminal should output something like this:

Logged in as Example#1234!

And congratulations, your bot is now online! 🎉

Later in this guide, we’ll implement more advanced features for the bot, such as listening for messages, handling commands, and more.