Buttons
Add interactive buttons to your Discord bot.
Basic Button
commands/button-example.js
await this.reply({
content: 'Click the button!',
components: [{
type: 1, // Action Row
components: [{
type: 2, // Button
style: 1, // Primary (blue)
label: 'Click Me',
custom_id: 'my_button'
}]
}]
});
// Handle button click
onButton('my_button', async function() {
await this.update('Button clicked!');
});
Button Styles
Style 1
Primary
Style 2
Secondary
Style 3
Success
Style 4
Danger
Style 5
Link
Button Handlers
Use onButton() to handle button clicks:
commands/button-handler.js
onButton('button_id', async function() {
// Access button context
const user = this.user;
// Update the message
await this.update({
content: `Clicked by ${user.username}!`,
components: [] // Remove buttons
});
});
Available Methods
this.update(content)- Update the messagethis.reply(content)- Send a new messagethis.defer()- Defer the interactionthis.user- User who clickedthis.message- The message with the button
Multiple Buttons
commands/menu.js
// Menu with multiple buttons
bot.command.new('menu', 'Show menu', async function() {
await this.reply({
content: 'Choose an option:',
components: [{
type: 1,
components: [
{ type: 2, style: 1, label: 'Option 1', custom_id: 'opt1' },
{ type: 2, style: 2, label: 'Option 2', custom_id: 'opt2' },
{ type: 2, style: 4, label: 'Cancel', custom_id: 'cancel' }
]
}]
});
});
// Handle each button
onButton('opt1', async function() {
await this.update('You chose Option 1!');
});
onButton('opt2', async function() {
await this.update('You chose Option 2!');
});
onButton('cancel', async function() {
await this.update('Cancelled.');
});
💡 Tips
💡 Custom IDs
Use descriptive custom_id values like "confirm_delete" instead of "btn1"
⚡ Update vs Reply
Use update() to modify the original message, reply() to send a new one
🎨 Emojis
Add emoji to button labels for better visual appeal