【问题标题】:discord.js write and read JSON filediscord.js 写入和读取 JSON 文件
【发布时间】:2021-04-22 15:04:48
【问题描述】:

我有一个问题。如何让我的 DiscordBot 写入和读取 JSON 文件?我已经编程了一些东西,但只有错误。有人可以帮我吗?我将不胜感激。

const Discord = require("discord.js")
const botconfig = require("../botconfig.json");
const colours = require("../colours.json");
const superagent = require("superagent");
const { report } = require("superagent");
const usedCommand = new Set();

module.exports.run = async(bot, message, args) => {
    message.delete();

    if (usedCommand.has(message.author.id)) {
        message.reply("du kannst diesen Command erst in 15 Sekunden wieder verwenden.").then(msg => msg.delete({ timeout: "10000" }));
    } else {
        let member = message.mentions.members.first() || message.guild.members.cache.get(args[0])

        let reason = args.slice(1).join(" ");
        if (!reason) message.reply("Bitte gebe einen Beweis in Form von einem Link (Video: Youtube, Bild: prnt.sc) an!")

        var fs = require("fs");
        var sampleObject = {
            username: member,
            proof: reason
        };

        fs.writeFile('../rpp.json', JSON.stringify(sampleObject, null, 4), (err) => {
            if (err) {
                console.error(err);
                return;
            };
            console.log("File has been created");
        });

        usedCommand.add(message.author.id);
        setTimeout(() => {
            usedCommand.delete(message.author.id);
        }, 15000);
    }
}

module.exports.config = {
    name: "rpp",
    description: "Bans a user from the guild!",
    usage: "+ban",
    accessableby: "Members",
    aliases: ["proofwrite"]
}

我希望我的 Discord 机器人使用“writerrp”命令将标记的用户和原因输入 JSON 文件。之后,我希望机器人使用“readrrp”命令读取用户的数据。

【问题讨论】:

标签: javascript node.js json discord.js


【解决方案1】:

这是从.json 文件读取的示例。

使用正确的语法正确设置 json 文件。

{
  "token": "<a token goes here>" //in your local json file - called config.json in this case
}
client.login(config.token) //reads from the config.json file as an obj, which calls the token param

读取json文件时,需要fs

const fs = require('fs');
//assuming your bot has args
let reason = args.slice(1,2) //removes the command and the @
let user = message.mentions.users.first()

//create a new obj
var obj = {
   table: []
};

//add some data to it e.g.
obj.table.push({reason: `${reason}`, user:`${user.username}`});

//convert it to json using the json#stringify() method
var json = JSON.stringify(obj);

//write to the json file
fs.writeFile('myjsonfile.json', json, 'utf8', callback);

//if you want to append, do this:
fs.readFile('myjsonfile.json', 'utf8', function readFileCallback(err, data){
    if (err){
        console.log(err);
    } else {
    obj = JSON.parse(data); //now it an object
    obj.table.push({id: 2, square:3}); //add some data
    json = JSON.stringify(obj); //convert it back to json
    fs.writeFile('myjsonfile.json', json, 'utf8', callback); // write it back 
}});

感谢@kailniris here

【讨论】:

  • 谢谢。但我得到一个错误:prnt.sc/x4bt1g
  • @Beyazz 我已经附加了一段代码,现在试试
  • @Beyazz 使用新代码,并确保将callback 放回
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-23
  • 2020-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-16
  • 1970-01-01
相关资源
最近更新 更多