-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
/
Copy pathecon-leaderboard.js
105 lines (93 loc) Β· 3.52 KB
/
econ-leaderboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { areJidsSameUser } from '@whiskeysockets/baileys'
import { createHash } from 'crypto'
import PhoneNumber from 'awesome-phonenumber'
import { canLevelUp, xpRange } from '../lib/levelling.js'
let handler = async (m, { conn, args, usedPrefix, participants }) => {
let users = Object.entries(global.db.data.users).map(([key, value]) => {
return { ...value, jid: key }
})
let who = m.quoted
? m.quoted.sender
: m.mentionedJid && m.mentionedJid[0]
? m.mentionedJid[0]
: m.fromMe
? conn.user.jid
: m.sender
let user = global.db.data.users[who]
if (!(who in global.db.data.users)) throw 'β³οΈ The user is not found in my database'
let pp = await conn.profilePictureUrl(who, 'image').catch(_ => './Guru.jpg')
let about = (await conn.fetchStatus(who).catch(console.error))?.status || ''
let { name, exp, credit, lastclaim, registered, regTime, age, level, role, warn } =
global.db.data.users[who]
let { min, xp, max } = xpRange(user.level, global.multiplier)
let username = conn.getName(who)
let math = max - xp
let prem = global.prems.includes(who.split('@')[0])
let sn = createHash('md5').update(who).digest('hex')
let totalgold
totalgold = Object.entries(global.db.data.users).map(([key, value]) => {
const user = { ...value, jid: key }
user.tg = user.credit + user.bank
return user.tg
})
let sortedExp = users.map(toNumber('exp')).sort(sort('exp'))
let sortedLim = users.map(toNumber('credit')).sort(sort('credit'))
let sortedLevel = users.map(toNumber('level')).sort(sort('level'))
let sortedBank = users.map(toNumber('bank')).sort(sort('bank'))
let sortedRank = users.map(toNumber('role')).sort(sort('role'))
let usersExp = sortedExp.map(enumGetKey)
let usersLim = sortedLim.map(enumGetKey)
let usersLevel = sortedLevel.map(enumGetKey)
let usersBank = sortedBank.map(enumGetKey)
let usersRank = sortedRank.map(enumGetKey)
let len =
args[0] && args[0].length > 0
? Math.min(50, Math.max(parseInt(args[0]), 5))
: Math.min(10, sortedExp.length)
let text = `
π *GLOBAL LEADERBOARD* π
${sortedExp
.slice(0, len)
.map(({ jid, exp, credit, level, bank, role }, i) => {
let totalgold = users.find(u => u.jid === jid).credit + users.find(u => u.jid === jid).bank
let user = global.db.data.users[jid]
let username = user.name
return `*#${i + 1}.*
*π Username:* ${username}
*π Experience:* ${exp}
*π Rank:* ${role}
*β¨ Level:* ${level}
*π Wallet:* ${credit}
*π¦ Bank:* ${bank}
*π° Gold:* ${totalgold}`
})
.join('\n\n\n')}
*You are at ${usersExp.indexOf(m.sender) + 1} out of total ${usersExp.length} members*`.trim()
conn.reply(m.chat, text, m, {
mentions: [
...usersExp.slice(0, len),
...usersLevel.slice(0, len),
...usersLim.slice(0, len),
...usersBank.slice(0, len),
...usersRank.slice(0, len),
].filter(v => !participants.some(p => areJidsSameUser(v, p.id))),
})
}
handler.help = ['leaderboard']
handler.tags = ['core']
handler.command = ['leaderboard', 'lb']
export default 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
}