因此,我使用fs在discord.js中为调平系统创建了一个脚本,以便每次有人键入一条消息时,都应该将数据保存在一个名为database.json的文件中。每当我键入一条消息时,它都会记录保存,但文件似乎为空。这是代码:
const lvlupEmbed = new Discord.MessageEmbed()
.setTitle("Noice, you leveled up!")
.setImage("https://www.pngkit.com/png/full/268-2687945_level-up-nasty-juice-level-up.png")
if (message.author.bot) return;
if (!db[message.author.id]) db[message.author.id] = {
xp: 0,
level: 0
};
db[message.author.id].xp++;
let userInfo = db[message.author.id];
if(userInfo.xp > 100) {
userInfo.level++
userInfo.xp = 0
message.channel.reply(lvlupEmbed)
}
fs.writeFile('database.json', JSON.stringify(db), (x) => {
if (x) console.error(x)
console.log('Data stored to database.')