2021年3月28日星期日

How to remove specific sections in a json file

Alright so i have a JSON file where i keep a few user ids to come back to for sertain command. Now what i want to do is be able to add and remove ids with commands. Ive gotten the adding all figuered out but removing them i cant for the life of me

else if (command === 'whitelist') { //command  var checker = whitelist; //whitelist json file  let test = Object.values(checker.users).find((value) => value.id === msg.author.id); //check id  if(!test)return msg.reply("Sorry you can't use this!");  if(test){  const user = msg.mentions.users.first();  if (!user) return msg.reply('Please mention a user!')  const json = require('./test.json') //teporary test file will be whitelist.json later  const item = Object.values(json.newdata).find(object => object.ID === user.id)  if (item){  msg.reply("Remove mentioned user from whitelist?")  msg.react(':thumbsup:').then(r => {  msg.react(':thumbsdown:');});  msg.awaitReactions((reaction, user) => user.id == msg.author.id && (reaction.emoji.name == ':thumbsup:' || reaction.emoji.name == ':thumbsdown:'),  { max: 1, time: 30000 }).then(collected => {if (collected.first().emoji.name == ':thumbsup:') {    //here idk what to add to remove a sertain section from a json    msg.reply("Removed user!")}  else  msg.reply('Operation canceled.');  }).catch(() => {msg.reply('No reaction after 30 seconds, operation canceled');});  return  }  if (!item){   let rawdata = fs.readFileSync('./test.json');  msg.reply("Add mentioned user to whitelist?")  msg.react(':thumbsup:').then(r => {  msg.react(':thumbsdown:');});  msg.awaitReactions((reaction, user) => user.id == msg.author.id && (reaction.emoji.name == ':thumbsup:' || reaction.emoji.name == ':thumbsdown:'),  { max: 1, time: 30000 }).then(collected => {if (collected.first().emoji.name == ':thumbsup:') {  let data = JSON.parse(rawdata);  data.newdata [user] = {"ID": user.id}  fs.writeFile("./test.json", JSON.stringify(data, null, 2), (err) => {  if (err)   embed = new Discord.MessageEmbed()  .setTitle('Error report')  .addField(`Logged error:`, err, false)  .addField(`Server:`, `${msg.guild}`, false)  .addField(`Command:`, `${command} ${commandArgs}`, false)  .setColor('#0099ff');  webhookClient.send(embed)  })  msg.reply("Whitelisted user!")}  else  msg.reply('Operation canceled.');  }).catch(() => {msg.reply('No reaction after 30 seconds, operation canceled');});  return}}  }
above u can find the whole command heres the Json file
{    "newdata": {      "<@804786710264938536>": {//for example if i want to i could remove this id from the file         "ID": "804786710264938536"      },      "<@361511417800556546>": {        "ID": "361511417800556546"      }    }  }
https://stackoverflow.com/questions/66847538/how-to-remove-specific-sections-in-a-json-file March 29, 2021 at 08:49AM

没有评论:

发表评论