buttons not working

Hi! I have a code that should give a sample math test - it gives you a linear equation and two answer choices (one of them is correct) The code is pasted below: const { ActionRowBuilder, ButtonBuilder, ButtonStyle, SlashCommandBuilder } = require('discord.js'); module.exports = { data: new SlashCommandBuilder() .setName('mathtest') .setDescription('Gives you a math test'), async execute(interaction) { var a = Math.floor(Math.random()10)+1; var b = Math.floor(Math.random()10)+1; var c = Math.floor(Math.random()(20-(a+b)))+a+b; var d = Math.floor(Math.random())+1; var aone; var atwo; var sol; if(d === 1){ aone = (c - b)/a; atwo = Math.floor(Math.random()10)+1; sol = aone; } if(d === 2){ aone = Math.floor(Math.random()*10)+1; atwo = (c-b)/a; sol = atwo; } const answerone = new ButtonBuilder() .setCustomId('answerone') .setLabel(${aone}) .setStyle(ButtonStyle.Primary); const answertwo = new ButtonBuilder() .setCustomId('answertwo') .setLabel(${atwo}) .setStyle(ButtonStyle.Primary); const row = new ActionRowBuilder() .addComponents(answerone, answertwo);
const response = await interaction.reply({ content: Solve the math problem, ${a}x + ${b} = ${c}, components: [row], });
const collectorFilter = i => i.user.id === interaction.user.id; try { const answer = await response.awaitMessageComponent({ filter: collectorFilter, time: 60_000 });
if (answer.Label === ${sol}) { await answer.update({ content: Sorry, incorrect, components: [] }); } else if (answer.Label !== ${sol}) { await answer.update({ content: 'You got it correct!', components: [] }); } } catch (e) { await response.editReply({ content: 'Nobody answered!', components: [] }); }
}, };
56 Replies
d.js toolkit
d.js toolkit2y 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.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
oh ok - i meant the label property but i think i can change it's custom id to the respective answer too
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
hm - it's still not working - here's the code: const { ActionRowBuilder, ButtonBuilder, ButtonStyle, SlashCommandBuilder } = require('discord.js'); module.exports = { data: new SlashCommandBuilder() .setName('mathtest') .setDescription('Gives you a math test'), async execute(interaction) { var a = Math.floor(Math.random()10)+1; var b = Math.floor(Math.random()10)+1; var c = Math.floor(Math.random()(20-(a+b)))+a+b; var d = Math.floor(Math.random())+1; var aone; var atwo; var sol; if(d === 1){ aone = (c - b)/a; atwo = Math.floor(Math.random()10)+1; sol = aone; } if(d === 2){ aone = Math.floor(Math.random()*10)+1; atwo = (c-b)/a; sol = atwo; } const answerone = new ButtonBuilder() .setCustomId(${aone}) .setLabel(${aone}) .setStyle(ButtonStyle.Primary); const answertwo = new ButtonBuilder() .setCustomId(${atwo}) .setLabel(${atwo}) .setStyle(ButtonStyle.Primary); const row = new ActionRowBuilder() .addComponents(${aone}, ${atwo});
const response = await interaction.reply({ content: Solve the math problem, ${a}x + ${b} = ${c}, components: [row], });
const collectorFilter = i => i.user.id === interaction.user.id; try { const answer = await response.awaitMessageComponent({ filter: collectorFilter, time: 60_000 });
if (answer.customId === ${sol}) { await answer.update({ content: Sorry, incorrect, components: [] }); } else if (answer.customId !== ${sol}) { await answer.update({ content: 'You got it correct!', components: [] }); } } catch (e) { await response.editReply({ content: 'Nobody answered!', components: [] }); }
}, };
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
it's currently saying "application did not respond"
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
i think its bc the code just isn't working
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
alright it's executing but it says that it's not working how do i do it without passing template literals?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
because i want to have two answer buttons and, generate two numbers, one being the answer (sol) and the other being a random number - then, by random (purpose of d variable), choose which button gets correct ans and which gets wrong ans
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs2y ago
If you aren't getting any errors, try to place console.log checkpoints throughout your code to find out where execution stops. • Once you do, log relevant values and if-conditions • More sophisticated debugging methods are breakpoints and runtime inspections: learn more
CalcWarrior
CalcWarriorOP2y ago
right now it's saying "application did not respond" but idk why because all my other commands execute fine
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
yea i'll comment out parts of my code to see it was working a while ago tho
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
im pretty sure there are errors but right now it's not executing so its prob taking too long
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
yea i played with the template literals it doesnt say anything there tho
CalcWarrior
CalcWarriorOP2y ago
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
i think everything above that in the console is from previous stuff
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
CalcWarrior
CalcWarriorOP2y ago
i never use .toJSON tho so idk what that's about
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
yea but now my ids are ${aone} and ${atwo}?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
oh but i never define answerone or answertwo anywhere
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
ohh so those things should put the button names, or the button ids
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
oh ok
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
oh ok yea now it's responding my current code is still in the debug console.log mode i'll see if the actual code works
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
regarding the template literals is there any other ones that should be removed? its kinda messy to put those template literals as customIds
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
hm but the sol thing isn't working sol should be equal to whichever is (c - b)/a and then i have the if(answer.customId === sol) or else if (answer.customID !== sol)
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
yea so either aone or atwo is (c - b)/a and whichever is would be equal to sol so if(answer.customId === sol) should be true only if answer.customId = (c-b)/a
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
well its kinda confusing with the template literals but yea aone and atwo should also be float numbers and then sol would be a float number but then idk if custom Ids can be numbers
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
oh ok yea idk but should i still do the ${} thing?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
but it's not currently working
CalcWarrior
CalcWarriorOP2y ago
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
ok
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CalcWarrior
CalcWarriorOP2y ago
ok i think its working now thanks
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server