I am making a Level system for my bot. It's working perfectly, but there's one problem about the level system, It is when the bot making a restart, the current exp is resetting too. How can I stop it like that?
Here is my current code.
const db = require('quick.db');
if (!db[msg.author.id]) db[msg.author.id] = {
xp: 0,
level: 0
};
db[msg.author.id].xp ;
let userInfo = db[msg.author.id];
if(userInfo.xp > 100) {
userInfo.level
userInfo.xp = 0
msg.reply("Congratulations, you level up")
}
if(msg.content.toLowerCase().startsWith(`${prefix}level`)) {
let userInfo = db[msg.author.id];
let member = msg.mentions.members.first();
let embed = new MessageEmbed()
.setTitle('Level Checking')
.setDescription("<@" msg.member.id ">" ` Your current level is: ${userInfo.level}`)
.setColor('RANDOM')
.addFields({name:"Level", value: `${userInfo.level}`})
.addField("XP", userInfo.xp "/100")
.setTimestamp()
.setFooter('Level | Reborn')
if(!member) return msg.channel.send({embeds: [embed]})
let memberInfo = db[member.id]
let embed2 = new MessageEmbed()
.setTitle('Level Checking')
.setDescription("<@" msg.member.id ">" ` Your current level is: ${memberInfo.level}`)
.setColor('RANDOM')
.addFields({name:"Level", value: `${memberInfo.level}`})
.addField("XP", memberInfo.xp "/100")
.setTimestamp()
.setFooter('Level | Reborn')
msg.channel.sendEmbed({embeds: [embed2]})
}
I'm using Discordjs v13
CodePudding user response:
I don't see you are connecting to a database. All I see that you are using quick.db
I have not used quick.db but from the way you have written I am assuming that it is storing the data in a temp variable called db. You need to store that db in a database (for you library it should be sqlite3)
CodePudding user response:
You are storing the data in the variable db
.
Variables always get resettet when your bot restarts.
To keep the data you need to write the data to the actual database
For example:
db.set(msg.author.id.'level', 0)
would set the users level to 0
Check the docs for more inforamtion about how getting data(and how to set/modify it)