Home > Enterprise >  Receiving discord.js slash command parameters
Receiving discord.js slash command parameters

Time:08-07

I'm currently trying to make a calculation using parameters input via a slash command. Putting them in works fine, however I do not know how to actually retrieve them. the current code yields an error TypeError: Cannot read properties of undefined (reading 'get') which has me quite confused, as the discord.js documentation shows that said function should exist and be exactly what I'm looking for.

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

module.exports = {
    data: new SlashCommandBuilder()
        .setName('manacost')
        .setDescription('Calculates the ´soul spawn cost using your inputs')
        .addStringOption(option =>
            option.setName('health')
                .setDescription('The amount of health your soul has. Supports k=1000...')
                .setRequired(true))
        .addStringOption(option =>
            option.setName('damage')
                .setDescription('The amount of damage your soul has. Supports k=1000...')
                .setRequired(true))
        .addIntegerOption(option =>
            option.setName('breeze')
                .setDescription('The amount of breeze you have.(0-80)')
                .setRequired(false)),
    async execute(interaction) {
        if (!interaction.isChatInputCommand()) return;
        let hp = interaction.options.get('health');
        let dmg = interaction.option.get('damage');
        let b =  interaction.option.get('breeze');
        console.log({hp, dmg, b});
        await interaction.reply({content: hp, dmg, b});
    },
}

CodePudding user response:

The interaction options can be found using interaction.option.getString() or interaction.option.getInteger(). Also in your Code you have mixed up interaction.options and interaction.option. You should only use interaction.options.

That means your code has to change from

let hp = interaction.options.get('health');
let dmg = interaction.option.get('damage');
let b =  interaction.option.get('breeze');

to

let hp = interaction.options.getString('health');
let dmg = interaction.options.getString('damage');
let b =  interaction.options.getInteger('breeze');

You should take a look at this post from the discord.js guide for more information and a look at the other option types.

  • Related