SHARE
TWEET

New Level Code (Supporting SQLite) v2.3

a guest Feb 14th, 2020 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. New Level Code (Supporting SQLite) v2.3
  3.  
  4. How to setup:
  5. 1. Run command prompt or powershell by Admin and run "npm i -g --production windows-build-tools"
  6. 2. Install "npm i discord.js better-sqlite3" for your bot
  7. 3. Install "npm i mathjs"
  8. (btw you dont need to make any files.. they will be made automaticly)
  9. */
  10.  
  11. // /*
  12. const SQLite = require('better-sqlite3')
  13. const sql = new SQLite('./levels.sqlite')
  14. const newMath = require('mathjs')
  15.  
  16. bot.on("ready", () => {
  17.     const table = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'levels';").get()
  18.     if(!table['count(*)']){
  19.         sql.prepare("CREATE TABLE levels (id TEXT PRIMARY KEY, user TEXT, guild TEXT, xp INTEGER, vxp INTEGER, level INTEGER);").run()
  20.         sql.prepare("CREATE UNIQUE INDEX idx_levels_id ON levels (id);").run()
  21.         sql.pragma("synchronous = 1")
  22.         sql.pragma("journal_mode = wal")
  23.     }
  24.  
  25.     bot.getlevel = sql.prepare("SELECT * FROM levels WHERE user = ? AND guild = ?")
  26.     bot.setlevel = sql.prepare("INSERT OR REPLACE INTO levels (id, user, guild, xp, vxp, level) VALUES (@id, @user, @guild, @xp, @vxp, @level);")
  27. })
  28.  
  29. bot.on('voiceStateUpdate', (oldMember, newMember) => {
  30.  
  31.     // Settings for voice state update \\
  32.     if(newMember.guild.id == 314762746380156928) var msgChat = "🤜-main-chat-🤛" // Auto message chat Name
  33.     if(newMember.guild.id == 483687285129216000) var msgChat = "level-up" // Auto message chat Name
  34.     var xpMultiplyer = 200 // How much xp is muliplyed to get lvls (You need to set same to text chat!!!!!)
  35.     var timeSetting = 120 // Time for intervals (Seconds)
  36.  
  37.     var time = newMath.multiply(timeSetting, 1000)
  38.     setInterval(() => {
  39.         if(newMember){
  40.             if(newMember.user.bot) return
  41.             if(!newMember.voiceChannel) return
  42.  
  43.             var user = newMember.user
  44.  
  45.             var level
  46.  
  47.             if(newMember.guild){
  48.                 level = bot.getlevel.get(newMember.user.id, newMember.guild.id)
  49.        
  50.                 if(!level){
  51.                     level = {
  52.                         id: `${newMember.guild.id}-${newMember.user.id}`,
  53.                         user: newMember.user.id,
  54.                         guild: newMember.guild.id,
  55.                         xp: 0,
  56.                         vxp: 0,
  57.                         level: 1
  58.                     }
  59.                 }
  60.             }
  61.  
  62.             var vxp = level.vxp
  63.             var curxp = level.xp
  64.             var allxp = curxp + vxp
  65.             var curlvl = level.level
  66.             var nextlvl = curlvl * xpMultiplyer * curlvl            
  67.  
  68.             level.vxp += 1
  69.  
  70.             if(nextlvl <= allxp){
  71.                 let curLevel = curlvl + 1
  72.                 let levelUpEmbed = new Discord.RichEmbed()
  73.                 .setAuthor(user.tag)
  74.                 .setColor(`#20f95a`)
  75.                 .addField(`You've leveled up to **${curLevel}** Level!`, `You have **${allxp}** xp`)
  76.                .setThumbnail(user.avatarURL)
  77.                .setTimestamp()
  78.  
  79.                newMember.guild.channels.find(channel => channel.name === msgChat).send(levelUpEmbed)
  80.  
  81.                level.level += 1
  82.            }
  83.  
  84.            bot.setlevel.run(level)
  85.  
  86.        } else if(oldMember){
  87.            if(oldMember.user.bot) return
  88.            if(!oldMember.voiceChannel) return
  89.  
  90.            var user = oldMember.user
  91.  
  92.            var level
  93.  
  94.            if(oldMember.guild){
  95.                level = bot.getlevel.get(oldMember.user.id, oldMember.guild.id)
  96.  
  97.                if(!level){
  98.                    level = {
  99.                        id: `${oldMember.guild.id}-${oldMember.user.id}`,
  100.                        user: oldMember.user.id,
  101.                        guild: oldMember.guild.id,
  102.                        xp: 0,
  103.                        vxp: 0,
  104.                        level: 1
  105.                    }
  106.                }
  107.            }
  108.  
  109.            var vxp = level.vxp
  110.            var curxp = level.xp
  111.            var allxp = curxp + vxp
  112.            var curlvl = level.level
  113.            var nextlvl = curlvl * xpMultiplyer * curlvl            
  114.  
  115.            level.vxp += 1
  116.  
  117.            if(nextlvl <= allxp){
  118.                let curLevel = curlvl + 1
  119.                let levelUpEmbed = new Discord.RichEmbed()
  120.                .setAuthor(user.tag)
  121.                .setColor(`#20f95a`)
  122.                .addField(`You've leveled up to **${curLevel}** Level!`, `You have **${allxp}** xp`)
  123.                 .setThumbnail(user.avatarURL)
  124.                 .setTimestamp()
  125.  
  126.                 oldMember.guild.channels.find(channel => channel.name === msgChat).send(levelUpEmbed)
  127.  
  128.                 level.level += 1
  129.             }
  130.  
  131.             bot.setlevel.run(level)
  132.  
  133.         }
  134.     }, time) // Time for how much you need to be in VC to get 2 XP!! (ms)
  135. })
  136.  
  137. bot.on("message", message => {
  138.     if(message.author.bot) return
  139.     if(!message.guild) return
  140.  
  141.     var xpMin = 5   // Minimum xp for lvl System
  142.     var xpMax = 25  // Maximum xp for lvl System
  143.     var xpMultiplyer = 200  // Xp Multriplyer
  144.     // (to get 1st lvl you need 200 (for example) xp, to get 2nd you need 200 * 2 (2 is level) and so on)
  145.  
  146.     const args = message.content.slice(PREFIX.length).trim().split(/ +/g)
  147.    
  148.     const targetUser = message.mentions.users.first() || bot.users.get(args[0])
  149.     var user = message.author
  150.  
  151.     var level
  152.  
  153.     if(message.guild){
  154.         level = bot.getlevel.get(message.author.id, message.guild.id)
  155.  
  156.         if(!level){
  157.             level = {
  158.                 id: `${message.guild.id}-${message.author.id}`,
  159.                 user: message.author.id,
  160.                 guild: message.guild.id,
  161.                 xp: 0,
  162.                 vxp: 0,
  163.                 level: 1
  164.             }
  165.         }
  166.     }
  167.  
  168.     var vxp = level.vxp
  169.     var curxp = level.xp
  170.     var allxp = curxp + vxp
  171.     var curlvl = level.level
  172.     var nextlvl = curlvl * xpMultiplyer * curlvl
  173.     var minusOneLvl = newMath.subtract(curlvl, 1)
  174.     var prevlvl = minusOneLvl * xpMultiplyer * minusOneLvl
  175.     var sublvl = nextlvl - prevlvl
  176.  
  177.     if(targetUser){
  178.         var tlevel
  179.         if(message.guild){
  180.             tlevel = bot.getlevel.get(targetUser.id, message.guild.id)
  181.    
  182.             if(!tlevel){
  183.                 tlevel = {
  184.                     id: `${message.guild.id}-${targetUser.id}`,
  185.                     user: targetUser.id,
  186.                     guild: message.guild.id,
  187.                     xp: 0,
  188.                     vxp: 0,
  189.                     level: 1
  190.                 }
  191.             }
  192.         }
  193.  
  194.         var tvxp = tlevel.vxp
  195.         var tcurxp = tlevel.xp
  196.         var tallxp = tcurxp + tvxp
  197.         var tcurlvl = tlevel.level
  198.         var tnextlvl = tcurlvl * xpMultiplyer * tcurlvl
  199.         var tminusOneLvl = newMath.subtract(tcurlvl, 1)
  200.         var tprevlvl = tminusOneLvl * xpMultiplyer * tminusOneLvl
  201.         var tsublvl = tnextlvl - tprevlvl
  202.     }
  203.    
  204.  
  205.     if(!message.content.startsWith(PREFIX)){
  206.         if(message.guild){
  207.             // Xp adding
  208.             var xpRandom = Math.floor(Math.random() * (xpMax - xpMin + 1)) + xpMin
  209.  
  210.             level.xp += xpRandom
  211.  
  212.             var curLevel = level.level + 1
  213.  
  214.             if(nextlvl <= allxp){
  215.                 let levelUpEmbed = new Discord.RichEmbed()
  216.                 .setAuthor(user.tag)
  217.                 .setColor(`#20f95a`)
  218.                 .addField(`You've leveled up to **${curLevel}** Level!`, `You have **${allxp}** xp`)
  219.                .setThumbnail(user.avatarURL)
  220.                .setTimestamp()
  221.  
  222.                message.channel.send(levelUpEmbed)
  223.                
  224.                level.level += 1
  225.            }
  226.            bot.setlevel.run(level)
  227.  
  228.            // Role adding if user gets X level
  229.            // Roles
  230.            let lvlUpRole1 = message.guild.roles.find(role => role.name === "Beginner")
  231.            let lvlUpRole2 = message.guild.roles.find(role => role.name === "Experienced")
  232.            let lvlUpRole3 = message.guild.roles.find(role => role.name === "Specialist")
  233.            let lvlUpRole4 = message.guild.roles.find(role => role.name === "Veteran")
  234.            let lvlUpRole5 = message.guild.roles.find(role => role.name === "Legend")
  235.            let lvlUpRole6 = message.guild.roles.find(role => role.name === "Overpowered")
  236.  
  237.            // Role Adding
  238.            if(level.level == 5) message.member.addRole(lvlUpRole1)
  239.            else if(level.level == 10) message.member.addRole(lvlUpRole2)
  240.            else if(level.level == 15) message.member.addRole(lvlUpRole3)
  241.            else if(level.level == 20) message.member.addRole(lvlUpRole4)
  242.            else if(level.level == 25) message.member.addRole(lvlUpRole5)
  243.            else if(level.level == 30) message.member.addRole(lvlUpRole6)
  244.        }
  245.    } else if(message.content.startsWith(PREFIX)){
  246.        const command = args.shift().toLowerCase()
  247.  
  248.        if(command === "level" || command === "xp" || command === "rank"){
  249.            message.delete()
  250.  
  251.            if(!targetUser){
  252.                let progressXp = newMath.subtract(allxp, prevlvl)
  253.                let percentageDivide = newMath.divide(progressXp, sublvl)
  254.                let percentageMultiply = newMath.multiply(percentageDivide, 100)
  255.                let percentageResult = newMath.round(percentageMultiply)
  256.    
  257.                let percentageTotalDivide = newMath.divide(allxp, nextlvl)
  258.                let percentageTotalMultiply = newMath.multiply(percentageTotalDivide, 100)
  259.                let percentageTotalResult = newMath.round(percentageTotalMultiply)
  260.  
  261.                let difference = newMath.subtract(nextlvl, allxp)
  262.                let yourEmbed = new Discord.RichEmbed()
  263.                .setAuthor(user.tag)
  264.                .setColor(`#ff0000`)
  265.                .addField(`**Level**`, curlvl, true)
  266.                .addField(`**Text XP**`, curxp, true)
  267.                .addField(`**Total XP**`, allxp, true)
  268.                .addField(`**VoiceChat XP**`, level.vxp, true)
  269.                .addField(`**Level Progress**`, `${progressXp}/${sublvl} (${percentageResult}%)`, true)
  270.                .addField(`**Total Progress**`, `${allxp}/${nextlvl} (${percentageTotalResult}%)`, true)
  271.                .setFooter(`${difference} XP to next level up`)
  272.                .setThumbnail(user.avatarURL)
  273.                .setTimestamp()
  274.    
  275.                return message.channel.send(yourEmbed)
  276.            } else if(targetUser){
  277.                let tprogressXp = newMath.subtract(tallxp, tprevlvl)
  278.                let tpercentageDivide = newMath.divide(tprogressXp, tsublvl)
  279.                let tpercentageMultiply = newMath.multiply(tpercentageDivide, 100)
  280.                let tpercentageResult = newMath.round(tpercentageMultiply)
  281.    
  282.                let tpercentageTotalDivide = newMath.divide(tallxp, tnextlvl)
  283.                let tpercentageTotalMultiply = newMath.multiply(tpercentageTotalDivide, 100)
  284.                let tpercentageTotalResult = newMath.round(tpercentageTotalMultiply)
  285.  
  286.                let tdifference = newMath.subtract(tnextlvl, tallxp)
  287.                let targetEmbed = new Discord.RichEmbed()
  288.                .setAuthor(targetUser.tag)
  289.                .setColor(`#ff0000`)
  290.                .addField(`**Level**`, tcurlvl, true)
  291.                .addField(`**Text XP**`, tcurxp, true)
  292.                .addField(`**Total XP**`, tallxp, true)
  293.                .addField(`**VoiceChat XP**`, tlevel.vxp, true)
  294.                .addField(`**Level Progress**`, `${tprogressXp}/${tsublvl} (${tpercentageResult}%)`, true)
  295.                .addField(`**Total Progress**`, `${tallxp}/${tnextlvl} (${tpercentageTotalResult}%)`, true)
  296.                .setFooter(`${tdifference} XP to next level up`)
  297.                .setThumbnail(targetUser.avatarURL)
  298.                .setTimestamp()
  299.    
  300.                return message.channel.send(targetEmbed)
  301.            }
  302.        } else if(command === "give"){
  303.            message.delete()
  304.            
  305.            if(!message.member.hasPermission("ADMINISTRATOR")) return message.reply("You are not the boss of me, you can't do that!")
  306.            // if(message.author.id !== message.guild.ownerID) return message.reply("You are not the boss of me, you can't do that!")
  307.  
  308.            const user = message.mentions.users.first() || bot.users.get(args[0])
  309.            if(!user) return message.reply("You must mention someone or give their ID!")
  310.  
  311.            const xpToAdd = parseInt(args[1], 10)
  312.            if(!xpToAdd) return message.reply("You didn't tell me how many xp to give...")
  313.  
  314.            let userlevel = bot.getlevel.get(user.id, message.guild.id)
  315.  
  316.            if(!userlevel){
  317.                userlevel = {
  318.                    id: `${message.guild.id}-${user.id}`,
  319.                    user: user.id,
  320.                    guild: message.guild.id,
  321.                    xp: 0,
  322.                    vxp: 0,
  323.                    level: 1
  324.                }
  325.            }
  326.  
  327.            userlevel.xp += xpToAdd
  328.  
  329.            // Auto Level up system (COMING SOON)
  330.  
  331.            bot.setlevel.run(userlevel)
  332.  
  333.            return message.channel.send(`**${user.tag}** has **received** **${xpToAdd}** xp and now has **${userlevel.xp}** xp.`)
  334.        } else if(command === "remove"){
  335.            message.delete()
  336.            
  337.            if(!message.member.hasPermission("ADMINISTRATOR")) return message.reply("You are not the boss of me, you can't do that!")
  338.            // if(message.author.id !== message.guild.ownerID) return message.reply("You are not the boss of me, you can't do that!")
  339.  
  340.            const user = message.mentions.users.first() || bot.users.get(args[0])
  341.            if(!user) return message.reply("You must mention someone or give their ID!")
  342.  
  343.            const xpToRemove = parseInt(args[1], 10)
  344.            if(!xpToRemove) return message.reply("You didn't tell me how many xp to give...")
  345.  
  346.            let userlevel = bot.getlevel.get(user.id, message.guild.id)
  347.  
  348.            if(!userlevel){
  349.                userlevel = {
  350.                    id: `${message.guild.id}-${user.id}`,
  351.                    user: user.id,
  352.                    guild: message.guild.id,
  353.                    xp: 0,
  354.                    vxp: 0,
  355.                    level: 1
  356.                }
  357.            }
  358.  
  359.            userlevel.xp -= xpToRemove
  360.  
  361.            // Auto Level DOWN system xd (COMING SOON)
  362.  
  363.            bot.setlevel.run(userlevel)
  364.  
  365.            return message.channel.send(`**${user.tag}** has **lost** **${xpToRemove}** xp and now has **${userlevel.xp}** xp.`)
  366.        } else if(command === "leaderboard" || command === "top10" || command === "top"){
  367.            message.delete()
  368.  
  369.            const top10 = sql.prepare("SELECT * FROM levels WHERE guild = ? ORDER BY xp + vxp DESC LIMIT 10;").all(message.guild.id)
  370.  
  371.            const embed = new Discord.RichEmbed()
  372.                .setAuthor(`${message.guild.name} - Leaderboard`, message.guild.iconURL)
  373.                .setDescription("Our top 10 xp leaders!")
  374.                .setColor("#ff0000")
  375.                .setThumbnail(message.guild.iconURL)
  376.                .setTimestamp()
  377.  
  378.                var i = 1
  379.            for(const data of top10){
  380.                embed.addField(`**${i++}.** ` + bot.users.get(data.user).tag, `Total xp amount: **${data.xp + data.vxp}** (level ${data.level})\nChat xp: **${data.xp}** - Voice xp: **${data.vxp}**`)
  381.            }
  382.            return message.channel.send(embed)
  383.        }
  384.    }
  385. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top