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

Discord.js按钮禁用报错:b.setDisabled is not a function解决方案咨询

解决Discord.js按钮禁用报错b.setDisabled is not a function的问题

问题根源

你遇到的报错来自两个核心错误:

  • 直接对ActionRowBuilder实例调用forEach方法,但ActionRowBuilder本身没有这个方法,你需要遍历它的components属性(存储按钮的数组)。
  • 在collector的end事件中,错误地将row包裹在数组[row]里遍历,此时遍历的是ActionRowBuilder对象而非按钮组件,而ActionRowBuilder没有setDisabled方法。

修复方案

将所有遍历按钮的代码从row.forEach()改为row.components.forEach();同时修正end事件中的遍历逻辑,直接操作按钮数组。

修改后的完整代码

const {
    SlashCommandBuilder,
    EmbedBuilder,
    ButtonStyle,
    ButtonBuilder,
    ActionRowBuilder,
    ActionRow,
} = require("discord.js");

module.exports = {
    data: new SlashCommandBuilder()
        .setName("highlow")
        .setDescription("👇 | Starts a new high or low game!"),
    async execute(interaction) {
        const randomNumber = Math.floor(Math.random() * 100) + 1;
        const hintNumber = Math.floor(Math.random() * 100) + 1;
        const row = new ActionRowBuilder().addComponents(
            new ButtonBuilder()
                .setLabel("High")
                .setStyle(ButtonStyle.Secondary)
                .setCustomId("high"),
            new ButtonBuilder()
                .setCustomId("low")
                .setLabel("Low")
                .setStyle(ButtonStyle.Secondary),
            new ButtonBuilder()
                .setCustomId("correct")
                .setLabel("Same")
                .setStyle(ButtonStyle.Secondary)
        );
        const sent = await interaction.reply({
            content: `Is my number higher or lower than ${hintNumber}?`,
            components: [row],
        });
        const collector = sent.createMessageComponentCollector({
            filter: (i) =>
                i.user.id === interaction.user.id && i.message.id === sent.id,
            time: 30000,
            max: 1,
        });
        let won = false;
        collector.on("collect", async (i) => {
            await i.deferUpdate({ fetchReply: true });
            // 修复:遍历row的components数组
            row.components.forEach((b) => b.setDisabled(true));
            if (i.customId === "high") {
                if (hintNumber > randomNumber) {
                    // 修复:遍历row的components数组
                    row.components.forEach((b) => {
                        if (b.customId === "high") b.setStyle(ButtonStyle.Danger);
                    });
                    await interaction.editReply({
                        content: `Sadge, you got it wrong! It was ${randomNumber}`,
                        components: [row],
                    });
                    won = false;
                } else {
                    // 修复:遍历row的components数组
                    row.components.forEach((b) => {
                        if (b.customId === "high") b.setStyle(ButtonStyle.Success);
                    });
                    await interaction.editReply({
                        content: `You guessed the number! It was ${randomNumber}`,
                        components: [row],
                    });
                    won = true;
                }
            } else if (i.customId === "low") {
                if (hintNumber < randomNumber) {
                    // 修复:遍历row的components数组
                    row.components.forEach((b) => {
                        if (b.customId === "low") b.setStyle(ButtonStyle.Danger);
                    });
                    await interaction.editReply({
                        content: `Sadge, you got it wrong! It was ${randomNumber}`,
                        components: [row],
                    });
                    won = false;
                } else {
                    // 修复:遍历row的components数组
                    row.components.forEach((b) => {
                        if (b.customId === "low") b.setStyle(ButtonStyle.Success);
                    });
                    await interaction.editReply({
                        content: `You are right! It was ${randomNumber}`,
                        components: [row],
                    });
                    won = true;
                }
            } else if (i.customId === "correct" && hintNumber === randomNumber) {
                // 修复:遍历row的components数组
                row.components.forEach((b) => {
                    if (b.customId === "correct") b.setStyle(ButtonStyle.Success);
                });
                await interaction.editReply({
                    content: `You guessed the number! It was ${randomNumber}`,
                    components: [row],
                });
                won = true;
            } else {
                await interaction.editReply({
                    content: `Sadge, you got it wrong! It was ${randomNumber}`,
                    components: [row],
                });
                won = false;
            }
        });
        collector.on("end", async (collected) => {
            if (!won && collected.size === 0) {
                // 修复:遍历row的components数组,而非包裹row的数组
                row.components.forEach((b) => b.setDisabled(true));
                await interaction.editReply({
                    content: `You didn't guess the number! It was ${randomNumber}`,
                    components: [row],
                });
            }
        });
    },
};

关键修改点

  1. 所有row.forEach()替换为row.components.forEach(),确保遍历的是按钮组件数组。
  2. 移除end事件中row的数组包裹[row],改为直接遍历row.components。

修改后即可正确调用按钮的setDisabled方法,实现点击后禁用所有按钮的功能。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 23:35:21