Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

280
Visualizações
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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda