【发布时间】:2021-06-25 02:07:32
【问题描述】:
好的,所以我有一个 JSON 文件,我在其中保留了一些用户 ID,以便返回某些命令。现在我想做的是能够使用命令添加和删除 id。我已经把所有的添加都弄清楚了,但是我一辈子都无法删除它们
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}}
}
{
"newdata": {
"<@804786710264938536>": {//for example if i want to i could remove this id from the file
"ID": "804786710264938536"
},
"<@361511417800556546>": {
"ID": "361511417800556546"
}
}
}
【问题讨论】:
-
您在寻找删除运算符吗? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…如果您有要删除的属性的名称,您只需执行
delete obj.prop
标签: javascript json discord.js