久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

保存 JSON 文件以便稍后從中獲取其他數(shù)據(jù)

Save JSON file to grab other data from it later(保存 JSON 文件以便稍后從中獲取其他數(shù)據(jù))
本文介紹了保存 JSON 文件以便稍后從中獲取其他數(shù)據(jù)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我希望能夠用新數(shù)據(jù)保存我的 json 文件,然后調(diào)用該數(shù)據(jù)以便我可以再次保存新數(shù)據(jù).現(xiàn)在它所做的就是,當(dāng)我調(diào)用 JSON 文件數(shù)據(jù)的任何部分時,我上次手動保存它時保持不變.(我確實編輯了一些代碼并更好地描述了我的問題)提前謝謝你!這是我的代碼,沒有錯誤日志:

I want to be able to save my json file with new data and then call upon that data so that I can save new data again. Right now all it is doing is it is, when I call upon any part of the JSON file's data, staying the same the last time I manually saved it. (I did edit some code and a better description of my problem) Thank you in advance! Here is my code there is no error log:

const Discord = require('discord.js');
const botconfig = require("./botconfig.json");
const fs = require("fs");
const bot = new Discord.Client();
    bot.on("message", async message => {
        let prefix = botconfig.prefix;
        let messageArray = message.content.split(" ");
        let cmd = messageArray[0];
        let args = messageArray.slice(1);
        console.log(message.member.id)
        var playerFile = require(`./playerData/${message.member.id}.json`);
        if (message.author.bot) return;
        if (message.channel.type === "dm") return;
        if (cmd.charAt(0) === prefix) {
            if(cmd === `${prefix}fc`){
                fs.exists(`./playerData/${message.member.id}.json`, function(exists) {
                    if(exists){
                    let ar = args[0];
                    let ninConsole = args[1];
                    let code = args[2];
                    if(ar === "add" || ar === "remove"){
                        if(code){
                            if(ar === "add"){
                                console.log("Add");
                                if(ninConsole === "switch"){
                                    console.log("Switch " + code); 
                                    let fileContent = `{"switch": "${code}","threeDS": "${playerFile.threeDS}"}`
                                    fs.writeFile(`./playerData/${message.member.id}.json`, fileContent, (err) => {
                                        if (err) {
                                            console.error(err);
                                            return;
                                        };
                                    });
                                }
                                if(ninConsole === "3ds"){
                                    let fileContent = `{"switch": "${playerFile.switch}","threeDS": "${code}"}`
                                    fs.writeFile(`./playerData/${message.member.id}.json`, fileContent, (err) => {
                                        if (err) {
                                            console.error(err);
                                            return;
                                        };
                                    });
                                }
                            }
                            if(ar === "remove"){
                                if(ninConsole === "switch"){
                                    let fileContent = `{"switch": "None","threeDS": "${playerFile.threeDS}"}`
                                    fs.writeFile(`./playerData/${message.member.id}.json`, fileContent, (err) => {
                                        if (err) {
                                            console.error(err);
                                            return;
                                        };
                                    });
                                }
                                if(ninConsole === "3ds"){
                                    let fileContent = `{"switch": "${playerFile.switch}","threeDS": "None"}`
                                    fs.writeFile(`./playerData/${message.member.id}.json`, fileContent, (err) => {
                                        if (err) {
                                            console.error(err);
                                            return;
                                        };
                                    });
                                }
                            }
                        }
                    }
                }else{
                    return;
                }
                });
            }

推薦答案

下面是使用 fs 將數(shù)據(jù)保存到 JSON 文件的示例:

Here is an example of saving data to a JSON file using fs:

JSON.parse(fs.readFileSync("./points.json", "utf8"));

這里有一個關(guān)于如何使用此代碼為不和諧機器人制作積分系統(tǒng)的示例:https://anidiotsguide_old.gitbooks.io/discord-js-bot-guide/content/coding-guides/storing-data-in-a-json-file.html

There is an example on how to use this code to make a points system for a discord bot here: https://anidiotsguide_old.gitbooks.io/discord-js-bot-guide/content/coding-guides/storing-data-in-a-json-file.html

下面是這個例子的代碼:

Here is the code for this example:

const Discord = require("discord.js");
const fs = require("fs");
const client = new Discord.Client();

let points = JSON.parse(fs.readFileSync("./points.json", "utf8"));
const prefix = "+";

client.on("message", message => {
  if (!message.content.startsWith(prefix)) return;
  if (message.author.bot) return;

  if (!points[message.author.id]) points[message.author.id] = {
    points: 0,
    level: 0
  };
  let userData = points[message.author.id];
  userData.points++;

  let curLevel = Math.floor(0.1 * Math.sqrt(userData.points));
  if (curLevel > userData.level) {
    // Level up!
    userData.level = curLevel;
    message.reply(`You"ve leveled up to level **${curLevel}**! Ain"t that dandy?`);
  }

  if (message.content.startsWith(prefix + "level")) {
    message.reply(`You are currently level ${userData.level}, with ${userData.points} points.`);
  }
  fs.writeFile("./points.json", JSON.stringify(points), (err) => {
    if (err) console.error(err)
  });

});

client.login("SuperSecretBotTokenHere");

我希望這會有所幫助!

這篇關(guān)于保存 JSON 文件以便稍后從中獲取其他數(shù)據(jù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發(fā)出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復(fù)必須使用導(dǎo)入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務(wù)器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復(fù)“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務(wù)器時的歡迎消息)
主站蜘蛛池模板: 亚洲精品影院 | 日韩一区二区在线视频 | 欧美黑人一区二区三区 | 一区二区国产精品 | 日韩在线小视频 | 粉嫩av久久一区二区三区 | 欧美一区日韩一区 | 久久国产激情视频 | 国产高清精品在线 | 亚洲免费视频网站 | 久久久久国产精品午夜一区 | 国产在线精品一区二区三区 | 成人精品国产免费网站 | 男女羞羞视频大全 | 久久成人人人人精品欧 | 91精品国产91久久久久久 | 久久夜色精品国产 | 一区二区在线 | 中文字幕亚洲视频 | 尹人av | 日韩中文字幕视频在线 | 国产精品一区二区三区在线 | 欧美三级三级三级爽爽爽 | 香蕉超碰 | 人人看人人草 | 久久夜视频 | 成人欧美一区二区三区1314 | 亚洲高清在线观看 | 放个毛片看看 | 亚洲播放一区 | 91传媒在线观看 | 一区二区精品 | 免费观看日韩精品 | 亚洲一区二区三区视频在线 | 91国语清晰打电话对白 | 国产一区二区精品在线观看 | 国产精品久久久久久久久久久久冷 | 激情五月婷婷 | 夜夜骚视频 | 岛国av免费在线观看 | 操久久 |