When I choose question, the answer value is not showing. i want to show the question and the answer.
Choose question: https://ibb.co/6sYLc1P
const { SlashCommandBuilder } = require("@discordjs/builders")
module.exports = {
data: new SlashCommandBuilder()
.setName("question")
.setDescription("what do you want to ask me?")
.addStringOption(option =>
option.setName('question')
.setDescription('Your question')
.setRequired(true)
.addChoices(
{ name: 'How old are you', value: '21' },
{ name: 'When is your birthday?', value: 'September 8, 2003' },
{ name: 'What is your favorite color?', value: 'Violet' },
)),
execute: async ({ interaction }) => {
await interaction.reply({ content: 'your question: ${name}' });
await interaction.reply({ content: 'answer: ${value}' })
}
}
Output:
your question: ${name}
My Expected output:
your question: How old are you?
answer: 21
CodePudding user response:
You have to use Template Literals if you want ${name} and ${value} to resolve into the value of the variables. https://www.w3schools.com/JS//js_string_templates.asp
So what you have to do is change ' to backtick `
What you have:
await interaction.reply({ content: 'your question: ${name}' });
await interaction. Reply({ content: 'answer: ${value}' })
What it should be:
await interaction.reply({ content: `your question: ${name}` });
await interaction. Reply({ content: `answer: ${value}` })
Update below
I could not find a way to get the text of the choice you select. You can probably store the objects in a separate variable and then search for the for the question from the given answer in the execute function. But this is how you get the value of the selected choice. It's all in the documentation https://discordjs.guide/slash-commands/parsing-options.html#choices
const { SlashCommandBuilder } = require("@discordjs/builders");
module.exports = {
data: new SlashCommandBuilder()
.setName("question")
.setDescription("what do you want to ask me?")
.addStringOption((option) =>
option
.setName("question")
.setDescription("Your question")
.setRequired(true)
.addChoices(
{ name: "How old are you", value: "21" },
{ name: "When is your birthday?", value: "September 8, 2003" },
{ name: "What is your favorite color?", value: "Violet" }
)
),
execute: async (interaction) => {
const answer = interaction.options.getString("question"); // Gets the value from the selected choice
await interaction.reply({ content: `answer: ${answer}` });
},
};