【问题标题】:Discord.js i make a warn command that write a file using FSDiscord.js 我发出警告命令,使用 FS 写入文件
【发布时间】:2021-06-09 10:51:07
【问题描述】:

因此,当我警告用户时,它可以正常工作,但是当我再次尝试警告某人时,它会替换旧字符串(原因),但我想让它添加并保留旧字符串。 (原因)我尝试了很多东西,但 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')
        
    }
})


}
}
   

【问题讨论】:

    标签: javascript command discord.js fs


    【解决方案1】:
       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')
            
        }
    })
    

    你能把第 2 行换成fs.writeFileSync("./warnings.json", jsonString, {'flags': 'a'}, err =&gt; {

    【讨论】:

    • 我做到了,但是当我第一次写它时尝试警告但当我再次尝试警告时它替换并删除旧的(第一次警告)
    猜你喜欢
    • 2021-06-02
    • 2021-02-27
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 2020-09-14
    • 2021-10-20
    • 2019-03-05
    相关资源
    最近更新 更多