revolt-bot/util/levels.js
2021-03-02 23:24:33 +01:00

52 lines
1.8 KiB
JavaScript

const Enmap = require('enmap');
const { client, logger, config } = require('..');
const levels = new Enmap({ name: "levels" });
let cooldowns = {}
client.on('message', async message => {
if (message.author === client.user._id) return;
try {
const channel = await client.channels.fetch(message.channel);
if (channel.channel_type !== "Group") return;
let groupLevels = levels.get(channel._id);
if (!groupLevels) groupLevels = {}
if (!groupLevels[message.author]) groupLevels[message.author] = { xp: 0, level: 0, enabled: true }
if (groupLevels[message.author].enabled === false) return;
// 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
groupLevels[message.author].xp += 15 + Math.round(Math.random() * 10);
let newLevel = 0;
for (const i of levelups) groupLevels[message.author].xp >= i && newLevel++;
if (newLevel > groupLevels[message.author].level) {
client.channels.sendMessage(message.channel, `>GG <@${message.author}>, you just leveled up! New level: **${newLevel}**`)
.catch(console.error);
groupLevels[message.author].level = newLevel;
}
levels.set(channel._id, groupLevels);
} 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];
levelups.push(Math.round(i * (i > 50000 ? 0.02 : (i < 100 ? 1.1 : 1.05))));
}
for (const i in levelups) levelups[i] += levelups[i - 1] || 0;
// Export level DB for access from other modules
module.exports = { levels, levelups };