简体   繁体   English

你如何在 5 秒后禁用按钮(discord.js)

[英]How do you disable a button after 5 seconds (discord.js)

I'm not very familiar with discord.js buttons and need help disabling a button after 5 seconds to avoid spam but I don't know how.我对 discord.js 按钮不是很熟悉,需要帮助在 5 秒后禁用按钮以避免垃圾邮件,但我不知道如何。 Here is my code:这是我的代码:

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);

When I try, it doesn't output any errors.当我尝试时,它不会输出任何错误。 I think I'm just missing something.我想我只是错过了一些东西。 Can someone help me?有人能帮我吗?

You're on the right track - you've done the first step, which is to change the button to be disabled after 5 seconds locally , but now you need to edit your message to reflect those changes online.您走在正确的轨道上 - 您已经完成了第一步,即将按钮更改为在本地5 秒后禁用,但现在您需要编辑您的消息以在线反映这些更改。

First, you have to store your sent message in a variable:首先,您必须将发送的消息存储在一个变量中:

const sentMessage = await message.channel.send({ embeds: [newEmbed], components: [row] });

Then, in your setTimeout , you can edit the sentMessage like Dregg said in his comment:然后,在您的setTimeout ,您可以像 Dregg 在他的评论中所说的那样编辑sentMessage

// After you disable the button
sentMessage.edit({ embeds: [newEmbed], components: [row] });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM