久久久久久久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精品一区二区三区久久久久 | 色爱区综合 | 国产亚洲精品精品国产亚洲综合 | 精品视频一区在线 | 国产一区二区在线视频 | 久久久在线视频 | 黄色网址在线免费播放 | a黄在线观看 | 2一3sex性hd | 激情一区二区三区 | 久久久久亚洲视频 | 91免费观看国产 | 中文字幕动漫成人 | 91在线 | 日韩精品一区二区久久 | 在线视频国产一区 | 精品国产成人 | 国产精品一二三区 | 男女羞羞网站 | 欧美精品久久久久 | 久草福利 | 国产激情在线播放 | 精品一区二区视频 | 久久久www成人免费精品 | 黄网站免费在线 | 日韩一区三区 | av中文字幕在线观看 | 99reav | 国产色黄 | 国产美女自拍视频 | 日韩欧美国产一区二区三区 | 韩国理论电影在线 | 国产在线高清 | 中文字幕av网站 | 成人精品一区亚洲午夜久久久 | 操操日| 中文字幕在线看 | 99精品视频免费观看 |