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

Discord按钮交互失败:点击按钮无法授予角色问题求助

问题解决:Discord按钮交互失败(This interaction failed)

核心问题分析

你的代码存在两个关键问题导致按钮交互失败:

  1. 收集器绑定错误:你在命令执行的频道创建了组件收集器,但实际带按钮的Embed是发送到ndaChannel的,收集器无法监听其他频道的按钮点击,导致Discord服务器端超时返回交互失败。
  2. 成员数据获取不可靠:从guild.members.cache获取用户数据可能因为缓存未更新导致获取失败,进而引发后续操作出错。

修改方案

1. 绑定收集器到发送的Embed消息

将收集器创建在发送到ndaChannel的消息上,而不是命令执行的频道:

// 替换原来的collector创建和消息发送代码
interaction.reply({ content: "I have posted the NDA embed!", ephemeral: true });
// 保存发送的消息对象
const sentMessage = await ndaChannel.send({ embeds: [ndaEmbed], components: [row] });
// 绑定收集器到这条消息
const collector = sentMessage.createMessageComponentCollector({
    componentType: ComponentType.Button
});

2. 可靠获取成员数据

用interactionButton.member直接获取点击按钮的成员对象,替代从缓存获取:

// 替换collector里的userData获取代码
let userData = interactionButton.member;

3. 添加错误处理(可选但推荐)

为角色操作添加错误捕获,避免静默失败:

case "nda":
    if (userData.roles.cache.has(ndaRole.id)) {
        message = `You already have the ${ndaRole} role!`;
    } else {
        try {
            await userData.roles.add(ndaRole);
            message = `You now have the ${ndaRole} role!`;
        } catch (error) {
            message = "Failed to assign role, please check bot permissions!";
            console.error(error); // 输出错误到控制台便于排查
        }
    }
    return interactionButton.reply({ content: `${message}`, ephemeral: true });

完整修改后的代码片段

const { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits, ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType } = require('discord.js');

module.exports = {
    category: "command",
    data: new SlashCommandBuilder()
        .setName('nda')
        .setDescription('Make the NDA embed + button for the role!')
        .setDefaultMemberPermissions(PermissionFlagsBits.ManageWebhooks),
    async execute(client, interaction) {

        const ndaRole = interaction.guild.roles.cache.get('1099050320660799609');
        if (!ndaRole) {
            return interaction.reply({ content: "NDA role not found!", ephemeral: true });
        }

        const ndaChannel = client.channels.cache.get('1145302828408111125');
        if (!ndaChannel) {
            return interaction.reply({ content: "NDA channel not found!", ephemeral: true });
        }

        const ndaEmbed = new EmbedBuilder()
            .setTitle('Instant Editing Staff NDA')
            .setColor('#e00c45')
            .addFields(
                { name: '**1. __Disclaimers__**', value: '...' },
                { name: '...', value: '...' }
                // 补充你的其他字段
            );

        const row = new ActionRowBuilder().addComponents(
            new ButtonBuilder()
                .setCustomId("nda")
                .setLabel("Confirm That You Have Read The NDA")
                .setEmoji("1099073509692543068")
                .setStyle(ButtonStyle.Success)
        );

        interaction.reply({ content: "I have posted the NDA embed!", ephemeral: true });
        // 保存发送的消息并绑定收集器
        const sentMessage = await ndaChannel.send({ embeds: [ndaEmbed], components: [row] });
        const collector = sentMessage.createMessageComponentCollector({
            componentType: ComponentType.Button
        });

        collector.on("collect", async (interactionButton) => {
            let id = interactionButton.customId;
            let userData = interactionButton.member;
            let message = "";

            switch (id) {
                case "nda":
                    if (userData.roles.cache.has(ndaRole.id)) {
                        message = `You already have the ${ndaRole} role!`;
                    } else {
                        try {
                            await userData.roles.add(ndaRole);
                            message = `You now have the ${ndaRole} role!`;
                        } catch (error) {
                            message = "Failed to assign role, please check bot permissions!";
                            console.error(error);
                        }
                    }
                    return interactionButton.reply({ content: `${message}`, ephemeral: true });
            }
        });
    },
};

额外检查项

  • 确保机器人拥有Manage Roles权限,并且机器人的角色位置高于ndaRole,否则无法给用户添加该角色。
  • 确认ndaRole和ndaChannel的ID完全正确,避免因ID错误导致对象获取失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 03:52:03