Having issues with embed buttons / parsing custom IDs

I'm fairly new to this so excuse if I'm not clear enough- This is the error I'm getting:
Button interaction received: suggestion
Unknown context: null
Button interaction received: suggestion
Unknown context: null
When I hit any button on my ticket embed. Other embed buttons work fine. Parsing function:
function parseCustomId(customId) {
const parts = customId.split('_');

if (parts.length === 1) {
return {
action: parts[0], // The action is the only part
currentPage: null, // No current page for single-part IDs
userId: null, // No user ID for single-part IDs
context: null // No context for single-part IDs
};
}

if (parts.length >= 3) {
return {
action: parts[0], // e.g., 'equip', 'unequip'
currentPage: parts[1] ? parseInt(parts[1], 10) : null, // Ensure it's a number
userId: parts[2] || null, // e.g., user ID, or null if missing
context: parts[3] || null // e.g., 'cyberware', or null if missing
};
}

// Handle unexpected custom ID formats
console.error(`Unexpected custom ID format: ${customId}`);
return {
action: null,
currentPage: null,
userId: null,
context: null
};
}

function isSinglePartCustomId(customId) {
return !customId.includes('_');
}
function parseCustomId(customId) {
const parts = customId.split('_');

if (parts.length === 1) {
return {
action: parts[0], // The action is the only part
currentPage: null, // No current page for single-part IDs
userId: null, // No user ID for single-part IDs
context: null // No context for single-part IDs
};
}

if (parts.length >= 3) {
return {
action: parts[0], // e.g., 'equip', 'unequip'
currentPage: parts[1] ? parseInt(parts[1], 10) : null, // Ensure it's a number
userId: parts[2] || null, // e.g., user ID, or null if missing
context: parts[3] || null // e.g., 'cyberware', or null if missing
};
}

// Handle unexpected custom ID formats
console.error(`Unexpected custom ID format: ${customId}`);
return {
action: null,
currentPage: null,
userId: null,
context: null
};
}

function isSinglePartCustomId(customId) {
return !customId.includes('_');
}
Button handler:
async function handleButtonInteraction(interaction) {
try {
const { customId } = interaction;
console.log(`Button interaction received: ${customId}`);

if (isSinglePartCustomId(customId)) {
console.log(`Handling single-part custom ID: ${customId}`);
return await handleSinglePartCustomId(interaction, customId);
}

const { action, currentPage, userId, context } = parseCustomId(customId);

if (!action || !context || !userId) {
console.error(`Invalid custom ID format: ${customId}`);
return await interaction.reply({ content: '⚠ Invalid button interaction format.', ephemeral: true });
}

console.log(`Parsed customId -> Action: ${action}, Page: ${currentPage}, UserId: ${userId}, Context: ${context}`);

const profilesData = JSON.parse(await fs.promises.readFile('./profiles.json', 'utf8'));
const userData = profilesData[userId];

if (!userData) {
return await interaction.reply({ content: 'Profile not found.', ephemeral: true });
}

switch (context) {
case 'cyberware':
await handleCyberwareAction(interaction, action, userData, currentPage, profilesData, userId);
break;

case 'store':
await handleStoreAction(interaction, action, currentPage);
... other cases

});
}
} catch (error) {
console.error(`⚠ Error during handleButtonInteraction: ${error.message}`);
await interaction.reply({ content: '⚠ An error occurred while processing your request.', ephemeral: true });
}
}

