You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Discord v14中15秒无交互后禁用按钮?

解决方案

首先明确:Discord v14中,链接样式(ButtonStyle.Link)的按钮无法被禁用,只有带自定义ID的按钮(比如你的「Close」按钮)支持禁用状态设置。下面是修改后的完整代码,实现15秒无交互后禁用可交互按钮:

const config = require('../../botconfig');
const { EmbedBuilder, ButtonBuilder, ButtonStyle, ActionRowBuilder, ComponentType } = require('discord.js');

module.exports = {
name: 'invite',
description: 'Crimson and Server invites',
run: async (client, interaction, args) => {
   try {
   const inviteEmbed = new EmbedBuilder()
      .setDescription('**__Invite • Support__**\n\n<a:Arrow:735141069033046106> Want to invite Crimson to your server? Feel free to click on the **"Invite"** button.\n\n<a:Arrow:735141069033046106> Need additional help with Crimson? Come join us in our humble abode by clicking on the **"Support"** button.')
      .setColor('#EE1C25')
      .setFooter({ text: `Command Requested by: ${interaction.user.tag}`, iconURL: interaction.user.displayAvatarURL() })
      .setTimestamp()
      .setThumbnail(client.user.displayAvatarURL());

   let botInvite = new ButtonBuilder()
      .setStyle(ButtonStyle.Link)
      .setURL(`https://discord.com/`)
      .setLabel('Invite');

   let support = new ButtonBuilder()
      .setStyle(ButtonStyle.Link)
      .setURL('https://discord.gg/')
      .setLabel('Support');
    
   let del = new ButtonBuilder()
      .setLabel(`Close`)
      .setCustomId(`delete`)
      .setEmoji(`❌`)
      .setStyle(ButtonStyle.Danger);

   const inviteMsg = await interaction.reply({ embeds: [inviteEmbed], components: [new ActionRowBuilder().addComponents(botInvite, support, del)], fetchReply: true });

   const collector = inviteMsg.createMessageComponentCollector({ componentType: ComponentType.Button, time: 15000 });

   collector.on('collect', async i => {
      if (i.user.id !== interaction.user.id) {
        await i.reply({ content: `This button is not for you!`, ephemeral: true });
        return;
      }

      console.log(`${i.user.tag} clicked on the ${i.customId} button.`);

      if (i.customId === 'delete') {
         await inviteMsg.delete();
         await i.deferUpdate();
      }
   });
   
   collector.on('end', async collected => {
      console.log(`Collected ${collected.size} interactions.`);
      // 创建禁用状态的Close按钮
      const disabledDel = new ButtonBuilder()
         .setLabel(`Close`)
         .setCustomId(`delete`)
         .setEmoji(`❌`)
         .setStyle(ButtonStyle.Danger)
         .setDisabled(true);

      // 重新构建组件行,链接按钮保持原样
      const disabledRow = new ActionRowBuilder().addComponents(botInvite, support, disabledDel);
      // 更新原消息的组件
      await inviteMsg.edit({ components: [disabledRow] });
   });

   } catch (err) {
     console.log(err);
     return interaction.reply(`\`${err}\`.`);
   }
  }
};

关键修改点:

  • 修正按钮点击判断错误:将i.id === 'delete'改为i.customId === 'delete',自定义按钮的唯一标识是customId而非id。
  • 在collector的end事件中,重新创建Close按钮并设置.setDisabled(true),通过编辑原消息更新组件,实现超时禁用效果。
  • 调整按钮点击后的回复逻辑,用deferUpdate替代错误写法,避免API调用报错。

内容的提问来源于stack exchange,提问作者Roman Beard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 11:36:14