Goal: Send a button and handle a click.
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] });
},
};
Create buttons/demo_button.js
module.exports = {
customID: "demo_button",
async execute(interaction) {
await interaction.reply({ content: "Button clicked!", ephemeral: true });
}
};
node index.js
Run /buttondemo and click the button.