pan andrzej
pan andrzej
Explore posts from servers
DIAdiscord.js - Imagine an app
Created by pan andrzej on 6/19/2023 in #djs-questions
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,
});
},
};
6 replies