Make a max levelup command?

I have some code that levels up your level by 1 everytime you execute the command and have the proper exp. I want to make it so if people have a lot of exp and haven’t leveled up for a long time can do something like $maxlevelup where it levels it up to the max instead of doing it everytime per levelup.

My current levelup command:

var user = message.author;

        Yen.findOne({
            guildID: bot.guilds.cache.get(message.guild.id).id,
            userID: user.id
        }, (err, data) => {

            if (err) console.log(err);
            if (!data) {
                const newData = new Yen({
                    guildID: bot.guilds.cache.get(message.guild.id).id,
                    guildName: message.guild.name,
                    name: bot.users.cache.get(user.id).username,
                    userID: user.id,
                    lb: "all",
                    yen: 0,
                    daily: 0,
                    xp: 0,
                    level: 1,
                    rank: 1,
                    class: "None",
                    wins: 0,
                    losses: 0
                })
                newData.save().catch(err => console.log(err));

                let embed = new MessageEmbed()
                    .setColor("#C586C0")
                    .setDescription(`You need **100** XP to level up! (**0** total xp)`)

                return message.channel.send(embed)

            } else {

                let level = data.level
                let xp = data.xp

                const getNeededXp = level => level * 100
                const needed = getNeededXp(level)

                function strengthInt(min, max) {
                    min = Math.ceil(min);
                    max = Math.floor(max);
                    return Math.floor(Math.random() * (max - min + 1)) + min;
                }

                var strengthupgrade = level * (strengthInt(2, 1))
                var speedupgrade = level * (strengthInt(2, 1))
                var agilityupgrade = level * (strengthInt(2, 1))
                var defenseupgrade = level * (strengthInt(2, 1))

                function maxhealthInt(min, max) {
                    min = Math.ceil(min);
                    max = Math.floor(max);
                    return Math.floor(Math.random() * (max - min + 1)) + min;
                }

                var maxHealthupgrade = level * maxhealthInt(2, 3)

                if (xp >= needed) {
                    ++level
                    xp -= needed

                    Stats.findOne({
                        guildID: bot.guilds.cache.get(message.guild.id).id,
                        userID: user.id
                    }, (err, statsdata) => {
                        if (err) console.log(err)
                        if (!statsdata) {
                            const newDataa = new Stats({
                                guildID: bot.guilds.cache.get(message.guild.id).id,
                                guildName: message.guild.name,
                                name: bot.users.cache.get(user.id).username,
                                userID: user.id,
                                statslb: "all",
                                strength: 0,
                                speed: 0,
                                agility: 0,
                                defense: 0,
                                trainTimeout: 0,
                                health: 100,
                                maxHealth: 100,
                            })
                            newDataa.save().catch(err => console.log(err));

                        } else {
                            statsdata.strength += strengthupgrade
                            statsdata.speed += speedupgrade
                            statsdata.agility += agilityupgrade
                            statsdata.defense += defenseupgrade

                            statsdata.maxHealth += maxHealthupgrade
                            statsdata.health += maxHealthupgrade

                            let strength = statsdata.strength
                            let speed = statsdata.speed
                            let agility = statsdata.agility
                            let defense = statsdata.defense
                            
                            let maxHealth = statsdata.maxHealth
                            let health = statsdata.health

                            async function updateOneta() {
                                await Stats.updateOne({
                                    guildID: bot.guilds.cache.get(message.guild.id).id,
                                    userID: message.author.id
                                }, {
                                    strength,
                                    speed,
                                    agility,
                                    defense,
                                    health,
                                    maxHealth,
                                })
                            }
                            updateOneta()
                        }
                    })

                    let lvlupembed = new MessageEmbed()
                        .setTitle("Level Up!")
                        .setColor("RANDOM")
                        .setDescription(`
                        You are now level **${level.toLocaleString()}** with **${xp.toLocaleString()}** XP!
                        You gained **+${strengthupgrade.toLocaleString()}** strength!
                        You gained **+${speedupgrade.toLocaleString()}** speed!
                        You gained **+${agilityupgrade.toLocaleString()}** agility!
                        You gained **+${defenseupgrade.toLocaleString()}** defense!
                        You gained **+${maxHealthupgrade.toLocaleString()}** health!`)
                        .setFooter(`${getNeededXp(level).toLocaleString()} XP needed to level up again!`)

                    message.reply(lvlupembed)

                    async function updateOnelevels() {

                        await Yen.updateOne({
                            guildID: bot.guilds.cache.get(message.guild.id).id,
                            userID: message.author.id
                        }, {
                            level,
                            xp,
                        })
                    }

                    updateOnelevels()



                } else {

                    let embed = new MessageEmbed()
                    .setColor("#C586C0")
                    .setDescription(`You need **${getNeededXp(level).toLocaleString()}** XP to level up!`)

                return message.channel.send(embed)
                }
            }
        })

Please help!!

Try doing a foreach and check how much XP they need for a lvl. Such as I have 100 xp the next one requires 5. I would do a foreach subtracting the amount of XP for each lvl until I reach a negative number. I will go back one lvl and that is the lvl to rank me.

Ok, your code for the command inside should be something like

if (Yen.xp >= Yen.level * 100) {
var amount = Yen.xp / 100 // makes it so that it should be something like 1, 2, 3, 4. It might break if you let people do this without xp or levels
Yen.level = Math.floor(amount)
}

what? What should I do??

put the code inside the command you want. I would recommend that you put this at the bottom

What would It to do? Make it so they can’t go past level 100?

No, it would run a check on the players xp. If they have over four hundred let’s say, level them up to 400. However, I noticed a bug in the code without even running it (ha) and you could just go like this

800 xp: 8th level, then you’d do

100 xp: 9th level

Wait wut???

instead of Yen its data and also, after this if statement is executed, should I do
data.save().catch(err => console.log(err))

maybe not, if you are coding a discord bot.

if (Yen.xp >= Yen.level * 100) {
var amount = Math.floor(Yen.xp);
if (amount > Yen.level * 100) {
Yen.level += amount
}
}

Updated my code

Should I do data.save()??? Im using mongoose.

save, or do the perfect method of saving (which I won’t mention)

How? I’ve tried some while loops but those didn’t work…

Have like an array of data for each lvl

Then do for each to check how much XP is required.

How would I do that in mongoose tho…

I can show you what the schema might look like.

{
levels: {
type: array
 }
}

Or you can check through the documents.

{
level: {
type: nested
required: true
 }
}

And to use each would be like this

var modelfind = levelarraymodel.find() //gets array of the model
var data = modelfind[0] //cause .find() returns an array of what it finds.
//foreach function here

or

levelmodel.find() //gets array of model
//foreach function here

I want to use my current schema tho… not make a new one.