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

Discord.js v13反应角色按钮重启后提示交互失败如何修复

修复方案

问题根源

你当前的实现存在几个核心问题导致重启/超时后按钮失效:

  • 你使用的createMessageComponentCollector是存储在运行内存中的临时收集器,你设置了time: 60000参数,意味着收集器创建60秒后就会自动销毁,哪怕不重启机器人,1分钟后按钮也会失效
  • 机器人重启后运行内存会清空,所有临时创建的collector都会消失,Discord发送的按钮交互请求没有对应逻辑处理,就会返回This interaction failed错误
  • 额外问题:你当前的过滤器设置为仅命令发起者可以点击按钮,其他用户点击按钮也会触发交互失败

修复步骤

  1. 移除原有命令中的临时collector相关代码,改用客户端全局interactionCreate事件处理按钮交互,该事件会在机器人每次启动时注册,重启后依然生效
  2. 调整交互匹配逻辑,通过按钮的customId判断是否为反应角色按钮,取消用户限制

修改后的命令代码

const Command = require("../Structures/Command.js");
const Discord = require('discord.js');
const { MessageButton } = require('discord.js');

module.exports = new Command({
    name: "reactionrolebuttons",
    description: "reaction role but button",
    async run(message, args, client, interaction, collect) {
        let embed = new Discord.MessageEmbed()
            .setTitle("Ping Roles:")
            .setColor("#0099ff")
            .setDescription("`Click some button bellow if you want get notified!`")

        const row = new Discord.MessageActionRow()
            .addComponents(
                new MessageButton()
                    .setCustomId('rr_Acm') // 加前缀方便后续事件过滤,避免和其他按钮冲突
                    .setLabel('Announcement')
                    .setStyle('SECONDARY')
                    .setEmoji("📰"),
                new MessageButton()
                    .setCustomId('rr_Gv')
                    .setLabel('Giveaways')
                    .setStyle('SECONDARY')
                    .setEmoji("🎉")
            )
        await message.channel.send({ embeds: [embed], components: [row] });
    }
});

新增全局交互事件处理代码

将以下代码添加到你的机器人主文件,或者事件监听注册目录中,保证机器人启动时会加载:

client.on('interactionCreate', async interaction => {
    // 只处理按钮交互,且customId以rr_开头的反应角色按钮
    if (!interaction.isButton() || !interaction.customId.startsWith('rr_')) return;

    const roleMap = {
        'rr_Acm': '901353036759310386',
        'rr_Gv': '901370872827346975'
    }
    const targetRoleId = roleMap[interaction.customId];
    if (!targetRoleId) return;

    const role = interaction.guild.roles.cache.get(targetRoleId);
    if (!role) return interaction.reply({ content: '身份组不存在', ephemeral: true });

    if (interaction.member.roles.cache.has(targetRoleId)) {
        await interaction.member.roles.remove(targetRoleId);
        await interaction.reply({ content: `${role} 已从你的身份组中移除`, ephemeral: true });
    } else {
        await interaction.member.roles.add(targetRoleId);
        await interaction.reply({ content: `${role} 已添加到你的身份组中`, ephemeral: true });
    }
})

注意事项

  • 确保你的机器人已开启GUILDS、GUILD_MEMBERS、GUILD_MESSAGE_REACTIONS等必要意图
  • 确保机器人在服务器中的角色层级高于要分配的两个身份组,且拥有管理角色权限

内容的提问来源于stack exchange,提问作者Gia Huy Nguyen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 14:12:02