[英]How do you disable a button after 5 seconds (discord.js)
我對 discord.js 按鈕不是很熟悉,需要幫助在 5 秒后禁用按鈕以避免垃圾郵件,但我不知道如何。 這是我的代碼:
const newEmbed = new Discord.MessageEmbed()
.setColor('#2ACAEA')
.setTitle("Commands")
.setDescription("Page 1 of 2")
.addFields(
{name: '!jn help', value: 'This command'},
{name: '!jn ping', value: 'Ping the bot.'},
{name: '!jn coinflip', value: 'Flip a coin :D'},
{name: '!jn amiadmin', value: 'Are you an admin? We can see!'}
)
.setFooter('Need more help? Join the support discord server.')
.setImage('https://i.imgur.com/Ta0yst7.png');
const row = new Discord.MessageActionRow()
.addComponents(
new Discord.MessageButton()
.setCustomId('2')
.setLabel('>>')
.setStyle('SUCCESS'),
);
message.channel.send({embeds: [newEmbed], components: [row]});
setTimeout(function () {
row.components[0].setDisabled(true);
}, 5000);
當我嘗試時,它不會輸出任何錯誤。 我想我只是錯過了一些東西。 有人能幫我嗎?
您走在正確的軌道上 - 您已經完成了第一步,即將按鈕更改為在本地5 秒后禁用,但現在您需要編輯您的消息以在線反映這些更改。
首先,您必須將發送的消息存儲在一個變量中:
const sentMessage = await message.channel.send({ embeds: [newEmbed], components: [row] });
然后,在您的setTimeout
,您可以像 Dregg 在他的評論中所說的那樣編輯sentMessage
:
// After you disable the button
sentMessage.edit({ embeds: [newEmbed], components: [row] });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.