fs.writeFile 不写入文件,除非 运行 两次
fs.writeFile not writing to file unless run twice
我正在努力学习 Node.js 所以我一直在尝试编写一个简单的 Discord 机器人。它 运行 很好,除非我尝试写入配置文件。它仅在我 运行 命令两次或另一个命令紧随其后 运行 时才写入文件。我似乎无法弄清楚为什么会那样做。它成功地在每个命令后在 Discord 中发布消息,但它只是没有更新的文件。它也不向控制台输出错误。
我有下面的代码和 config.json 示例。任何帮助将不胜感激。
config.json:
{
"ownerID": "1234567890",
"token": "insert-bot-token-here",
"prefix": "!",
"defaultStatus": "status-here"
}
const Discord = require("discord.js");
const client = new Discord.Client();
const fs = require("fs")
const config = require("./config.json");
client.on("ready", () => {
console.log("I am ready!");
client.user.setActivity(config.defaultStatus, {
type: 'WATCHING'
});
});
client.on("message", (message) => {
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (!message.content.startsWith(config.prefix) || message.author.bot) return;
if (command === "ping") {
message.channel.send("pong!");
} else
if (message.author.id !== config.ownerID) return message.reply("you must be the bot owner to run this command.");
let configData = JSON.stringify(config, null, 2);
// Command to change the prefix for commands
if (command === "prefix") {
let newPrefix = message.content.split(" ").slice(1, 2)[0];
config.prefix = newPrefix;
fs.writeFile("./config.json", configData, (err) => console.error);
message.channel.send("Prefix has been updated to \"" + newPrefix + "\"");
}
// Command to change the bot status
if (command === "status") {
let game = args.slice(0).join(" ");
client.user.setActivity(game, {
type: 'WATCHING'
});
message.channel.send("Status has been updated to \"" + game + "\"");
}
// Command to change the default bot status
if (command === "defaultstatus") {
let newStatus = args.slice(0).join(" ");
config.defaultStatus = newStatus;
client.user.setActivity(newStatus, {
type: 'WATCHING'
});
fs.writeFile("./config.json", configData, (err) => console.error);
message.channel.send("Default status has been updated to \"" + newStatus + "\"");
}
});
client.login(config.token);
这仅适用于第二次,因为您在更改 config
对象之前声明了 configData
。
configData = JSON.stringify(config, null, 2); //saves the config as a string
// you do all your stuff, including changing the prefix, for example
config.prefix = '-'; //this is stored in config, but not in configData
// then you write the file with the old data
fs.writeFile("./config.json", configData, (err) => console.error);
这导致每次更改都会延迟。
您应该在 更改 config
:
之后创建 configData
...
config.prefix = '-'; // inside the command
...
let configData = JSON.stringify(config, null, 2);
fs.writeFile('./config.json', configData, err => console.error);
我正在努力学习 Node.js 所以我一直在尝试编写一个简单的 Discord 机器人。它 运行 很好,除非我尝试写入配置文件。它仅在我 运行 命令两次或另一个命令紧随其后 运行 时才写入文件。我似乎无法弄清楚为什么会那样做。它成功地在每个命令后在 Discord 中发布消息,但它只是没有更新的文件。它也不向控制台输出错误。
我有下面的代码和 config.json 示例。任何帮助将不胜感激。
config.json:
{
"ownerID": "1234567890",
"token": "insert-bot-token-here",
"prefix": "!",
"defaultStatus": "status-here"
}
const Discord = require("discord.js");
const client = new Discord.Client();
const fs = require("fs")
const config = require("./config.json");
client.on("ready", () => {
console.log("I am ready!");
client.user.setActivity(config.defaultStatus, {
type: 'WATCHING'
});
});
client.on("message", (message) => {
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (!message.content.startsWith(config.prefix) || message.author.bot) return;
if (command === "ping") {
message.channel.send("pong!");
} else
if (message.author.id !== config.ownerID) return message.reply("you must be the bot owner to run this command.");
let configData = JSON.stringify(config, null, 2);
// Command to change the prefix for commands
if (command === "prefix") {
let newPrefix = message.content.split(" ").slice(1, 2)[0];
config.prefix = newPrefix;
fs.writeFile("./config.json", configData, (err) => console.error);
message.channel.send("Prefix has been updated to \"" + newPrefix + "\"");
}
// Command to change the bot status
if (command === "status") {
let game = args.slice(0).join(" ");
client.user.setActivity(game, {
type: 'WATCHING'
});
message.channel.send("Status has been updated to \"" + game + "\"");
}
// Command to change the default bot status
if (command === "defaultstatus") {
let newStatus = args.slice(0).join(" ");
config.defaultStatus = newStatus;
client.user.setActivity(newStatus, {
type: 'WATCHING'
});
fs.writeFile("./config.json", configData, (err) => console.error);
message.channel.send("Default status has been updated to \"" + newStatus + "\"");
}
});
client.login(config.token);
这仅适用于第二次,因为您在更改 config
对象之前声明了 configData
。
configData = JSON.stringify(config, null, 2); //saves the config as a string
// you do all your stuff, including changing the prefix, for example
config.prefix = '-'; //this is stored in config, but not in configData
// then you write the file with the old data
fs.writeFile("./config.json", configData, (err) => console.error);
这导致每次更改都会延迟。
您应该在 更改 config
:
configData
...
config.prefix = '-'; // inside the command
...
let configData = JSON.stringify(config, null, 2);
fs.writeFile('./config.json', configData, err => console.error);