Search code examples
javascriptnode.jsdiscord.js

How do I make a string option automatically selected in Discord.js?


I've been working on a Discord bot and been reading up on the docs. I'm quite new to this and I can't find any YT tutorials on the topic, here's the code:

const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('test')
        .setDescription('Select a test to use.')
        .addStringOption(option =>
            option
                .setName('type')
                .setDescription('Type of test requested')),

        async execute(interaction) {
            const ptypes = interaction.options.getString('type') ?? 'No reason provided';
            await interaction.reply(`You chose ${ptypes}`);
        },
};
When I enter the command, instead of automatically showing a box for me to type in, it makes me select the option called 'test' before I type anything.

Here's how that would work:
/test > [click]test > [input]test

Here's how I want it to work:
/test > [input]test


Solution

  • You can accomplish this, by supplying .setRequired()

    .addStringOption(option =>
                option
                    .setName('type')
                    .setDescription('Type of test requested'))
                    .setRequired(true),