← Back to lessons

Lesson 6 — Buttons

Goal: Send a button and handle a click.

Video

Step-by-step

Step 1 — Create a command to send a button

Create commands/buttondemo.js

const { SlashCommandBuilder } = require("@discordjs/builders");
const { ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js");

module.exports = {
  dev: true,
  data: new SlashCommandBuilder().setName("buttondemo").setDescription("Sends a demo button"),
  async execute(interaction) {
    const row = new ActionRowBuilder().addComponents(
      new ButtonBuilder()
        .setCustomId("demo_button")
        .setLabel("Click me")
        .setStyle(ButtonStyle.Primary)
    );
    await interaction.reply({ content: "Press the button:", components: [row] });
  },
};
Step 2 — Create the button handler file

Create buttons/demo_button.js

module.exports = {
  customID: "demo_button",
  async execute(interaction) {
    await interaction.reply({ content: "Button clicked!", ephemeral: true });
  }
};
Step 3 — Restart and test
node index.js

Run /buttondemo and click the button.

Completion checklist

← Lesson 5 Next → Lesson 7