text: let levelling = require('../lib/levelling')
  const canvacord = require('canvacord')
  
  let handler = async (m, { conn, usedPrefix }) => {
    let pp = './src/avatar_contact.png'
    let who = m.sender
    let discriminator = who.substring(9, 13)
    try {
      pp = await conn.getProfilePicture(who)
    } catch (e) {
    } finally {
      let user = global.db.data.users[m.sender]
      let users = Object.entries(global.db.data.users).map(([key, value]) => {
        return { ...value, jid: key }
      })
      let sortedLevel = users.map(toNumber('level')).sort(sort('level'))
      let usersLevel = sortedLevel.map(enumGetKey)
      let { min, xp, max } = levelling.xpRange(user.level, global.multiplier)
      if (!levelling.canLevelUp(user.level, user.exp, global.multiplier)) {
        let rank = await new canvacord.Rank()
          .setRank(usersLevel.indexOf(m.sender) + 1)
          .setAvatar(pp)
          .setLevel(user.level)
          .setCurrentXP(user.exp - min)
          .setRequiredXP(xp)
          .setProgressBar("#f2aa4c", "COLOR")
          .setUsername(conn.getName(who))
          .setDiscriminator(discriminator)
        rank.build()
          .then(async data => {
            await conn.sendButtonImg(m.chat, `Level *${user.level} (${user.exp - min}/${xp})*\nKurang *${max - user.exp}* lagi!`.trim(), data, '© Erick', 'AUTO LEVEL UP', `${usedPrefix}on autolevelup`, { height: 282, width: 934 })
          })
      }
      let before = user.level * 1
      while (levelling.canLevelUp(user.level, user.exp, global.multiplier)) user.level++
      if (before !== user.level) {
        let rank = await new canvacord.Rank()
          .setRank(usersLevel.indexOf(m.sender) + 1)
          .setAvatar(pp)
          .setLevel(user.level)
          .setCurrentXP(user.exp - min)
          .setRequiredXP(xp)
          .setProgressBar("#f2aa4c", "COLOR")
          .setUsername(conn.getName(who))
          .setDiscriminator(discriminator)
        rank.build()
          .then(async data => {
            await conn.sendButtonImg(m.chat, `_*Level Up!*_\n_${before}_ -> _${user.level}_`.trim(), data, '© Erick', 'AUTO LEVEL UP', `${usedPrefix}on autolevelup`, { height: 282, width: 934 })
          })
      }
    }
  }
  
  handler.help = ['levelup']
  handler.tags = ['xp']
  
  handler.command = /^levelup$/i
  
  module.exports = handler
  
  function sort(property, ascending = true) {
    if (property) return (...args) => args[ascending & 1][property] - args[!ascending & 1][property]
    else return (...args) => args[ascending & 1] - args[!ascending & 1]
  }
  
  function toNumber(property, _default = 0) {
    if (property) return (a, i, b) => {
      return { ...b[i], [property]: a[property] === undefined ? _default : a[property] }
    }
    else return a => a === undefined ? _default : a
  }
  
  function enumGetKey(a) {
    return a.jid
  }