Events, embeds and buttons

The bot reacting without waiting for a command, colored message cards and clickable buttons.

Documentation / Learn to write a bot / Events, embeds and buttons

  1. Turn on the right intent

    MessageContent to read messages, GuildMembers for member events.

  2. Listen to the event

    Hook into events like MessageCreate and GuildMemberAdd with client.on.

  3. Answer nicely

    Send a card with EmbedBuilder and a button with ButtonBuilder.

Commands run when a user asks for them; events run when something happens on the server. Aegis's AutoMod and welcome messages work this way.

Listen to messages

To read message content, Message Content Intent must be on both in the code and in the Developer Portal. Do not forget the message.author.bot check so the bot does not react to its own messages.

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

client.on(Events.MessageCreate, async (message) => {
  if (message.author.bot) return;

  if (message.content.includes("discord.gg/")) {
    await message.delete();
    await message.channel.send(`${message.author}, sharing invite links is not allowed.`);
  }
});

Welcome new members

The GuildMemberAdd event needs the GuildMembers intent (Server Members Intent in the Developer Portal). member.guild.systemChannel is the server's system messages channel.

index.jsjavascript
client.on(Events.GuildMemberAdd, async (member) => {
  const channel = member.guild.systemChannel;
  if (!channel) return;
  await channel.send(`Welcome ${member}! With you, we are ${member.guild.memberCount} people.`);
});

If you put a member into the text as ${member}, Discord shows it as a mention.

Send an embed

An embed is a message card with a colored line on its left, a title and fields. All of Aegis's log messages are embeds.

index.jsjavascript
const { EmbedBuilder } = require("discord.js");

const card = new EmbedBuilder()
  .setColor(0x0066ff)
  .setTitle("Server info")
  .addFields(
    { name: "Members", value: String(interaction.guild.memberCount), inline: true },
    { name: "Channels", value: String(interaction.guild.channels.cache.size), inline: true },
  )
  .setFooter({ text: "My bot" });

await interaction.reply({ embeds: [card] });

Add a button

Buttons are sent inside a row (ActionRow). When one is clicked, the InteractionCreate event comes in again; this time isButton() is true and you tell which button it was by its customId.

index.jsjavascript
const { ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js");

// Sending
const row = new ActionRowBuilder().addComponents(
  new ButtonBuilder().setCustomId("accept").setLabel("Accept the rules").setStyle(ButtonStyle.Success),
);
await interaction.reply({ content: "Did you read the rules?", components: [row] });

// When clicked
client.on(Events.InteractionCreate, async (i) => {
  if (i.isButton() && i.customId === "accept") {
    await i.reply({ content: "Thanks!", flags: MessageFlags.Ephemeral });
  }
});