2021-03-02 22:24:33 +00:00
|
|
|
const Enmap = require('enmap');
|
2021-03-06 14:39:00 +00:00
|
|
|
const { client, logger, config, db } = require('..');
|
2021-03-02 22:24:33 +00:00
|
|
|
|
|
|
|
const levels = new Enmap({ name: "levels" });
|
|
|
|
let cooldowns = {}
|
|
|
|
|
2021-03-06 15:36:05 +00:00
|
|
|
if (process.env.DISABLE_LEVELS) logger.info('DISABLE_LEVELS is set, users won\'t be able to gain XP.');
|
|
|
|
|
2021-03-02 22:24:33 +00:00
|
|
|
client.on('message', async message => {
|
2021-03-06 15:36:05 +00:00
|
|
|
if (process.env.DISABLE_LEVELS) return;
|
|
|
|
|
2021-03-02 22:24:33 +00:00
|
|
|
if (message.author === client.user._id) return;
|
|
|
|
try {
|
2021-03-06 14:39:00 +00:00
|
|
|
if (db.blacklist.get(message.author)?.blacklisted) return;
|
|
|
|
|
2021-03-02 22:24:33 +00:00
|
|
|
const channel = await client.channels.fetch(message.channel);
|
|
|
|
if (channel.channel_type !== "Group") return;
|
2021-03-06 14:39:00 +00:00
|
|
|
let userLevels = levels.get(message.author);
|
|
|
|
if (!userLevels) userLevels = { xp: 0, level: 0 }
|
2021-03-02 22:24:33 +00:00
|
|
|
|
2021-03-06 14:39:00 +00:00
|
|
|
if (userLevels.enabled === false) return;
|
2021-03-02 22:24:33 +00:00
|
|
|
|
|
|
|
// Ensure users only get XP once a minute
|
|
|
|
if (cooldowns[message.author] > Date.now()) return;
|
|
|
|
cooldowns[message.author] = Date.now() + 1000 * 60;
|
|
|
|
|
|
|
|
// Give a random amount of XP between 15 and 25
|
2021-03-06 14:39:00 +00:00
|
|
|
userLevels.xp += 15 + Math.round(Math.random() * 10);
|
2021-03-02 22:24:33 +00:00
|
|
|
|
|
|
|
let newLevel = 0;
|
2021-03-06 14:39:00 +00:00
|
|
|
for (const i of levelups) userLevels.xp >= i && newLevel++;
|
2021-03-02 22:24:33 +00:00
|
|
|
|
2021-03-06 14:39:00 +00:00
|
|
|
if (newLevel > userLevels.level) {
|
2021-03-02 22:24:33 +00:00
|
|
|
client.channels.sendMessage(message.channel, `>GG <@${message.author}>, you just leveled up! New level: **${newLevel}**`)
|
2021-03-06 14:39:00 +00:00
|
|
|
.catch(console.error)
|
|
|
|
.then(msg => setTimeout(() =>
|
|
|
|
client.channels.deleteMessage(channel._id, msg?._id).catch(console.warn), 15000));
|
2021-03-02 22:24:33 +00:00
|
|
|
|
2021-03-06 14:39:00 +00:00
|
|
|
userLevels.level = newLevel;
|
2021-03-02 22:24:33 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 14:39:00 +00:00
|
|
|
levels.set(message.author, userLevels);
|
2021-03-02 22:24:33 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// How much XP is required to level up
|
|
|
|
// 30, 63, 99, 139, 183, 231, 284, ...
|
|
|
|
const levelups = [ 30 ];
|
|
|
|
while (levelups.length < 250) {
|
|
|
|
let i = levelups[levelups.length - 1];
|
2021-03-06 14:39:00 +00:00
|
|
|
levelups.push(Math.round(i * (i > 50000 ? 0.05 : (i < 100 ? 1.3 : 1.1))));
|
2021-03-02 22:24:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const i in levelups) levelups[i] += levelups[i - 1] || 0;
|
|
|
|
|
|
|
|
// Export level DB for access from other modules
|
|
|
|
module.exports = { levels, levelups };
|