async function handleSinglePartCustomId(interaction, action) {

switch (action) {
case 'verify':
await interaction.reply({
content: 'You can send your Year of Birth in chat and wait for a staff member to manually verify you. No need to send any ID or private details.',
ephemeral: true
});
break;
case 'partner':
await interaction.reply({
content: 'For partnerships, please DM <@1256824247515480115> directly. Include your ad and avoid using everyone or here pings.',
ephemeral: true
});
break;
case 'giveaway':
await interaction.reply({
content: 'Congratulations! Please wait for a staff member to assist you with your giveaway claim.',
ephemeral: true
});
break;
case 'suggestion':
await interaction.reply({
content: 'Please write your suggestion, and a staff member will review it shortly.',
ephemeral: true
});
break;
default:
await interaction.reply({ content: '⚠ Unknown action.', ephemeral: true });
}
}
async function handleButtonInteraction(interaction) {
try {
const { customId } = interaction;
console.log(`Button interaction received: ${customId}`);

if (isSinglePartCustomId(customId)) {
console.log(`Handling single-part custom ID: ${customId}`);
return await handleSinglePartCustomId(interaction, customId);
}

const { action, currentPage, userId, context } = parseCustomId(customId);

if (!action || !context || !userId) {
console.error(`Invalid custom ID format: ${customId}`);
return await interaction.reply({ content: '⚠ Invalid button interaction format.', ephemeral: true });
}

console.log(`Parsed customId -> Action: ${action}, Page: ${currentPage}, UserId: ${userId}, Context: ${context}`);

const profilesData = JSON.parse(await fs.promises.readFile('./profiles.json', 'utf8'));
const userData = profilesData[userId];

if (!userData) {
return await interaction.reply({ content: 'Profile not found.', ephemeral: true });
}

switch (context) {
case 'cyberware':
await handleCyberwareAction(interaction, action, userData, currentPage, profilesData, userId);
break;

case 'store':
await handleStoreAction(interaction, action, currentPage);
... other cases

});
}
} catch (error) {
console.error(`⚠ Error during handleButtonInteraction: ${error.message}`);
await interaction.reply({ content: '⚠ An error occurred while processing your request.', ephemeral: true });
}
}

