Parse Arguments from Discord interactions

I am currently coding a discord bot and wanted to play around with the new interaction feature (aka slash-commands).

I managed to get a “/test” Interaction working, and the bot receives the command and executes my code for it (following discord.js manual). Now I know from interaction.commandName what name for example the command has (in this case “test”), but I don’t seem to be able to get the arguments that are passed in.

Following is my “test.js”, which is for the “/test” command:

const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('test')
        .setDescription('Test with arguments!')
    .addStringOption(option =>
      option.setName("device")
        .setDescription("Some argument")
        .setRequired(true)
        .addChoice("Arg A", "A")
        .addChoice("Arg B", "B")
    ),
    async execute(interaction, log) {
        // log is a function to log to a file...
        // Ignore it, since it is not *currently* in use.
 
        return interaction.reply("You selected the argument ????")
    
        // Delete message after 5 seconds
        .then(setTimeout(() => interaction.deleteReply(), 5000));
    },
};

The command itself gets called by a function that is also from the documentation of discord.js:

client.on('interactionCreate', async(interaction) => {
  if (!interaction.isCommand()) return;

    const command = client.commands.get(interaction.commandName);

    if (!command) return;

    try {
        await command.execute(interaction, log);
    } catch (error) {
        console.error(error);
        await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
    }
});

I looked through the documentation and searched quite a bit online, but I don’t seem to find anything. If anyone could help me that would be great.

Answer

Warning: I did not test this code CommandInteractions have an options property. You can use the get method on it.

//the option name is 'TEST' and the user inputted 'hey'
let option = interaction.options.get("TEST")
console.log(option.value)

.value shows the input as shown here. It can be empty if there is no input (option was not required and user did not input that option)