javascript Discord Bot,显示在“.addChoice”上选择问题后的值

vzgqcmou  于 2022-11-27  发布在  Java
关注(0)|答案(1)|浏览(90)

当我选择问题时,未显示答案值。我希望显示问题和答案。
选择问题: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}' })

    }
}

输出:

your question: ${name}

Screenshot output

我的预期输出:

your question: How old are you?

answer: 21
bvn4nwqk

bvn4nwqk1#

如果希望${name}和${value}解析为变量的值,则必须使用模板文字。https://www.w3schools.com/JS//js_string_templates.asp
所以你要做的就是把'改为倒勾'
您拥有的:

await interaction.reply({ content: 'your question: ${name}' });
    await interaction. Reply({ content: 'answer: ${value}' })

应该是:

await interaction.reply({ content: `your question: ${name}` });
    await interaction. Reply({ content: `answer: ${value}` })

更新如下
我找不到一种方法来获取您选择的选项的文本。您可以将对象存储在一个单独的变量中,然后在执行函数中从给定的答案中搜索问题的。但这是您获取所选选项的值的方法。所有这些都在文档www.example.com中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}` });
  },
};

相关问题