<commit_before>const moment = require('moment');

moment.locale('de');

function Levels(bot) {
    this.bot = bot;

    this.users = {};
}

Levels.prototype.getLevelExp = function (level) {
    return 5 * (Math.pow(level, 2)) + 50 * level + 100;
};

Levels.prototype.getLevelFromXp = function (xp) {
    let level = 0;

    while (xp >= this.getLevelExp(level)) {
        xp -= this.getLevelExp(level);
        level++;
    }

    return level;
};

Levels.prototype.getRandomExp = function () {
    return this.bot.randomInt(15, 25);
};

Levels.prototype.giveExp = function (message) {
    const key = `${this.bot.server.id}.Levels`;
    const field = message.author.id;

    if(message.author.id in this.users) {
        const timeoutEnd = this.users[message.author.id];

        if(timeoutEnd.diff(moment()) > 0) {
            return;
        }
    }

    let getCurrentExp = () => {
        this.bot.redis.hget(key, field, (err, result) => {
            if(err) {
                return this.bot.log(`[Levels] Could not giveExp (hget) ${err}`);
            }

            let currentExp = 0;

            if(result != null) {
                currentExp = result;
            }

            updateExp(parseInt(currentExp));
        })
    };

    let updateExp = currentExp => {
        const newExp = currentExp + this.getRandomExp();

        this.bot.redis.hset(key, field, newExp, err => {
            if(err) {
                return this.bot.log(`[Levels] Could not giveExp (hset) ${err}`);
            }

            this.users[message.author.id] = moment().add(1, 'm');

            const currentLevel = this.getLevelFromXp(currentExp);
            const newLevel = this.getLevelFromXp(newExp);

            if(currentLevel != newLevel) {
                this.bot.respond(message, `du hast soeben Level ${newLevel} erreicht!`);
            }
        })
    };

    getCurrentExp();
};

Levels.prototype.setExp = function (member, exp, callback) {
    const key = `${this.bot.server.id}.Levels`;
    const field = member.id;

    this.bot.redis.hset(key, field, exp, err => {
        if(err) {
            return callback(err);
        }

        callback(false);
    })
};


if (!(typeof exports === 'undefined')) {
    exports.Levels = Levels;
}
<commit_msg>added new util commands for levels<commit_after>const moment = require('moment');

moment.locale('de');

function Levels(bot) {
    this.bot = bot;

    this.users = {};
}

Levels.prototype.getLevelExp = function (level) {
    return 5 * (Math.pow(level, 2)) + 50 * level + 100;
};

Levels.prototype.getLevelFromXp = function (xp) {
    let level = 0;

    while (xp >= this.getLevelExp(level)) {
        xp -= this.getLevelExp(level);
        level++;
    }

    return level;
};

Levels.prototype.getRandomExp = function () {
    return this.bot.randomInt(15, 25);
};

Levels.prototype.giveExp = function (message) {
    const key = `${this.bot.server.id}.Levels`;
    const field = message.author.id;

    if (message.author.id in this.users) {
        const timeoutEnd = this.users[message.author.id];

        if (timeoutEnd.diff(moment()) > 0) {
            return;
        }
    }

    let getCurrentExp = () => {
        this.bot.redis.hget(key, field, (err, result) => {
            if (err) {
                return this.bot.log(`[Levels] Could not giveExp (hget) ${err}`);
            }

            let currentExp = 0;

            if (result != null) {
                currentExp = result;
            }

            updateExp(parseInt(currentExp));
        })
    };

    let updateExp = currentExp => {
        const newExp = currentExp + this.getRandomExp();

        this.bot.redis.hset(key, field, newExp, err => {
            if (err) {
                return this.bot.log(`[Levels] Could not giveExp (hset) ${err}`);
            }

            this.users[message.author.id] = moment().add(1, 'm');

            const currentLevel = this.getLevelFromXp(currentExp);
            const newLevel = this.getLevelFromXp(newExp);

            if (currentLevel != newLevel) {
                this.bot.respond(message, `du hast soeben Level ${newLevel} erreicht!`);
            }
        })
    };

    getCurrentExp();
};


Levels.prototype.getExp = function (member, callback) {
    const key = `${this.bot.server.id}.Levels`;
    const field = 'id' in member ? member.id : member;

    this.bot.redis.hget(key, field, (err, result) => {
        if (err) {
            return callback(err, null);
        }

        callback(false, result);
    });
};

Levels.prototype.getAllExp = function (callback) {
    const key = `${this.bot.server.id}.Levels`;

    this.bot.redis.hgetall(key, (err, results) => {
        if (err) {
            return callback(err, null);
        }

        callback(false, results);
    });
};

Levels.prototype.getRanklist = function (callback) {
    this.getAllExp((err, results) => {
        if (err) {
            return callback(err, null);
        }

        let ranks = Object.keys(results).map(function (key) {
            return [key, results[key]];
        });

        ranks.sort(function (first, second) {
            return second[1] - first[1];
        });

        callback(false, ranks);
    });
};

Levels.prototype.setExp = function (member, exp, callback) {
    const key = `${this.bot.server.id}.Levels`;
    const field = 'id' in member ? member.id : member;

    this.bot.redis.hset(key, field, exp, err => {
        if (err) {
            return callback(err);
        }

        callback(false);
    });
};


if (!(typeof exports === 'undefined')) {
    exports.Levels = Levels;
}

{'fraction_non_alphanumeric': 0.15143417067521825, 'fraction_numerical': 0.005701051131302334, 'mean_word_length': 2.0395235517054684, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2529977', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1752, 'n_words': 364}