因此,当我警告用户时,它可以正常工作,但是当我再次尝试警告某人时,它会替换旧字符串(原因),但我想让它添加并保留旧字符串。 (原因)我尝试了很多东西,但 fs 仍然自动替换旧字符串(原因)。
还有我的 javascript 代码:
const Discord = require("discord.js")
const fs = require("fs");
module.exports = {
name: "warn",
description: "Warn command",
async execute( message){
let config = require("../config.json")
let lang = JSON.parse(fs.readFileSync("./langs.json", "utf8"));
if(!lang[message.guild.id]){
lang[message.guild.id] = {
lang: config.lang
};
}
let correct1 = lang[message.guild.id].lang;
const ping = require(`../lang/${correct1}/warn.json`)
const response = ping
const messageArray = message.content.split(' ');
const args = messageArray.slice(1);
let sEmbed1 = new Discord.MessageEmbed()
.setColor("#FF0000")
.setTitle("Permission refused.")
.setDescription("<:dont:803357503412502588> | You don't have the permission for this command. (`MANAGE_ROLES`)");
if(!message.member.hasPermission("MANAGE_ROLES")) return message.reply(sEmbed1)
const user = message.mentions.users.first()
if(!user) {
return message.channel.send("<:dont:803357503412502588> | You need to mention a user.")
}
if(message.mentions.users.first().bot) {
return message.channel.send("<:dont:803357503412502588> | You can't warn a bot")
}
if(message.author.id === user.id) {
return message.channel.send("<:dont:803357503412502588> | You can't warn yourself!")
}
const reason = args.slice(1).join(" ")
if(!reason) {
return message.channel.send("<:dont:803357503412502588> | You need a reason")
}
const warnings = {
reason: `${reason}`,
guild_id: `${message.guild.id}`,
user_id: `${user.id}`,
moderator: `${message.author.username}`
}
const jsonString = JSON.stringify(warnings)
fs.writeFile('./warnings.json', jsonString, err => {
if (err) {
console.log('Error writing file', err)
} else {
console.log('Successfully wrote file')
}
})
}
}
答案 0 :(得分:0)
const jsonString = JSON.stringify(warnings)
fs.writeFile('./warnings.json', jsonString, err => {
if (err) {
console.log('Error writing file', err)
} else {
console.log('Successfully wrote file')
}
})
你能不能用 fs.writeFileSync("./warnings.json", jsonString, {'flags': 'a'}, err => {
替换第 2 行