Return Embeds and Button
อยากให้มันแสดงอย่างอื่นที่ไม่ใช่ text ได้ไหม? ได้! เราสามารถใช้ Embed และ Button ในการแสดงผลที่สวยงามขึ้นได้นะ
Embed
ก่อนอื่นเพิ่ม EmbedBuilder กันก่อน
const { ..., EmbedBuilder } = require('discord.js');
ไปที่เรา handle คำสั่ง animal
// 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] });
}
});
มาอธิบายแต่ละส่วนกัน

setColor()
: set สีของตัว Embed ซึ่งจะแสดงเป็นเส้นด้านซ้ายsetTitle()
: ใส่ title ในที่นี้เป็น TigersetURL()
: กดที่ title แล้วไปหน้าเว็บไหนต่อsetAuthor()
: ส่งโดยใคร ในที่นี้ชื่อ zookeeper เป็นรูปหมูเด้ง กดที่ author แล้วไปหน้าเว็บไหนต่อsetDescription()
: ใส่รายละเอียด ในที่นี้บอกรายละเอียดเสือโคร่งsetThumbnail()
: รูป thumbnail ซึ่งจะแสดงที่มุมขวาล่างaddFields()
: เพิ่มการแสดง text ในส่วนหัวข้อย่อย ในที่นี้จะเป็น Scientific Name, Food และ Zoo PlacesetImage()
: ใส่รูปใหญ่ ๆ ด้านล่างsetTimestamp()
: บอกว่าข้อความนี้ถูกส่งมาเวลาไหนsetFooter()
: ด้านล่างจะแสดง text หรือ icon อะไร
เกี่ยวกับ Embed สามารถอ่านเพิ่มเติมได้ที่นี่
Button
เพิ่ม Button กัน เพิ่ม ButtonBuilder
, ActionRowBuilder
, ButtonStyle
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] });
}
});
setLabel()
: เราอยากให้แสดง text อะไรsetStyle()
: style ของปุ่ม ในที่นี้คลิกเพื่อเข้าหน้า website จึงเป็น ButtonStyle.LinksetURL()
: เราอยากให้เปิดหน้าเว็บไหน
ตอนที่ bot reply ให้แสดง Embed และสร้าง component row ที่ข้างในมี Button ที่เราเพิ่งสร้างนั่นเอง
เกี่ยวกับ Button อ่านเพิ่มเติมได้ที่นี่
Last updated