Sunar

Button

Buttons are interactive elements users can click to trigger specific actions. They are ideal for creating interactive messages, such as confirmation prompts or menu navigation.

Usage

import { Button, execute } from 'sunar';

const button = new Button({ id: 'example' });

execute(button, (interaction) => {
	// handle execution
});

export { button };

Implementation

The following example demonstrates how to implement a Button using Sunar:

import { Button, Slash, execute } from 'sunar';
import {
	ActionRowBuilder,
	ButtonBuilder,
	ButtonStyle,
	PermissionFlagsBits,
} from 'discord.js';

const slash = new Slash({
	name: 'leave',
	description: 'Make the bot leave the server',
	dmPermission: false,
	defaultMemberPermissions: [PermissionFlagsBits.Administrator],
});

execute(slash, (interaction) => {
	const button = new ButtonBuilder()
		.setCustomId('confirmLeave')
		.setLabel('Leave')
		.setStyle(ButtonStyle.Danger);

	const row = new ActionRowBuilder().setComponents(button);

	interaction.reply({
		content: 'Are you certain about my leaving the server?',
		components: [row],
	});
});

const button = new Button({ id: 'confirmLeave' });

execute(button, async (interaction) => {
	await interaction.reply({
		content: 'Leaving...',
		ephemeral: true,
	});

	interaction.guild.leave();
});

export { slash, button };

Reference

ButtonOptions

Prop

Type

ButtonConfig

Prop

Type

How is this guide?

Last updated on