75 lines
2.9 KiB
JavaScript
75 lines
2.9 KiB
JavaScript
const Enmap = require('enmap');
|
|
const { client, logger, config, db } = require('..');
|
|
let Filter = require('bad-words'),
|
|
filter = new Filter();
|
|
|
|
const levels = new Enmap({ name: "levels" });
|
|
let cooldowns = {}
|
|
|
|
if (process.env.DISABLE_LEVELS === 'true') logger.info('DISABLE_LEVELS is set, users won\'t be able to gain XP.');
|
|
|
|
client.on('message', async message => {
|
|
if (process.env.DISABLE_LEVELS === 'true') return;
|
|
|
|
if (message.author === client.user._id) return;
|
|
try {
|
|
if (db.blacklist.get(message.author)?.blacklisted) return;
|
|
|
|
const channel = await client.channels.fetch(message.channel);
|
|
if (channel.channel_type !== "Group") return;
|
|
let userLevels = levels.get(message.author);
|
|
if (!userLevels) userLevels = { xp: 0, level: 0 }
|
|
|
|
let xpDiff = 0;
|
|
if (process.env.DISABLE_PROFANITY_FILTER !== 'true' && filter.isProfane(message.content)) {
|
|
// Take a random amount of XP between 5 and 15
|
|
xpDiff = (5 + Math.round(Math.random() * 10)) * -1;
|
|
}
|
|
else if (cooldowns[message.author] <= Date.now()) {
|
|
// Ensure users only get XP once a minute
|
|
cooldowns[message.author] = Date.now() + 1000 * 60;
|
|
// Give a random amount of XP between 15 and 25
|
|
xpDiff += 15 + Math.round(Math.random() * 10);
|
|
}
|
|
|
|
if (xpDiff !== 0) {
|
|
logger.debug(`${message.author} => ${xpDiff} XP`);
|
|
userLevels.xp += xpDiff;
|
|
if (userLevels.xp < 0) userLevels.xp = 0;
|
|
}
|
|
|
|
let newLevel = 0;
|
|
for (const i of levelups) userLevels.xp >= i && newLevel++;
|
|
|
|
if (newLevel > userLevels.level) {
|
|
client.channels.sendMessage(message.channel, `>GG <@${message.author}>, you just leveled up! New level: **${newLevel}**`)
|
|
.catch(console.error)
|
|
.then(msg => setTimeout(() =>
|
|
client.channels.deleteMessage(channel._id, msg?._id).catch(console.warn), 15000));
|
|
}
|
|
else if (newLevel < userLevels.level) {
|
|
client.channels.sendMessage(message.channel, `> GG <@${message.author}>, you posted so much cringe that you just lost a level! You are now level **${newLevel}**.`)
|
|
.catch(console.error)
|
|
.then(msg => setTimeout(() =>
|
|
client.channels.deleteMessage(channel._id, msg?._id).catch(console.warn), 15000));
|
|
}
|
|
|
|
userLevels.level = newLevel;
|
|
levels.set(message.author, userLevels);
|
|
} 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.05 : (i < 100 ? 1.3 : 1.1))));
|
|
}
|
|
|
|
for (const i in levelups) levelups[i] += levelups[i - 1] || 0;
|
|
|
|
// Export level DB for access from other modules
|
|
module.exports = { levels, levelups }; |