ButtonBuilder > TypeError: Cannot read properties of undefined (reading 'customId')

Part of Index.js
const { componentTogglerIds } = require('./commands/main/start-contract');
const buttonToggleCounters = {};
...
if (interaction.isButton()) {
try {
const buttonIndex = componentTogglerIds.indexOf(interaction.customId);

if (buttonIndex !== -1) {
const styles = [ButtonStyle.Success, ButtonStyle.Danger, ButtonStyle.Primary];

// Get the toggle counter for the specific button or initialize it to 0
let toggleCounter = buttonToggleCounters[interaction.customId] || 0;

// Toggle the button style
const newStyle = styles[toggleCounter % styles.length];

// Get the current button details

const currentButton = interaction.message.components[0].components[buttonIndex];
console.log(currentButton)
// HERE IT CRASHES AND CONSOLE LOG RETURNS UNDEFINED. EVERYWHERE BEFORE IT RETURNS PROPPER ID
const currentId = currentButton.customId;
const currentLabel = currentButton.label;

// Increment the toggle counter for the specific button
buttonToggleCounters[interaction.customId] = (toggleCounter + 1) % styles.length;

// Create a new button with the updated style
const newButton = new ButtonBuilder()
.setCustomId(currentId)
.setLabel(currentLabel)
.setStyle(newStyle);

// Replace the existing button in the MessageActionRow
interaction.message.components[0].components[buttonIndex] = newButton;

// Edit the message with the updated button style
await interaction.update({
components: [interaction.message.components[0]],
});
}
} catch (error) {
console.error(error);
}}
const { componentTogglerIds } = require('./commands/main/start-contract');
const buttonToggleCounters = {};
...
if (interaction.isButton()) {
try {
const buttonIndex = componentTogglerIds.indexOf(interaction.customId);

if (buttonIndex !== -1) {
const styles = [ButtonStyle.Success, ButtonStyle.Danger, ButtonStyle.Primary];

// Get the toggle counter for the specific button or initialize it to 0
let toggleCounter = buttonToggleCounters[interaction.customId] || 0;

// Toggle the button style
const newStyle = styles[toggleCounter % styles.length];

// Get the current button details

const currentButton = interaction.message.components[0].components[buttonIndex];
console.log(currentButton)
// HERE IT CRASHES AND CONSOLE LOG RETURNS UNDEFINED. EVERYWHERE BEFORE IT RETURNS PROPPER ID
const currentId = currentButton.customId;
const currentLabel = currentButton.label;

// Increment the toggle counter for the specific button
buttonToggleCounters[interaction.customId] = (toggleCounter + 1) % styles.length;

// Create a new button with the updated style
const newButton = new ButtonBuilder()
.setCustomId(currentId)
.setLabel(currentLabel)
.setStyle(newStyle);

// Replace the existing button in the MessageActionRow
interaction.message.components[0].components[buttonIndex] = newButton;

// Edit the message with the updated button style
await interaction.update({
components: [interaction.message.components[0]],
});
}
} catch (error) {
console.error(error);
}}
20 Replies
d.js toolkit
d.js toolkit11mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
Not Kuma anymore
Not Kuma anymoreOP11mo ago
start-contract.js
No description
No description
Not Kuma anymore
Not Kuma anymoreOP11mo ago
Freezing button only returns error. Other buttons work properly. Changing label and id still gives same kind of error
SpecialSauce
SpecialSauce11mo ago
Show the error and stack trace.
Not Kuma anymore
Not Kuma anymoreOP11mo ago
undefined
TypeError: Cannot read properties of undefined (reading 'customId')
at Client.<anonymous> (C:\Users\lukas\Desktop\PhasmoBot\PoltergeistBot\index.js:144:36)
at Client.emit (node:events:514:28)
at InteractionCreateAction.handle (C:\Users\lukas\Desktop\PhasmoBot\PoltergeistBot\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
at module.exports [as INTERACTION_CREATE] (C:\Users\lukas\Desktop\PhasmoBot\PoltergeistBot\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (C:\Users\lukas\Desktop\PhasmoBot\PoltergeistBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:355:31)
at WebSocketManager.<anonymous> (C:\Users\lukas\Desktop\PhasmoBot\PoltergeistBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:239:12)
at WebSocketShard.onMessage (C:\Users\lukas\Desktop\PhasmoBot\PoltergeistBot\node_modules\@discordjs\ws\dist\index.js:988:14)
undefined
TypeError: Cannot read properties of undefined (reading 'customId')
at Client.<anonymous> (C:\Users\lukas\Desktop\PhasmoBot\PoltergeistBot\index.js:144:36)
at Client.emit (node:events:514:28)
at InteractionCreateAction.handle (C:\Users\lukas\Desktop\PhasmoBot\PoltergeistBot\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
at module.exports [as INTERACTION_CREATE] (C:\Users\lukas\Desktop\PhasmoBot\PoltergeistBot\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (C:\Users\lukas\Desktop\PhasmoBot\PoltergeistBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:355:31)
at WebSocketManager.<anonymous> (C:\Users\lukas\Desktop\PhasmoBot\PoltergeistBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:239:12)
at WebSocketShard.onMessage (C:\Users\lukas\Desktop\PhasmoBot\PoltergeistBot\node_modules\@discordjs\ws\dist\index.js:988:14)
SpecialSauce
SpecialSauce11mo ago
What is index.js line 144?
Not Kuma anymore
Not Kuma anymoreOP11mo ago
const currentId = currentButton.customId;
SpecialSauce
SpecialSauce11mo ago
CurrentButton is undefined at that point. You should log some values and find where your logic isn’t finding what you expect.
Not Kuma anymore
Not Kuma anymoreOP11mo ago
check comment above line 144 it properly returns id of freezing everywhere above ok, i'll show u smth i've found rn
Not Kuma anymore
Not Kuma anymoreOP11mo ago
console log near 144
No description
Not Kuma anymore
Not Kuma anymoreOP11mo ago
other buttons vs last freezing
SpecialSauce
SpecialSauce11mo ago
Right it’s undefined.
Not Kuma anymore
Not Kuma anymoreOP11mo ago
buttonindex for first 3 buttons are 0, 1 and 2 but for freezing its 5
SpecialSauce
SpecialSauce11mo ago
And how many buttons does that row have?
Not Kuma anymore
Not Kuma anymoreOP11mo ago
yeah, it should return 3, i've changed it manually to 3 and it works, but buttonindex returns 5 4 buttons
Not Kuma anymore
Not Kuma anymoreOP11mo ago
No description
Not Kuma anymore
Not Kuma anymoreOP11mo ago
so indexes should be 0; 1; 2; 3 not 0; 1; 2; 5 hm i think i know let me check
SpecialSauce
SpecialSauce11mo ago
Check componentTogglerIds.
Not Kuma anymore
Not Kuma anymoreOP11mo ago
yeah, it depends on order that i've set wrong xD
No description
Not Kuma anymore
Not Kuma anymoreOP11mo ago
So it's solved. Wrong order inside of object. Thanks @SpecialSauce 1heartcute
Want results from more Discord servers?
Add your server