Skip to content

Instantly share code, notes, and snippets.

@rgranata1
Created May 30, 2023 20:42
Show Gist options
  • Save rgranata1/1cabce0083ce2e9adf31230f4aff93a4 to your computer and use it in GitHub Desktop.
Save rgranata1/1cabce0083ce2e9adf31230f4aff93a4 to your computer and use it in GitHub Desktop.
Discord Button Interaction
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, CommandInteraction, ComponentType } from 'discord.js'
export const execute = async (interaction: CommandInteraction): Promise<void> => {
const buttons = new ActionRowBuilder<ButtonBuilder>()
.addComponents(new ButtonBuilder().setCustomId('ok').setLabel('Ok').setStyle(ButtonStyle.Primary))
.addComponents(new ButtonBuilder().setCustomId('cancel').setLabel('Cancel').setStyle(ButtonStyle.Secondary))
const message = await interaction.reply({ content: 'Run Sample?', components: [buttons] })
try {
const btnInteraction = await message.awaitMessageComponent({
componentType: ComponentType.Button,
time: 60 * 1000, // 1 minute
})
if (btnInteraction.customId === 'ok') {
await interaction.editReply({ content: 'You replied: Ok', components: [] })
} else {
await interaction.editReply({ content: 'Cancelled', components: [] })
}
} catch (error) {
if (error.code === 'InteractionCollectorError') {
await interaction.editReply({ content: 'Timed Out', components: [] })
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment