DEV Community

Fauzan
Fauzan

Posted on • Edited on

Build welcome message bot for your discord server with discord v13

This tutorial requires basic knowledge on how to use discord, what is guild, channel and discord.js

Discord.js is a powerful Node.js module that allows you to interact with the Discord API very easily. It takes a much more object-oriented approach than most other JS Discord libraries, making your bot's code significantly tidier and easier to comprehend.

Usability, consistency, and performance are key focuses of discord.js, and it also has nearly 100% coverage of the Discord API. It receives new Discord features shortly after they arrive in the API.

Having that to said by discord.js, discord.js is the most battle-tested for discord bot related stuff, it's include many features like Intents, Guild and Member related events, and much more.

Now, I will introduce you how to setup welcome message discord bot for your guild or we commonly call it discord server.

Install Node.js

Node.js v16 or later is recommended, you can download it here for v16.13.2 LTS because it is supported directly by the tests workflow based on Discord test workflow, so it's much safer to use Node.js v16 or later.

Init Project With NPM

Before installing dependencies needed for discord bot, you must init project for your bot with npm first.

cd project-directory
npm init -y 
# -y is recommended for tutorial purpose 😄 DO NOT COPY THIS LINE.
Enter fullscreen mode Exit fullscreen mode

Install Dependencies

Before start coding, you can start by installing discord.js, and dotenv for local development, you can use dotenv for production but I'm not recommend it, since the term of environment variable itself must be stored in operating system and not a file, also, it may cause unexpected persistence of variable values.

  • Install discord.js
npm install discord.js 
Enter fullscreen mode Exit fullscreen mode
  • Install dotenv
npm install dotenv
Enter fullscreen mode Exit fullscreen mode

Usage

After installing discord.js and dotenv, we can create a script with event that will receive guild member arrival named guildMemberAdd.

  • Add type: "module" in your package.json for modern ES modules, reason why.
"type": "module"
Enter fullscreen mode Exit fullscreen mode

Get to know about ESM with the gist made by sindresorhus here

  • Create index.js file to put the event and client login code
// index.js - DO NOT COPY THIS LINE.
import Discord from "discord.js";
const client = new Discord.Client({
  intents: [
    Discord.Intents.FLAGS.GUILDS,
    Discord.Intents.FLAGS.GUILD_MEMBERS,
  ],
});
import dotenv from "dotenv";
dotenv.config();

client.on("ready", () => {
  console.log("Bot is ready.");
});

client.on("guildMemberAdd", (member) => {
  let channel = member.guild.channels.cache;

  let embed = new Discord.MessageEmbed()
    .setThumbnail(
      "https://your-image-url" // make sure to change this to your image
    )
    .addField(
      `:point_right: Welcome!`,
      `Hello, welcome to ${member.guild.name} <@${member.user.id}>!`,
      true
    )
    .addField(
      `:zap: Guild Statistics`,
      `Server member count: ${member.guild.memberCount}`,
      true
    )
    .setColor("YELLOW")
    .setImage(
      "https://your-image-url" // make sure to change this to your image
    );

  channel
    .find((channel) => channel.id === process.env.CHANNEL_ID)
    .send({ embeds: [embed] });
});

client.login(process.env.DISCORD_TOKEN);
Enter fullscreen mode Exit fullscreen mode

⚠️ Do not forget to enable your GUILD_MEMBERS intent flags, see here why

  • Create .env file

⚠️ this is include your bot credentials, DO NOT share it to anyone you don't work with.

DISCORD_TOKEN=
CHANNEL_ID=
Enter fullscreen mode Exit fullscreen mode

How to get DISCORD_TOKEN?

This is little bit challenging for most developers who didn't know nothing about discord bot ecosystem before, also, this is important and the rules change sometimes, so you must have to get used to it. 😄

Since in this tutorial I'm not introduce on how you create a bot from scratch and the detail of it, you can look how to setup bot using discord developer dashboard by looking for this tutorial and get the bot token to put inside DISCORD_TOKEN= of your .env file.

How to get CHANNEL_ID?

To get a Channel ID you can right click to the channel and click on "Copy ID" then paste it inside the CHANNEL_ID= of your .env file.


Discord.js has many useful utilities, but since v13 there's lot of changes you must follow, but despite that, it's awesome library you can use to create bots and manage it.

Thanks for reading!

Top comments (0)