How do I create buttons under messages?

I have a message how to link buttons to it? I understand MessageActionRow, Message Button, Message Embed are outdated
const { Client, Intents, MessageActionRow, MessageButton, MessageEmbed } = require('discord.js');

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.DIRECT_MESSAGES, Intents.FLAGS.DIRECT_MESSAGE_REACTIONS] });

client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});

client.on('guildMemberAdd', async member => {
const flags = [
{ name: 'Test1', emoji: '🇷🇺' },
{ name: 'Test2', emoji: '🇺🇸' },
{ name: 'Test3', emoji: '🇩🇪' },
{ name: 'Test4', emoji: '🇹🇷' },
{ name: 'Test5', emoji: '🇮🇷' },
{ name: 'Test6', emoji: '🇺🇦' },
{ name: 'Test7', emoji: '🇯🇵' }
];

const row = new MessageActionRow();
for (const flag of flags) {
const button = new MessageButton()
.setCustomId(`language_${flag.name}`)
.setLabel(flag.name)
.setStyle('PRIMARY')
.setEmoji(flag.emoji);
row.addComponents(button);
}

const embed = new MessageEmbed()
.setTitle('Test Title:')
.setDescription('Test Message')
.setColor('#0099ff')
.setTimestamp();

try {
const dmChannel = await member.createDM();
await dmChannel.send({ embeds: [embed], components: [row] });
} catch (error) {
console.error('Error sending message:', error);
}
});

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

const selectedLanguage = interaction.customId.split('_')[1];
await interaction.reply(`Your Answer: ${selectedLanguage}`);
});

client.login('TOKEN');
const { Client, Intents, MessageActionRow, MessageButton, MessageEmbed } = require('discord.js');

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.DIRECT_MESSAGES, Intents.FLAGS.DIRECT_MESSAGE_REACTIONS] });

client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});

client.on('guildMemberAdd', async member => {
const flags = [
{ name: 'Test1', emoji: '🇷🇺' },
{ name: 'Test2', emoji: '🇺🇸' },
{ name: 'Test3', emoji: '🇩🇪' },
{ name: 'Test4', emoji: '🇹🇷' },
{ name: 'Test5', emoji: '🇮🇷' },
{ name: 'Test6', emoji: '🇺🇦' },
{ name: 'Test7', emoji: '🇯🇵' }
];

const row = new MessageActionRow();
for (const flag of flags) {
const button = new MessageButton()
.setCustomId(`language_${flag.name}`)
.setLabel(flag.name)
.setStyle('PRIMARY')
.setEmoji(flag.emoji);
row.addComponents(button);
}

const embed = new MessageEmbed()
.setTitle('Test Title:')
.setDescription('Test Message')
.setColor('#0099ff')
.setTimestamp();

try {
const dmChannel = await member.createDM();
await dmChannel.send({ embeds: [embed], components: [row] });
} catch (error) {
console.error('Error sending message:', error);
}
});

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

const selectedLanguage = interaction.customId.split('_')[1];
await interaction.reply(`Your Answer: ${selectedLanguage}`);
});

client.login('TOKEN');
5 Replies
d.js toolkit
d.js toolkit•16mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
treble/luna
treble/luna•16mo ago
pass them in as components: [yourcomponents]
Dmitry Batkovich
Dmitry BatkovichOP•16mo ago
limit 5 buttons? all right bypassed the problem by creating an additional row
Unknown User
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
Dmitry Batkovich
Dmitry BatkovichOP•16mo ago
working:
message.channel.send({
embeds: [exampleEmbed],
components:
[
new ActionRowBuilder().setComponents
(
new ButtonBuilder()
.setCustomId('ruID')
.setLabel('🇷🇺')
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId('usID')
.setLabel('🇺🇸')
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId('deID')
.setLabel('🇩🇪')
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId('trID')
.setLabel('🇹🇷')
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId('irID')
.setLabel('🇮🇷')
.setStyle(ButtonStyle.Primary),
),
new ActionRowBuilder().setComponents
(
new ButtonBuilder()
.setCustomId('uaID')
.setLabel('🇺🇦')
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId('jpID')
.setLabel('🇯🇵')
.setStyle(ButtonStyle.Primary)
)
],
});
message.channel.send({
embeds: [exampleEmbed],
components:
[
new ActionRowBuilder().setComponents
(
new ButtonBuilder()
.setCustomId('ruID')
.setLabel('🇷🇺')
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId('usID')
.setLabel('🇺🇸')
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId('deID')
.setLabel('🇩🇪')
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId('trID')
.setLabel('🇹🇷')
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId('irID')
.setLabel('🇮🇷')
.setStyle(ButtonStyle.Primary),
),
new ActionRowBuilder().setComponents
(
new ButtonBuilder()
.setCustomId('uaID')
.setLabel('🇺🇦')
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId('jpID')
.setLabel('🇯🇵')
.setStyle(ButtonStyle.Primary)
)
],
});
Want results from more Discord servers?
Add your server