Create Level System For Discord Bot Easily 1

Create Level System For Discord Bot Easily

Hi guys, Welcome back to the brand-new post of mine. Today I will show you how you can Create Level System For Discord Bot Easily. Lets get directly to the point.

How To Create Level System For Discord Bot Easily

Create Level System For Discord Bot Easily

First of all we will create class in different .js file, Given below class is created by me and my friend

const db = require("quick.db")

class Util {
  static getLevel(xp, extra = false) {
  let level = 0;
    
    //WHILE LOOP
    while(xp >= Util.getLevelxp(level)) {
      xp -= Util.getLevelxp(level);
      level++
    }
    if(extra) return [level, xp];
    else return level;
    }

  
  static getLevelxp(level) {
    return 5 * Math.pow(level, 2) + 50 * level + 100;
  }
  
  static getInfo(exp) {
    let [level, remxp] = Util.getLevel(exp, true);
    let levelxp = Util.getLevelxp(level);
    
    return { level, remxp, levelxp}
  }
  
  static addexp(message) {
    let toadd = Math.floor(Math.random() * 3 + 3);
    let oldxp = db.get(`xp_${message.author.id}_${message.guild.id}`)
    let oldlvl = Util.getLevel(oldxp)
    let newxp = oldxp = toadd;
    let newlvl = Util.getLevel(newxp);
    
    
    if(newlvl > oldlvl) 
    message.channel.send(`${message.author}, You just reached level ${newlvl}`)
    db.add(`xp_${message.author.id}_${message.guild.id}`, toadd)
  }
}

module.exports = Util;

We will be using it for our level system, I suggest you to look up codes properly and try to understand.

Now go to main file, in my case my main file is server.js and add exp to author data by using addexp(message) and you need to define addexp also

const { addexp } = require("./handlers/xp.js")

client.on("message", async message => {
if(message.author.bot) return;
  if(!message.guild) return;
  
return addexp(message)
})

Now lets go and create level command for bot, first we will be defining user

 const user = message.mentions.users.first() || message.author;

After defining user we will make sure that author does not mention its own bot

   if(user.id === client.user.id) { //IF BOT
      return message.channel.send("😉 | I am on level 100")
    }

Now we will make sure that author does not mention any bot because we can’t let bot have xp

 if(user.bot) {
      return message.channel.send("Bot do not have levels")
    }

Well it’s time to define XP and we are using quick.db for storing XP for every user per server and before that don’t forget to define getInfo and quick.db

 let xp = db.get(`xp_${user.id}_${message.guild.id}`) || 0;
    
    const {level, remxp, levelxp} = getInfo(xp);

We also make sure if user have 0 XP bot will send alert message.

if(xp === 0) return message.channel.send(`**${user.tag}** is out of the xp`)

Now we will send embed with all level details

let embed = new discord.MessageEmbed()
    .setAuthor(user.username, message.guild.iconURL())
    .setColor("#ff2050")
    .setThumbnail(user.avatarURL())
    .setDescription(`**LEVEL** - ${level}
**XP** - ${remxp}/${levelxp}`)
    
 message.channel.send(embed)   

You are done here and ready to use the bot

Create Level System For Discord Bot Easily 2

Result Code

Here is the result code of whole repo – HERE

Also Read: Creating Pokedex Command for Discord Bot Using API and discord.js v12

One Comment

Leave a Reply
  1. very cool & great job …but does it calculate xp based on members activity on voice channels & text or just text channel ?

Leave a Reply

Your email address will not be published. Required fields are marked *