Tutorial: Creating a Discord Bot (Part 2)

NicolasBrondinBernard

Author
@NicolasBrondinBernard

Learn how to read and respond to messages with your Discord bot!

Article published on 11/11/2021, last updated on 10/08/2026

This second part of the tutorial on creating a Discord bot requires that you have followed the first part, available right here!

In the rest of this tutorial, we will discover how to make our bot interact with a Discord server, and how to make it reply to messages!

Automatically generate an invite link

First of all, we are going to slightly modify our code from the previous tutorial to make things easier for us. If you have to kick your bot in order to re-invite it to your Discord server multiple times, the process can be tedious.

The code below therefore generates the invite link and displays it every time the bot starts up!

const token  = process.env.DISCORD_TOKEN
const clientId  = process.env.CLIENT_ID
const inviteLink = `https://discord.com/api/oauth2/authorize?client_id=${clientId}&permissions=536870911991&scope=bot%20applications.commands`;

// When the client is ready, the virtual user is shown as "Logged in" in the servers.
client.once("ready", function(){
  console.log(`I am ready! Logged in as ${client.user.tag}!`);
  console.log(`Click to invite me: ${inviteLink}`);
});

Note that all the permissions are included here, to make the design phase easier, but I recommend reducing them to the minimum afterwards!

Reacting to being added to a server

When your bot is invited to a server, an event is sent with the information about that server.

Warning: In the Discord SDK, we never talk about a "server" but about a "guild", although it's simply a synonym.

You just need to listen for the "guildCreate" event to react accordingly. Here we simply display the server's name:

//Called on guild invite
client.on("guildCreate", function(guild){
  console.log(`I joined the guild: ${guild.name}`);
});

Obviously, if your bot is not running at the time it is added to the server, the event will never be received.

Listening to and replying to messages

As with everything else, you just need to listen for the "messageCreate" event to retrieve the information of ALL messages sent on the server, regardless of the channel.

So you need to be very careful about the conditions you check before replying or performing other actions.

To reply to a message, simply call the "message.reply(...)" method as shown below:


//...
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
//...

//Called on any message
client.on('messageCreate', message => {
  if(message.content.indexOf("/test") > -1){
    message.reply(`I'm a test and it's working !`)
    .then(() => console.log(`Replied to message successfully`))
    .catch(console.error);
  }
});

Even though the code above allows us to emulate listening for a slash command (/test), there is a much more suitable method that we will see in a future tutorial.

Don't forget to add "GUILD_MESSAGES" to your list of intents, otherwise you won't receive any events related to messages!

The third part of this tutorial, in which we'll learn how to make your bot interact, is currently being written. In the meantime, I'll refer you to the official Discord.js documentation: https://discord.js.org/#/docs/main/stable/general/welcome


Alexander Shatov sur Unsplash

Finished reading this article?
Our complete courses
Take it to the next level with our courses!

Complete courses, exercises and certificates to really learn programming!

4.8 average rating

Comments (0)

to leave a comment

No comments yet