Editing a button issue

So im basically trying to make a slash command that edits one of 2 buttons in Embed Message. And after invoking command, it throws this error: components[0].components[BASE_TYPE_BAD_LENGTH]: Must be between 1 and 1521 in length. at message.edit() How I can do that in a correct way? Is that even possible? Here's my code:
const { SlashCommandBuilder, PermissionFlagsBits } = require("discord.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("button")
.setDescription("block or unblock")
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.addBooleanOption((option) =>
option
.setName("state")
.setDescription("Block or unblock")
.setRequired(true)
)
.addStringOption((option) =>
option
.setName("button")
.setDescription("which one?")
.setRequired(true)
),

async execute(interaction) {
const { options, channel } = interaction;
const buttonState = options.getBoolean("state");
const buttonName = options.getString("button");

let buttonIndex;

if (buttonName.toLowerCase() === "first") {
buttonIndex = 1;
} else if (buttonName.toLowerCase() === "second") {
buttonIndex = 0;
} else {
return interaction.reply({
content: "Incorrect name!",
ephemeral: true,
});
}

const message = await channel.messages.fetch("1120412253901045832");

if (!message) {
return interaction.reply({
content: "No messages!",
ephemeral: true,
});
}

const buttons = message.components[0].components;
const buttonToEdit = buttons[buttonIndex];

buttonToEdit.disabled = buttonState;
buttonToEdit.style = buttonState ? "SUCCESS" : "DANGER";

await message.edit({ components: [buttons] });

interaction.reply({
content: "Button updated!",
ephemeral: true,
});
},
};
const { SlashCommandBuilder, PermissionFlagsBits } = require("discord.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("button")
.setDescription("block or unblock")
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.addBooleanOption((option) =>
option
.setName("state")
.setDescription("Block or unblock")
.setRequired(true)
)
.addStringOption((option) =>
option
.setName("button")
.setDescription("which one?")
.setRequired(true)
),

async execute(interaction) {
const { options, channel } = interaction;
const buttonState = options.getBoolean("state");
const buttonName = options.getString("button");

let buttonIndex;

if (buttonName.toLowerCase() === "first") {
buttonIndex = 1;
} else if (buttonName.toLowerCase() === "second") {
buttonIndex = 0;
} else {
return interaction.reply({
content: "Incorrect name!",
ephemeral: true,
});
}

const message = await channel.messages.fetch("1120412253901045832");

if (!message) {
return interaction.reply({
content: "No messages!",
ephemeral: true,
});
}

const buttons = message.components[0].components;
const buttonToEdit = buttons[buttonIndex];

buttonToEdit.disabled = buttonState;
buttonToEdit.style = buttonState ? "SUCCESS" : "DANGER";

await message.edit({ components: [buttons] });

interaction.reply({
content: "Button updated!",
ephemeral: true,
});
},
};
5 Replies
d.js toolkit
d.js toolkit12mo ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
edocsil
edocsil12mo ago
Use the ButtonStyle enum instead of "SUCCESS" or "DANGER"
d.js docs
d.js docs12mo ago
RangeError [BitFieldInvalid]: Invalid bitfield flag or number: undefined - All SCREAMING_SNAKE_CASE enums have been changed to PascalCase - Intents: Intents.FLAGS.GUILD_MESSAGES -> GatewayIntentBits.GuildMessages - Permissions: Permissions.FLAGS.SEND_MESSAGES -> PermissionFlagsBits.SendMessages
edocsil
edocsil12mo ago
Also modifying the button on the existing message object is a bad idea because you're intentionally making your cache desynced. Better to clone it with a new ButtonBuilder
pan andrzej
pan andrzej12mo ago
ok then I will do that