Search code examples
javascriptnode.jsjsondiscord.js

Get the value that was passed through discord.js option


const data = new SlashCommandBuilder() // </> command
  .setName('info')
  .setDescription('информация об админе')
  .addStringOption(option =>
    option.setName('steam64')
      .setDescription('numbers')
      .setRequired(true));
//code 
client.on('interactionCreate', async interaction => {
  if (interaction.commandName === 'убитьбота') {
    interaction.reply("Успешно!");
    client.destroy();
  }
  else if (interaction.commandName === 'info'){ // Problem
    console.log("here");
    id = interaction.options.string; // over here //
    console.log(id) // undefined
    console.log(admins[id]) // undefined
    const embed = new MessageEmbed()
      .setColor('#77dd77')
      .setTitle('Информация')
      .setDescription(`Имя: ${admins[id].NAME} \n **Описание:** ${admins[id].DIS} \n **Звание:** ${admins[id].ADMINLVL} \n **Наказание** - ***Выговоров:*** ${admins[id].PUNS.VARN} | ***Предов:*** ${admins[id].PUNS.PRED}`); // error
    await interaction.reply({ content: 'Вот!', ephemeral: true, embeds: [embed]});
    console.log(interaction.option);
    interaction.reply(`debug - {admins} в conlose)`);
  }    
});

How can I get the variable that was passed through option?
I've already gone through most of the methods, but I haven't found a method to get a variable.

I use Discord.js v13.12.0.

JSON:

{
    "76561198887558606": {
        "NAME": "Пельмень",
        "DIS": "Вы хотите знать что-то обо мне? Хи-Хи [NO DATA] ooops!",
        "ADMINLVL": "GOD XD",
        "PUNS":
        {
            "VARN": 0,
            "PRED": 0
        }
    },
    "76561199037779891":{
        "NAME": "Senko Number ₲ne",
        "DIS": "[NO DATA] ooops!",
        "ADMINLVL": "Технарь",
        "PUNS":
        {
            "VARN": 0,
            "PRED": 0
        }
    }
}

Solution

  • To get the user input for the command option set by .addStringOption(option => option.setName('steam64')), you can use interaction.options.getString('steam64').

    else if (interaction.commandName === 'info') {
      const id = interaction.options.string('steam64');
      console.log(id) // the value provided by user
    

    You can check out the documentation of the CommandInteractionOptionResolver where you'll find all the methods you may need:

    // .addStringOption((option) =>
    //   option.setName('string').setDescription('Enter a string'),
    // )
    const string = interaction.options.getString('string');
    
    // .addIntegerOption((option) =>
    //   option.setName('integer').setDescription('Enter an integer'),
    // )
    const integer = interaction.options.getInteger('integer');
    
    // .addNumberOption((option) =>
    //   option.setName('number').setDescription('Enter a number'),
    // )
    const number = interaction.options.getNumber('number');
    
    // .addBooleanOption((option) =>
    //   option.setName('boolean').setDescription('Select a boolean'),
    // )
    const boolean = interaction.options.getBoolean('boolean');
    
    // .addUserOption((option) =>
    //   option.setName('target').setDescription('Select a user'),
    // )
    const user = interaction.options.getUser('target');
    const member = interaction.options.getMember('target');
    
    // .addChannelOption((option) =>
    //   option.setName('channel').setDescription('Select a channel'),
    // )
    const channel = interaction.options.getChannel('channel');
    
    // .addRoleOption((option) =>
    //   option.setName('role').setDescription('Select a role'),
    // )
    const role = interaction.options.getRole('role');
    
    // .addAttachmentOption((option) =>
    //   option.setName('attachment').setDescription('Attach something'),
    // );
    const attachment = interaction.options.getAttachment('attachment');
    
    // .addMentionableOption((option) =>
    //   option.setName('mentionable').setDescription('Mention something'),
    // )
    const mentionable = interaction.options.getMentionable('mentionable');