อยากให้มันแสดงอย่างอื่นที่ไม่ใช่ text ได้ไหม? ได้! เราสามารถใช้ Embed และ Button ในการแสดงผลที่สวยงามขึ้นได้นะ
const { ..., EmbedBuilder } = require('discord.js');
// Reply command
client.on('interactionCreate', async interaction => {
...
if (commandName === 'ping') {
...
} else if (commandName === 'animal') {
const animalInput = interaction.options.getString('animal');
const animalOutput = animals.find(animal => animal.name_en == animalInput);
const embed = new EmbedBuilder()
.setColor(0x0099FF)
.setTitle(animalOutput.name_en)
.setURL(animalOutput.reference_url)
.setAuthor({
name: 'zookeeper',
iconURL: "https://i.imgur.com/dxeXsqc.jpeg",
url: 'https://khaokheow.zoothailand.org/intro.php'
})
.setDescription(animalOutput.description)
.setThumbnail(animalOutput.image_url)
.addFields(
{ name: 'Scientific Name', value: animalOutput.scientific_name, inline: true },
{ name: 'Food', value: animalOutput.diet },
{ name: 'Zoo Place', value: animalOutput.place, inline: true },
)
.setImage(animalOutput.image_url)
.setTimestamp()
.setFooter({ text: '❤️', iconURL: "https://i.imgur.com/dxeXsqc.jpeg" }
);
await interaction.reply({ embeds: [embed] });
}
});
const { ..., ButtonBuilder, ActionRowBuilder, ButtonStyle} = require('discord.js');
// Reply command
client.on('interactionCreate', async interaction => {
...
if (commandName === 'ping') {
...
} else if (commandName === 'animal') {
const animalInput = interaction.options.getString('animal');
const animalOutput = animals.find(animal => animal.name_en == animalInput);
const embed = new EmbedBuilder()
.setColor(0x0099FF)
.setTitle(animalOutput.name_en)
.setURL(animalOutput.reference_url)
.setAuthor({
name: 'zookeeper',
iconURL: "https://i.imgur.com/dxeXsqc.jpeg",
url: 'https://khaokheow.zoothailand.org/intro.php'
})
.setDescription(animalOutput.description)
.setThumbnail(animalOutput.image_url)
.addFields(
{ name: 'Scientific Name', value: animalOutput.scientific_name, inline: true },
{ name: 'Food', value: animalOutput.diet },
{ name: 'Zoo Place', value: animalOutput.place, inline: true },
)
.setImage(animalOutput.image_url)
.setTimestamp()
.setFooter({ text: '❤️', iconURL: "https://i.imgur.com/dxeXsqc.jpeg" }
);
const button = new ButtonBuilder()
.setLabel('ข้อมูลเพิ่มเติม')
.setStyle(ButtonStyle.Link)
.setURL(animalOutput.reference_url);
const row = new ActionRowBuilder().addComponents(button);
await interaction.reply({ embeds: [embed], components: [row] });
}
});
ตอนที่ bot reply ให้แสดง Embed และสร้าง component row ที่ข้างในมี Button ที่เราเพิ่งสร้างนั่นเอง