async function handleSinglePartCustomId(interaction, action) {

switch (action) {
case 'verify':
await interaction.reply({
content: 'You can send your Year of Birth in chat and wait for a staff member to manually verify you. No need to send any ID or private details.',
ephemeral: true
});
break;
case 'partner':
await interaction.reply({
content: 'For partnerships, please DM <@1256824247515480115> directly. Include your ad and avoid using everyone or here pings.',
ephemeral: true
});
break;
case 'giveaway':
await interaction.reply({
content: 'Congratulations! Please wait for a staff member to assist you with your giveaway claim.',
ephemeral: true
});
break;
case 'suggestion':
await interaction.reply({
content: 'Please write your suggestion, and a staff member will review it shortly.',
ephemeral: true
});
break;
default:
await interaction.reply({ content: '⚠ Unknown action.', ephemeral: true });
}
}
Is there anything that stands out to anyone that could potentially cause the error I'm receiving?
No description
8 Replies
d.js toolkit
d.js toolkit4mo 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 staff
chewie
chewie4mo ago
where does it log Unknown context tho
𝙚𝙡𝙮𝙣𝙖𝙧𝙞
here:
async function handleSinglePartCustomId(interaction, action) {

switch (action) {
case 'verify':
await interaction.reply({
content: 'You can send your Year of Birth in chat and wait for a staff member to manually verify you. No need to send any ID or private details.',
ephemeral: true
});
break;
case 'partner':
await interaction.reply({
content: 'For partnerships, please DM <@1256824247515480115> directly. Include your ad and avoid using everyone or here pings.',
ephemeral: true
});
break;
case 'giveaway':
await interaction.reply({
content: 'Congratulations! Please wait for a staff member to assist you with your giveaway claim.',
ephemeral: true
});
break;
case 'suggestion':
await interaction.reply({
content: 'Please write your suggestion, and a staff member will review it shortly.',
ephemeral: true
});
break;
default:
console.error(`Unknown action for single-part customId: ${action}`);
await interaction.reply({ content: '⚠ Unknown action.', ephemeral: true });
}
}
async function handleSinglePartCustomId(interaction, action) {

switch (action) {
case 'verify':
await interaction.reply({
content: 'You can send your Year of Birth in chat and wait for a staff member to manually verify you. No need to send any ID or private details.',
ephemeral: true
});
break;
case 'partner':
await interaction.reply({
content: 'For partnerships, please DM <@1256824247515480115> directly. Include your ad and avoid using everyone or here pings.',
ephemeral: true
});
break;
case 'giveaway':
await interaction.reply({
content: 'Congratulations! Please wait for a staff member to assist you with your giveaway claim.',
ephemeral: true
});
break;
case 'suggestion':
await interaction.reply({
content: 'Please write your suggestion, and a staff member will review it shortly.',
ephemeral: true
});
break;
default:
console.error(`Unknown action for single-part customId: ${action}`);
await interaction.reply({ content: '⚠ Unknown action.', ephemeral: true });
}
}
if thats what you mean? sorry if i misunderstand
chewie
chewie4mo ago
It's logging these 2 lines, one is visible in the code you provided, the other is not.
No description
𝙚𝙡𝙮𝙣𝙖𝙧𝙞
ahh, here I believe:
client.on('interactionCreate', async (interaction) => {
if (interaction.isButton() || interaction.isCommand()) {
try {
if (interaction.isCommand()) {
console.log(`Command interaction received: ${interaction.commandName}`);
await handleCommandInteraction(interaction);
} else if (interaction.isButton()) {
console.log(`Button interaction received: ${interaction.customId}`);

const { action, currentPage, userId, context } = parseCustomId(interaction.customId);

switch (context) {
case 'store':
await handleCyberwareInteraction(interaction, action, currentPage);
break;
case 'unverified':
await handleUnverifiedPagination(interaction, action, currentPage);
break;
case 'profile':
await handleProfileButtonInteraction(interaction, action, userId);
break;
default:
console.error(`Unknown context: ${context}`);
return await interaction.reply({ content: 'An error occurred while processing the button interaction.', ephemeral: true });
}
}
} catch (error) {
console.error(`Error handling interaction: ${error.message}`);
if (!interaction.replied && !interaction.deferred) {
await interaction.reply({ content: '⚠ There was an error while executing this command!', ephemeral: true });
} else if (interaction.deferred) {
await interaction.editReply({ content: '⚠ There was an error while executing this command!', ephemeral: true });
}
}
}
});
client.on('interactionCreate', async (interaction) => {
if (interaction.isButton() || interaction.isCommand()) {
try {
if (interaction.isCommand()) {
console.log(`Command interaction received: ${interaction.commandName}`);
await handleCommandInteraction(interaction);
} else if (interaction.isButton()) {
console.log(`Button interaction received: ${interaction.customId}`);

const { action, currentPage, userId, context } = parseCustomId(interaction.customId);

switch (context) {
case 'store':
await handleCyberwareInteraction(interaction, action, currentPage);
break;
case 'unverified':
await handleUnverifiedPagination(interaction, action, currentPage);
break;
case 'profile':
await handleProfileButtonInteraction(interaction, action, userId);
break;
default:
console.error(`Unknown context: ${context}`);
return await interaction.reply({ content: 'An error occurred while processing the button interaction.', ephemeral: true });
}
}
} catch (error) {
console.error(`Error handling interaction: ${error.message}`);
if (!interaction.replied && !interaction.deferred) {
await interaction.reply({ content: '⚠ There was an error while executing this command!', ephemeral: true });
} else if (interaction.deferred) {
await interaction.editReply({ content: '⚠ There was an error while executing this command!', ephemeral: true });
}
}
}
});
chewie
chewie4mo ago
yeah, makes sense it errors then it's a single part customId, it won't have a context
𝙚𝙡𝙮𝙣𝙖𝙧𝙞
ahh thank you so much that was it! I add a check for single part customids before it tries to parse it for a context, it works now, I appreciate it!
Want results from more Discord servers?
Add your server