Search code examples
javascriptdiscorddiscord.js

Store SlashCommandBuilder options in another js file


I have an option in a SlashCommandBuilder that goes like this:

.addStringOption((option) => option
    .setName('city')
    .setDescription('In which city are you currently based from?')
    .addChoices(
        { 
           name: 'City 1', 
           value: 'City-1-Example' 
        }, 
        { 
           name: 'City 2', 
           value: 'City-2-Example' 
        }
    )
    .setRequired(true)
)

I was thinking of creating another separate .js file, let's say options.js that contains:

export const options = {
    city: [
        { 
           name: 'City 1', 
           value: 'City-1-Example' 
        }, 
        { 
           name: 'City 2', 
           value: 'City-2-Example' 
        }
    ],

    optionsForAnotherCommand: [
        ...
    ]
}

so that I could easily edit options in one file for all SlashCommandBuilder options. Makes it organized and clean, I can go ahead and do this now:

.addStringOption((option) => option
    .setName('city')
    .setDescription('In which city are you currently based from?')
    .addChoices(options.city)
    .setRequired(true)
)

However, it returns to me an error:

errors: [[ 0, ValidationError: Expected the value to not be an array ...

The code works with the first example, but it's going to be a hassle if I want to change options for all of my commands since I have to open them file by file. Was wondering if this is possible or if it's not, is there a more organized way to store your options in a SlashCommandBuilder?


Solution

  • Thank you for the answer @Zsolt Meszaros.

    The answer is simple, you just add ... before it:

    .addChoices(...options.city)