javascript - Is there a way to create a JSON file using discord.js v12?
问题描述
I'm making a discord bot and I was wondering if you can create a JSON file with it. I haven't found a way to do it. My command reacts to &createfile {name} and it will always create a JSON file. My code so far:
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
}
}
Is there a way to create a file like that? Thanks :)
解决方案
You can create JSON files using fs
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);
}
}
推荐阅读
- c++ - 条件循环:变量与函数?
- android - 将数据传输到 BLE 设备时在 Android 中显示进度
- python - 针对现有数据集删除一个新行
- php - 将数据传递给模态代码点火器框架
- android - 如何使用 espresso 来测试矢量绘图
- python - 我的打印语句只会打印 10 的倍数,而不是介于两者之间
- vba - 如何将彩色文本更改为冒号,然后将冒号后的文本更改为不同的颜色
- php - 通过 Codeigniter Ajax 在视图中显示数据时未定义的变量
- python - 如何为烧瓶应用程序构建更新程序?
- javascript - 为什么 Firebase Auth 在登录时会重新加载页面?