first of all, I'm new to discord.js v14 so still learning. I'm working on a level system based XPS and levels but when I try to fetch level even if its "0" on the database (checked it multiple times) it still fetches it as "1". Btw I have no problem with fetching XP when I try to fetch XP it gives it normally. I tried everything I know and stuck so hard on this rn, would be so happy if you guys help.
My code that gives XP to the user per message
const user = require('../schemas/user.js')
module.exports = {
system: `level`,
event: `messageCreate`,
async execute(bot, message) {
if (message.author.bot || message.channel.type === 'dm') return;
if (bot.levelCooldown.has(message.author.id)) return;
let kontrol = await user.findOne({discord_id: message.author.id});
if (!kontrol) {
let newUser = new user({
discord_id: message.author.id,
xp: 0,
level: 0,
})
await newUser.save();
}
var userdata = await user.findOne({discord_id: message.author.id}).exec();
userdata.xp = parseInt(userdata.xp) 1
if (userdata.xp < 1000) userdata.level = 0
else if (userdata.xp >= 1000) userdata.level = 1
else if (userdata.xp >= 2000) userdata.level = 2
else if (userdata.xp >= 4000) userdata.level = 3
else if (userdata.xp >= 8000) userdata.level = 4
else if (userdata.xp >= 16000) userdata.level = 5
await userdata.save();
bot.levelCooldown.add(message.author.id);
setTimeout(() => {
bot.levelCooldown.delete(message.author.id);
}, 5000);
}
};
User schema
const mongoose = require('mongoose')
const user = new mongoose.Schema({
discord_id: {
type: Number,
unique: true
},
xp: Number,
level: Number,
});
module.exports = mongoose.model('user', user)
Code that I fetch user data
const { EmbedBuilder } = require('discord.js');
const user = require('../schemas/user.js')
module.exports = {
name: 'user',
aliases: ['üye'],
async execute(bot, message, args) {
var üye;
if (!args[0]) üye = message.member
else üye = message.guild.members.cache.find(member => member.id === args[0]) || message.guild.members.cache.find(member => member.id === message.mentions.members.first().id)
if (!üye) üye = message.member
let kontrol = await user.findOne({discord_id: üye.id});
if (!kontrol) {
let newUser = new user({
discord_id: üye.id,
xp: 0,
level: 0,
})
await newUser.save();
}
var userdata = await user.findOne({discord_id: üye.id}).exec();
var hedef;
var kalan;
if (userdata.level = 0) {
hedef = 1000
kalan = parseInt(hedef) - parseInt(userdata.xp)
} else if (userdata.level = 1) {
hedef = 2000
kalan = parseInt(hedef) - parseInt(userdata.xp)
} else if (userdata.level = 2) {
hedef = 4000
kalan = parseInt(hedef) - parseInt(userdata.xp)
} else if (userdata.level = 3) {
hedef = 8000
kalan = parseInt(hedef) - parseInt(userdata.xp)
} else if (userdata.level = 4) {
hedef = 16000
kalan = parseInt(hedef) - parseInt(userdata.xp)
} else if (userdata.level = 5) {
hedef = '?'
kalan = 'Yakında'
}
var embed = new EmbedBuilder()
.setColor(bot.config.color)
.setAuthor({name: üye.user.tag, iconURL: üye.user.avatarURL()})
.setThumbnail(üye.user.avatarURL())
.setDescription(`**〉Genel bilgiler** \n**ID:** ${üye.id} \n**Avatar:** [Tıkla](${üye.user.avatarURL()}) \n*(Olum buraya ne koysam bilmiyorum amk önerin bir şeyler bana -Grkm)* \n\n**〉Seviye bilgileri** \n**Seviye:** ${userdata.level} \n**XP:** ${userdata.xp}/${hedef} \n\`Sıradaki seviye için ${kalan} xp gerekiyor\``)
message.channel.send({embeds: [embed]})
},
};
Output User command (command that fetches user data)
CodePudding user response:
Your if
statements seem to be incorrect. They are assigning a value to userdata.level
(after it is retrieved) as opposed to checking its value.
So the following:
if (userdata.level = 0) {
Should have an additional =
, such as:
if (userdata.level == 0) {
And repeat for the remainder of the else if
statements.
You could consider using a switch
statement there instead but that's an aside.