【问题标题】:Is there a way to create a JSON file using discord.js v12?有没有办法使用 discord.js v12 创建 JSON 文件?
【发布时间】:2020-12-25 06:35:48
【问题描述】:

我正在制作一个不和谐的机器人,我想知道你是否可以用它创建一个 JSON 文件。我还没有找到办法。我的命令对 &createfile {name} 做出反应,它总是会创建一个 JSON 文件。到目前为止我的代码:

const Discord = require('discord.js');

module.exports = {
    name: 'createfile',
    description: "The bot will create a .json file named after the argument",
    execute(message, args){
        var fileName = args[0]
        if(!args) {
            return message.reply('invalid name')
        };
        // Create file {fileName}.json
    }
}

有没有办法创建这样的文件?谢谢:)

【问题讨论】:

  • 你可能想看看 node.js 的fs 模块,尤其是writeFileSync 函数。谷歌一下,你很快就明白了

标签: javascript json discord.js


【解决方案1】:

您可以使用 fs 创建 JSON 文件

const Discord = require('discord.js');
const fs = require('fs');

module.exports = {
    name: 'createfile',
    description: "The bot will create a .json file named after the argument",
    execute(message, args){
        var fileName = args[0]
        if(!args) {
            return message.reply('invalid name')
        };
        
        let jsonFile = { 
          fileName: fileName
        };
 
        let data = JSON.stringify(jsonFile);
        fs.writeFileSync(`${fileName}.json`, data);
    }
}

【讨论】:

    猜你喜欢
    • 2020-12-05
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多