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-16 13:16:17 +00:00
let Filter = require ( 'bad-words' ) ,
filter = new Filter ( ) ;
2021-03-02 22:24:33 +00:00
const levels = new Enmap ( { name : "levels" } ) ;
let cooldowns = { }
2021-03-16 13:16:17 +00:00
if ( process . env . DISABLE _LEVELS === 'true' ) logger . info ( 'DISABLE_LEVELS is set, users won\'t be able to gain XP.' ) ;
2021-03-06 15:36:05 +00:00
2021-03-02 22:24:33 +00:00
client . on ( 'message' , async message => {
2021-03-16 13:16:17 +00:00
if ( process . env . DISABLE _LEVELS === 'true' ) return ;
2021-03-06 15:36:05 +00:00
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-16 13:16:17 +00:00
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 ) ;
}
2021-03-02 22:24:33 +00:00
2021-03-16 13:16:17 +00:00
if ( xpDiff !== 0 ) {
logger . debug ( ` ${ message . author } => ${ xpDiff } XP ` ) ;
userLevels . xp += xpDiff ;
2021-03-16 13:26:39 +00:00
if ( userLevels . xp < 0 ) userLevels . xp = 0 ;
2021-03-16 13:16:17 +00:00
}
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-16 13:16:17 +00:00
}
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 ) ) ;
2021-03-02 22:24:33 +00:00
}
2021-03-16 13:16:17 +00:00
userLevels . level = newLevel ;
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 } ;