I can't figure out discord.js v13 buttons

Viewed 4062

I just can't figure out how to send a button interaction with a message in Discord.js v13 in v12 you just use discord-buttons but here it's built-in somehow and I can't quite get it.

2 Answers

Discord.js has a great documentation and you can find more about buttons in Buttons Section. It is well documented and there are some good examples too.

const { MessageActionRow, MessageButton } = require('discord.js');

client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;

    if (interaction.commandName === 'ping') {
        const row = new MessageActionRow()
            .addComponents(
                new MessageButton()
                    .setCustomId('primary')
                    .setLabel('Primary')
                    .setStyle('PRIMARY'),
            );

        await interaction.reply({ content: 'Pong!', components: [row] });
    }
});

For external redirect button, you should use .setLabel('LINK') instead of .setLabel('Primary') then remove .setCustomId('primary'). Add .setURL('URL')

For more click here

Related