【发布时间】:2021-06-25 17:28:56
【问题描述】:
所以我创建了一个简单地从 JSON 文件中添加或删除 ID 的命令。这个 JSON 只是白名单用户的一个小型数据库。
该命令运行良好,并且 JSON 文件得到更新,但每次调用该命令再次对其进行编辑时,它都不会读取新版本,而是读取未编辑的旧版本。
所以我猜我需要为机器人更新它,但我不知道该怎么做。
任何人都知道这是如何发生或为什么会发生并有解决方案吗?这是命令代码:
else if (command === 'whitelist') {
const user = msg.mentions.users.first();
if (!user) return msg.reply('Please mention a user!')
const tester = require('./whitelist.json')
const test = Object.values(tester.newdata).find(object => object.ID === msg.author.id)
if (!test) return msg.reply("Sorry you can't use this!");
if (test) {
const json = require('./whitelist.json')
const item = Object.values(json.newdata).find(object => object.ID === user.id)
if (item) {
msg.reply("Remove mentioned user from whitelist?")
msg.react('????').then(r => {
msg.react('????');
});
msg.awaitReactions((reaction, user) => user.id == msg.author.id && (reaction.emoji.name == '????' || reaction.emoji.name == '????'), {
max: 1,
time: 30000
}).then(collected => {
if (collected.first().emoji.name == '????') {
let rawdata = fs.readFileSync('./whitelist.json');
let data = JSON.parse(rawdata);
delete data.newdata[user]
fs.writeFileSync("./whitelist.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("Removed user!")
} else
msg.reply('Operation canceled.');
}).catch(() => {
msg.reply('No reaction after 30 seconds, operation canceled');
});
return
}
if (!item) {
let rawdata = fs.readFileSync('./whitelist.json');
msg.reply("Add mentioned user to whitelist?")
msg.react('????').then(r => {
msg.react('????');
});
msg.awaitReactions((reaction, user) => user.id == msg.author.id && (reaction.emoji.name == '????' || reaction.emoji.name == '????'), {
max: 1,
time: 30000
}).then(collected => {
if (collected.first().emoji.name == '????') {
let data = JSON.parse(rawdata);
data.newdata[user] = {
"ID": user.id
}
fs.writeFileSync("./whitelist.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
}
}
}
【问题讨论】:
-
如果你有办法改进我的代码,我很高兴听到它
-
你应该缩进你的代码
-
这对你来说可能有用,但我更喜欢如果它全部排好不要杀了我 o.o
-
你真的喜欢这种“缩进”吗?
-
我的意思是我做 lmao
标签: javascript node.js json discord discord.js