简体   繁体   中英

Discord.js Level System

My 'problem' is more of a feature I am looking to add, I used this guide: https://anidiots.guide/coding-guides/sqlite-based-points-system I changed the code a little to mainly give you a random amount of XP, I am looking to edit how much XP is needed to level up.

Right now it is a static amount, being 5000 needed to level up. I am trying to make it increase the amount needed to level up by an extra 5000 each time you level up.

Currently, it works like this:

Level 1 to 2 = 5000 total XP needed

Level 2 to 3 = 10000 total xp needed

Currently, the amount needed to level up is always 5000 between each level.

This is how I want it to work:

Level 1 to 2 = 5000 total XP needed

Level 2 to 3 = 15000 total XP needed

Which will be 5000 to level 2 and then 10000 to level 3 and so on (increasing the amount needed by 5000 each time you level up)

I spent the best part of 2 hours trying different things, and mainly looking at the code being completely out of my depth. I believed that doing something like this would work, but I have no idea if it's correct

if (score.level == '1') {
    nextLevel = 5000
}
if (score.level == '2' {
    nextLevel = 10000
}

I highly doubt this is correct, otherwise, my message event would be very long, as I plan to have 100 levels

The code in its entirety:

    let score;
    if (message.guild) {
        score = bot.getScore.get(message.author.id, message.guild.id);
        if (!score) {
            score = {
                id: `${message.guild.id}-${message.author.id}`,
                user: message.author.id,
                guild: message.guild.id,
                points: 0,
                level: 1,
            };
        }
        const xpAdd = Math.floor(Math.random() * 10) + 50;
        const curxp = score.points;
        const curlvl = score.level;
        const nxtLvl = score.level * 5000;
        score.points = curxp + xpAdd;
        if (nxtLvl <= score.points) {
            score.level = curlvl + 1;
            const lvlup = new MessageEmbed()
                .setAuthor(
                    `Congrats ${message.author.username}`,
                    message.author.displayAvatarURL()
                )
                .setTitle('You have leveled up!')
                .setThumbnail('https://i.imgur.com/lXeBiMs.png')
                .setColor(color)
                .addField('New Level', curlvl + 1);
            message.channel.send(lvlup).then(msg => {
                msg.delete({
                    timeout: 10000,
                });
            });
        }
        bot.setScore.run(score);
    }

The code as-is works fine and as expected, but as-is is not very good, as there is no reward from going from level 30-31 as it's the same amount of XP needed to get from level 1-2

Here's a little formula which should do the trick (if I understand your problem correctly):

const nxtLvl = 5000 * (Math.pow(2, score.level) - 1);

This gives the following xp requirements to level up:

1->2:  5000
2->3:  15000
3->4:  35000
4->5:  75000
5->6: 155000

Try something like this:

const levels = [0, 5000, 15000, 30000, 50000, 75000];
....
nextLevel = levels[score.level];

Edit

@Dan you mean like this:

nextLevel = 5000 * Math.round( score.level * (score.level + 1) / 2 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM