Home > Blockchain >  How can i make this discord.js v14 slash command not return "undefined" for each option?
How can i make this discord.js v14 slash command not return "undefined" for each option?

Time:01-08

The idea of this command is to add a user to the .txt file full of customers info so it can be used by other parts of the bot.

The problem is no matter what i put into the options that pop up here

Command Example

it will always return "undefined" for each option like so.

Example Reply

i think the issue is its not actually returning anything from the interaction when asking for that information inside the execute function. im not quite sure why though.

here is the code in question.

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

module.exports = {
  data: new SlashCommandBuilder()
    // command name
    .setName('addfrozencustomer')
    // command description
    .setDescription('Add a frozen customer account/discord')
    .addStringOption(option =>
        option.setName('id')
            .setDescription('The ID of the customer')
            .setRequired(true))
    .addStringOption(option =>
        option.setName('username')
            .setDescription('The username of the customer')
            .setRequired(true))
    .addStringOption(option =>
        option.setName('email')
            .setDescription('The email of the customer')
            .setRequired(true)),
    async execute(interaction) {
        const { id, username, email } = interaction.options;
        const data = `${id}:::${username}:::${email}`;
        
        fs.appendFile('users/frozencustomers.txt', `${data}\n`, (error) => {
            if (error) {
            console.error(error);
            return;
            }
        
            console.log('The data was successfully written to the file.');
        });
        interaction.channel.send(`Successfully added frozen customer:\n ID: ${id}\n User: ${username}\n Email: ${email}`);
        }
              
};

Out of everything i have tried the code above is the closest iv come to getting this to work as intended.

CodePudding user response:

You can't grab interaction options values directly from the interaction.options object, it has seperate methods to grab each type of value, in your case:

const
  username = interaction.options.getString("username"),
  id = interaction.options.getString("id"),
  email = interaction.options.getString("email")

You can read more about this here: https://discordjs.guide/slash-commands/parsing-options.html#command-options

  • Related