Home > Software design >  How to get update data from json file in discord.js
How to get update data from json file in discord.js

Time:01-21

I have a help command and when I use this, According to the language entered in the data of the json file, the bot responds to that language, but when we change the language through the json file, it still responds with the previous language.

My json file: {"serverid": "fa"} serverid for example 70436705474 :)

const lang = require('../data/lang.json')


module.exports = {
    name: "help",
    description: "For see help",
    execute(client, message){

        let server = message.guild.id // server id for example 70436705474

        if (lang[server] == "fa"){
            let embed1 = new Discord.MessageEmbed()
            .setAuthor("سلام")
            .setDescription(`چطور میتونم کمکتون کنم؟`)
        message.channel.send(embed1)
        } else if (lang[server] == "en") {
            let embed2 = new Discord.MessageEmbed()
            .setAuthor("Hi")
            .setDescription(`How can I help you?`)
            message.channel.send(embed2)
        }
    }
}

response is embed 1

when I edit json file to => {"serverid": "en"} again I get response embed1 but command not update

I need when I edit the language fa to en in json file I get "embed2"

CodePudding user response:

Welcome to Stack Overflow!

Make sure your json file doesn't actually say "serverid", and contains the server's ID.

If it has the server ID, but you're changing the json file as the bot is running, you'll want to load the json file through the fs module, or delete the json file from require cache (delete require.cache[require.resolve('../data/lang.json')])

The issue is coming from the fact that the JSON file is being read from memory due to the way it's being imported, so changes to its physical copy won't affect the memory copy unless it's reloaded.

  • Related