#Saving user input to as JSON object

6 messages · Page 1 of 1 (latest)

untold hawk
#

Hello! I have a command that will ask the user for 2 inputs, how can I now grab this 2 inputs the user has typed and add it to an array as an object inside of a JSON file?
I mean, every time the command is run I want it to create a new object following this format: const object{name:"input1", artist:"input2"} and then save it to an array that I have inside of a JSON file.
This is my command and then an example of what the json should look like:


module.exports={

data: new SlashCommandBuilder()
    .setName('test')
    .setDescription('Testing options')
    .addStringOption(option =>
        option.setName('input')
            .setDescription('The input to test')
            .setRequired(true))
        .addStringOption(option =>
                option.setName('input2')
                    .setDescription('The second input to test')
                    .setRequired(true)),

            async execute(interaction){
                let input1 = interaction.options.getString('input');
                let input2 = interaction.options.getString('input2');
                await interaction.reply(input1 + ` - ` + input2)},

 };

//THIS IS A DIFFERENT FILE, A .JSON FILE. THE "DIE FOR YOU" AND "FEAR" AND INPUT1 AND THE "THE WEEKND" AND "KENDRICK LAMAR" ARE INPUT2.
[
    {
        "name":"Die for You",
        "artist":"The Weeknd"
    },
    {
        "name":"FEAR",
        "artist":"Kendrick Lamar"
    }
]

jolly blaze
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

quick geyser
#

well, you already have the inputs as variables, so you can create an object containing the inputs like so:

const data = { name: input1, artist: input2 }

And then use node-fs to push this object to your json file.

and btw this is not djs related, please check #resources and #useful-servers

#

( @untold hawk ^ )

untold hawk
#

thank you a lot! I will take a look

shell trellis
#

Saving user input to s JSON object