simply-xp 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/package.json +4 -4
- package/simplyxp.js +1 -1
- package/src/addLevel.js +70 -71
- package/src/addXP.js +111 -112
- package/src/charts.js +88 -83
- package/src/leaderboard.js +69 -70
- package/src/lvlRole.js +53 -54
- package/src/rank.js +4 -6
- package/src/roleSetup.js +121 -122
- package/src/setLevel.js +70 -71
package/README.md
CHANGED
|
@@ -30,8 +30,10 @@ yarn add simply-xp
|
|
|
30
30
|
|
|
31
31
|
# 🎉 What's New 🎉
|
|
32
32
|
|
|
33
|
-
- We have patched
|
|
34
|
-
-
|
|
33
|
+
- We have patched various issues
|
|
34
|
+
- Switched to the true **Chart.js** package!
|
|
35
|
+
- You no longer need to use D.JS for this package, alternatives can work!
|
|
36
|
+
- We have also updated our website guide! We hope you find it useful 😊
|
|
35
37
|
|
|
36
38
|
# But Why ?
|
|
37
39
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "simply-xp",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "A Simple, Easy and Beginner Friendly XP System",
|
|
5
5
|
"main": "simplyxp.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -41,14 +41,14 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"canvas": "^2.9.3",
|
|
44
|
-
"chart.js
|
|
44
|
+
"chart.js": "^3.8.0",
|
|
45
45
|
"mongoose": "^6.0.15"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"husky": "^7.0.
|
|
48
|
+
"husky": "^7.0.4",
|
|
49
49
|
"pretty-quick": "^3.1.1"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
|
-
"discord.js": "
|
|
52
|
+
"discord.js": "*"
|
|
53
53
|
}
|
|
54
54
|
}
|
package/simplyxp.js
CHANGED
package/src/addLevel.js
CHANGED
|
@@ -1,71 +1,70 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* @param {
|
|
7
|
-
* @param {string}
|
|
8
|
-
* @param {
|
|
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
|
-
user.xp
|
|
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
|
-
module.exports = addLevel
|
|
1
|
+
const levels = require('../src/models/level.js')
|
|
2
|
+
let { roleSetup } = require('../simplyxp')
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {Discord.Message} message
|
|
6
|
+
* @param {string} userID
|
|
7
|
+
* @param {string} guildID
|
|
8
|
+
* @param {number} level
|
|
9
|
+
*/
|
|
10
|
+
async function addLevel(message, userID, guildID, level) {
|
|
11
|
+
if (!userID) throw new Error('[XP] User ID was not provided.')
|
|
12
|
+
|
|
13
|
+
if (!guildID) throw new Error('[XP] Guild ID was not provided.')
|
|
14
|
+
|
|
15
|
+
if (!level) throw new Error('[XP] Level amount is not provided.')
|
|
16
|
+
|
|
17
|
+
let { client } = message
|
|
18
|
+
|
|
19
|
+
const user = await levels.findOne({ user: userID, guild: guildID })
|
|
20
|
+
|
|
21
|
+
if (!user) {
|
|
22
|
+
const newUser = new levels({
|
|
23
|
+
user: userID,
|
|
24
|
+
guild: guildID,
|
|
25
|
+
xp: 0,
|
|
26
|
+
level: 0
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
await newUser
|
|
30
|
+
.save()
|
|
31
|
+
.catch((e) => console.log(`[XP] Failed to save new user to database`))
|
|
32
|
+
|
|
33
|
+
let xp = (level * 10) ** 2
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
level: level,
|
|
37
|
+
exp: xp
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
let level1 = user.level
|
|
41
|
+
|
|
42
|
+
user.level += 1
|
|
43
|
+
user.xp = (user.level * 10) ** 2
|
|
44
|
+
|
|
45
|
+
await user
|
|
46
|
+
.save()
|
|
47
|
+
.catch((e) =>
|
|
48
|
+
console.log(`[XP] Failed to add Level | User: ${userID} | Err: ${e}`)
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
if (level1 !== level) {
|
|
52
|
+
let data = {
|
|
53
|
+
xp: user.xp,
|
|
54
|
+
level: user.level,
|
|
55
|
+
userID,
|
|
56
|
+
guildID
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let role = await roleSetup.find(client, guildID, level)
|
|
60
|
+
|
|
61
|
+
client.emit('levelUp', message, data, role)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
level: user.level,
|
|
66
|
+
xp: user.xp
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
module.exports = addLevel
|
package/src/addXP.js
CHANGED
|
@@ -1,112 +1,111 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* @param {
|
|
7
|
-
* @param {string}
|
|
8
|
-
* @param {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
let
|
|
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
|
-
user.
|
|
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
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
module.exports = addXP
|
|
1
|
+
const levels = require('../src/models/level.js')
|
|
2
|
+
let { roleSetup } = require('../simplyxp')
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {Discord.Message} message
|
|
6
|
+
* @param {string} userID
|
|
7
|
+
* @param {string} guildID
|
|
8
|
+
* @param {number} xp
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
async function addXP(message, userID, guildID, xp) {
|
|
12
|
+
if (!userID) throw new Error('[XP] User ID was not provided.')
|
|
13
|
+
|
|
14
|
+
if (!guildID) throw new Error('[XP] Guild ID was not provided.')
|
|
15
|
+
|
|
16
|
+
if (!xp) throw new Error('[XP] XP amount is not provided.')
|
|
17
|
+
|
|
18
|
+
let { client } = message
|
|
19
|
+
|
|
20
|
+
let min
|
|
21
|
+
let max
|
|
22
|
+
if (xp.min) {
|
|
23
|
+
if (!xp.max)
|
|
24
|
+
throw new Error(
|
|
25
|
+
'[XP] XP min amount is provided but max amount is not provided.'
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
min = Number(xp.min)
|
|
29
|
+
|
|
30
|
+
if (Number(xp.min).toString() === 'NaN')
|
|
31
|
+
throw new Error('[XP] XP amount (min) is not a number.')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (xp.max) {
|
|
35
|
+
if (!xp.min)
|
|
36
|
+
throw new Error(
|
|
37
|
+
'[XP] XP max amount is provided but min amount is not provided.'
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
max = Number(xp.max)
|
|
41
|
+
|
|
42
|
+
if (Number(xp.max).toString() === 'NaN')
|
|
43
|
+
throw new Error('[XP] XP amount (max) is not a number.')
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (xp.min && xp.max) {
|
|
47
|
+
let randomNumber = Math.floor(Math.random() * (max - min) + min)
|
|
48
|
+
|
|
49
|
+
xp = randomNumber
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const user = await levels.findOne({ user: userID, guild: guildID })
|
|
53
|
+
|
|
54
|
+
let lvl = Math.floor(0.1 * Math.sqrt(xp))
|
|
55
|
+
|
|
56
|
+
if (!user) {
|
|
57
|
+
const newUser = new levels({
|
|
58
|
+
user: userID,
|
|
59
|
+
guild: guildID,
|
|
60
|
+
xp: xp,
|
|
61
|
+
level: lvl
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
await newUser
|
|
65
|
+
.save()
|
|
66
|
+
.catch((e) => console.log(`[XP] Failed to save new user to database`))
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
level: 0,
|
|
70
|
+
exp: 0
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
let level1 = user.level
|
|
74
|
+
|
|
75
|
+
user.xp += parseInt(xp, 10)
|
|
76
|
+
user.level = Math.floor(0.1 * Math.sqrt(user.xp))
|
|
77
|
+
|
|
78
|
+
await user
|
|
79
|
+
.save()
|
|
80
|
+
.catch((e) =>
|
|
81
|
+
console.log(`[XP] Failed to add XP | User: ${userID} | Err: ${e}`)
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
let level = user.level
|
|
85
|
+
|
|
86
|
+
xp = user.xp
|
|
87
|
+
|
|
88
|
+
if (user.xp === 0 || Math.sign(user.xp) === -1) {
|
|
89
|
+
xp = 0
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (level1 !== level) {
|
|
93
|
+
let data = {
|
|
94
|
+
xp,
|
|
95
|
+
level,
|
|
96
|
+
userID,
|
|
97
|
+
guildID
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let role = await roleSetup.find(client, guildID, level)
|
|
101
|
+
|
|
102
|
+
client.emit('levelUp', message, data, role)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
level,
|
|
107
|
+
xp
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
module.exports = addXP
|
package/src/charts.js
CHANGED
|
@@ -1,83 +1,88 @@
|
|
|
1
|
-
let
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* @param {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
let data = []
|
|
14
|
-
let
|
|
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
|
-
|
|
1
|
+
let leaderboard = require('./leaderboard')
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {Discord.Message} message
|
|
5
|
+
* @param {import('../index').chartsOptions} options
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
async function charts(message, options = []) {
|
|
9
|
+
let { client } = message
|
|
10
|
+
const ChartJS = require('chart.js')
|
|
11
|
+
const Canvas = require('canvas')
|
|
12
|
+
|
|
13
|
+
let data = []
|
|
14
|
+
let pos = options?.position || 5
|
|
15
|
+
let uzern = []
|
|
16
|
+
|
|
17
|
+
let ctx = Canvas.createCanvas(950, 526)
|
|
18
|
+
await leaderboard(client, message.guild.id).then((e) => {
|
|
19
|
+
e.forEach((m) => {
|
|
20
|
+
if (m.position <= pos) {
|
|
21
|
+
data.push(m.xp)
|
|
22
|
+
uzern.push(m.tag)
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
new ChartJS(ctx, {
|
|
28
|
+
type: options.type || 'bar',
|
|
29
|
+
data: {
|
|
30
|
+
labels: uzern,
|
|
31
|
+
datasets: [
|
|
32
|
+
{
|
|
33
|
+
label: 'Leaderboards',
|
|
34
|
+
data: data,
|
|
35
|
+
backgroundColor: [
|
|
36
|
+
'rgba(255, 99, 132, 0.5)',
|
|
37
|
+
'rgba(255, 159, 64, 0.5)',
|
|
38
|
+
'rgba(255, 205, 86, 0.5)',
|
|
39
|
+
'rgba(75, 192, 192, 0.5)',
|
|
40
|
+
'rgba(54, 162, 235, 0.5)',
|
|
41
|
+
'rgba(153, 102, 255, 0.5)',
|
|
42
|
+
'rgb(201, 203, 207, 0.5)'
|
|
43
|
+
],
|
|
44
|
+
borderColor: [
|
|
45
|
+
'rgb(255, 99, 132)',
|
|
46
|
+
'rgb(255, 159, 64)',
|
|
47
|
+
'rgb(255, 205, 86)',
|
|
48
|
+
'rgb(75, 192, 192)',
|
|
49
|
+
'rgb(54, 162, 235)',
|
|
50
|
+
'rgb(153, 102, 255)',
|
|
51
|
+
'rgb(201, 203, 207)'
|
|
52
|
+
],
|
|
53
|
+
borderWidth: 2
|
|
54
|
+
}
|
|
55
|
+
]
|
|
56
|
+
},
|
|
57
|
+
options: {
|
|
58
|
+
animation: false,
|
|
59
|
+
plugins: {
|
|
60
|
+
title: {
|
|
61
|
+
display: true,
|
|
62
|
+
text: 'XP Datasheet'
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
plugins: [
|
|
67
|
+
{
|
|
68
|
+
id: 'simply-xp',
|
|
69
|
+
beforeDraw: (chart) => {
|
|
70
|
+
const ctx = chart.canvas.getContext('2d')
|
|
71
|
+
ctx.save()
|
|
72
|
+
ctx.globalCompositeOperation = 'destination-over'
|
|
73
|
+
ctx.fillStyle = options.background || '#2F3136'
|
|
74
|
+
ctx.fillRect(0, 0, chart.width, chart.height)
|
|
75
|
+
ctx.restore()
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
]
|
|
79
|
+
}).update()
|
|
80
|
+
|
|
81
|
+
const attachment = {
|
|
82
|
+
attachment: ctx.toBuffer(),
|
|
83
|
+
name: 'chart.png'
|
|
84
|
+
}
|
|
85
|
+
return attachment
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
module.exports = charts
|
package/src/leaderboard.js
CHANGED
|
@@ -1,70 +1,69 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* @param {
|
|
6
|
-
* @param {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
.
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
result
|
|
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
|
-
module.exports = leaderboard
|
|
1
|
+
const levels = require('../src/models/level.js')
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {Discord.Client} client
|
|
5
|
+
* @param {string} guildID
|
|
6
|
+
* @param {number} limit
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
async function leaderboard(client, guildID, limit) {
|
|
10
|
+
if (!guildID) throw new Error('[XP] Guild ID was not provided.')
|
|
11
|
+
|
|
12
|
+
let g = client.guilds.cache.get(guildID)
|
|
13
|
+
|
|
14
|
+
let leaderboard = await levels
|
|
15
|
+
.find({
|
|
16
|
+
guild: guildID
|
|
17
|
+
})
|
|
18
|
+
.sort([['xp', 'descending']])
|
|
19
|
+
.exec()
|
|
20
|
+
|
|
21
|
+
let led = []
|
|
22
|
+
|
|
23
|
+
function shortener(count) {
|
|
24
|
+
const COUNT_ABBRS = ['', 'k', 'M', 'T']
|
|
25
|
+
|
|
26
|
+
const i = 0 === count ? count : Math.floor(Math.log(count) / Math.log(1000))
|
|
27
|
+
let result = parseFloat((count / Math.pow(1000, i)).toFixed(2))
|
|
28
|
+
result += `${COUNT_ABBRS[i]}`
|
|
29
|
+
return result
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
leaderboard.map((key) => {
|
|
33
|
+
let user = g.members.cache.get(key.user)
|
|
34
|
+
if (key.xp === 0) return
|
|
35
|
+
|
|
36
|
+
let pos =
|
|
37
|
+
leaderboard.findIndex(
|
|
38
|
+
(i) => i.guild === key.guild && i.user === key.user
|
|
39
|
+
) + 1
|
|
40
|
+
|
|
41
|
+
if (limit) {
|
|
42
|
+
if (pos > Number(limit)) return
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let shortXP = shortener(key.xp)
|
|
46
|
+
|
|
47
|
+
if (!user) return
|
|
48
|
+
|
|
49
|
+
led.push({
|
|
50
|
+
guildID: key.guild,
|
|
51
|
+
userID: key.user,
|
|
52
|
+
xp: key.xp,
|
|
53
|
+
shortxp: shortXP,
|
|
54
|
+
level: key.level,
|
|
55
|
+
position: pos,
|
|
56
|
+
username: user.user.username,
|
|
57
|
+
tag: user.user.tag
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
led = led.filter(
|
|
62
|
+
(thing, index, self) =>
|
|
63
|
+
index === self.findIndex((t) => t.userID === thing.userID)
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
return led
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports = leaderboard
|
package/src/lvlRole.js
CHANGED
|
@@ -1,54 +1,53 @@
|
|
|
1
|
-
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* @param {
|
|
7
|
-
* @param {string}
|
|
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
|
-
module.exports = lvlRole
|
|
1
|
+
const levels = require('../src/models/level.js')
|
|
2
|
+
const lrole = require('../src/models/lvlrole.js')
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {Discord.Message} message
|
|
6
|
+
* @param {string} userID
|
|
7
|
+
* @param {string} guildID
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
async function lvlRole(message, userID, guildID) {
|
|
11
|
+
let e = await lrole.find({
|
|
12
|
+
gid: guildID
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
if (!e) return
|
|
16
|
+
|
|
17
|
+
let user = await levels.findOne({
|
|
18
|
+
user: userID,
|
|
19
|
+
guild: guildID
|
|
20
|
+
})
|
|
21
|
+
if (!user) {
|
|
22
|
+
const newuser = new levels({
|
|
23
|
+
user: userID,
|
|
24
|
+
guild: guildID
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
await newuser
|
|
28
|
+
.save()
|
|
29
|
+
.catch((e) => console.log(`[XP] Failed to save new user to database`))
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
e.forEach((ee) => {
|
|
33
|
+
ee = ee.lvlrole
|
|
34
|
+
|
|
35
|
+
ee.forEach((xd) => {
|
|
36
|
+
if (user && user.level >= Number(xd.lvl)) {
|
|
37
|
+
let u = message.guild.members.cache.get(userID)
|
|
38
|
+
|
|
39
|
+
let real = message.guild.roles.cache.find((r) => r.id === xd.role)
|
|
40
|
+
if (!real) return
|
|
41
|
+
else {
|
|
42
|
+
u.roles.add(real).catch((err) => {
|
|
43
|
+
message.channel.send(
|
|
44
|
+
'[XP] ERROR: Role is higher than me. `MISSING_PERMISSIONS`'
|
|
45
|
+
)
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
module.exports = lvlRole
|
package/src/rank.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
let Discord = require('discord.js')
|
|
2
1
|
const levels = require('../src/models/level.js')
|
|
3
2
|
const { join } = require('path')
|
|
4
3
|
|
|
@@ -283,11 +282,10 @@ async function rank(message, userID, guildID, options = []) {
|
|
|
283
282
|
ctx.font = '30px "Sans Serif"'
|
|
284
283
|
ctx.fillText(textXPEdited, 730, 180)
|
|
285
284
|
|
|
286
|
-
const attachment =
|
|
287
|
-
canvas.toBuffer(),
|
|
288
|
-
AttachmentName
|
|
289
|
-
|
|
290
|
-
|
|
285
|
+
const attachment = {
|
|
286
|
+
attachment: canvas.toBuffer(),
|
|
287
|
+
name: AttachmentName
|
|
288
|
+
}
|
|
291
289
|
return attachment
|
|
292
290
|
} catch (err) {
|
|
293
291
|
console.log(`[XP] Error Occured. | rankCard | Error: ${err.stack}`)
|
package/src/roleSetup.js
CHANGED
|
@@ -1,122 +1,121 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* @param {
|
|
7
|
-
* @param {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (rol)
|
|
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
|
-
* @param {
|
|
60
|
-
* @param {
|
|
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
|
-
* @param {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
* @param {
|
|
104
|
-
* @param {string}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
module.exports = roleSetup
|
|
1
|
+
const lrole = require('../src/models/lvlrole.js')
|
|
2
|
+
|
|
3
|
+
class roleSetup {
|
|
4
|
+
/**
|
|
5
|
+
* @param {Discord.Client} client
|
|
6
|
+
* @param {string} guildID
|
|
7
|
+
* @param {import('../index').lvladdOptions} options
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
static async add(client, guildID, options = []) {
|
|
11
|
+
let rol = await lrole.findOne({
|
|
12
|
+
gid: guildID,
|
|
13
|
+
lvlrole: {
|
|
14
|
+
lvl: options.level,
|
|
15
|
+
role: options.role
|
|
16
|
+
}
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
let g = client.guilds.cache.get(guildID)
|
|
20
|
+
|
|
21
|
+
let roll = g.roles.cache.find((r) => r.id === options.role)
|
|
22
|
+
|
|
23
|
+
if (roll) {
|
|
24
|
+
if (rol) throw new Error('Level Already Exist. Use delete')
|
|
25
|
+
else if (!rol) {
|
|
26
|
+
let newrol = await lrole.findOne({
|
|
27
|
+
gid: guildID
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
if (!newrol) {
|
|
31
|
+
newrol = new lrole({
|
|
32
|
+
gid: guildID,
|
|
33
|
+
lvlrole: []
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
await newrol.save()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
newrol.lvlrole.push({ lvl: options.level, role: options.role })
|
|
40
|
+
|
|
41
|
+
await newrol
|
|
42
|
+
.save()
|
|
43
|
+
.catch((e) =>
|
|
44
|
+
console.log(`[XP] Failed to add lvlrole to database | ${e}`)
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
return true
|
|
48
|
+
}
|
|
49
|
+
} else {
|
|
50
|
+
throw new Error(
|
|
51
|
+
'Role ID is invalid. | ' +
|
|
52
|
+
`Guild ID: ${guildID} | Role ID: ${options.role}`
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @param {Discord.Client} client
|
|
59
|
+
* @param {string} guildID
|
|
60
|
+
* @param {import('../index').lvlremoveOptions} options
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
static async remove(client, guildID, options = []) {
|
|
64
|
+
let rol = await lrole.find({
|
|
65
|
+
gid: guildID
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
if (!rol || rol.length === 0)
|
|
69
|
+
throw new Error('Level role with this level does not exist')
|
|
70
|
+
rol = rol[0].lvlrole.find((item) => item.lvl === options.level) || undefined
|
|
71
|
+
|
|
72
|
+
if (rol) {
|
|
73
|
+
let newrol = await lrole.findOneAndUpdate(
|
|
74
|
+
{
|
|
75
|
+
gid: guildID
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
$pull: { lvlrole: { lvl: options.level } }
|
|
79
|
+
}
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
return true
|
|
83
|
+
} else throw new Error('Level role with this level does not exist')
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {Discord.Client} client
|
|
88
|
+
* @param {string} guildID
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
static async fetch(client, guildID) {
|
|
92
|
+
let rol = await lrole.find({
|
|
93
|
+
gid: guildID
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
if (!rol || rol.length === 0) return
|
|
97
|
+
|
|
98
|
+
return rol[0].lvlrole
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @param {Discord.Client} client
|
|
103
|
+
* @param {string} guildID
|
|
104
|
+
* @param {string} level
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
static async find(client, guildID, level) {
|
|
108
|
+
let rol = await lrole.find({
|
|
109
|
+
gid: guildID
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
if (!rol || rol.length === 0) return
|
|
113
|
+
rol = rol[0].lvlrole.find((item) => item.lvl === level) || undefined
|
|
114
|
+
|
|
115
|
+
if (rol) {
|
|
116
|
+
return rol
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
module.exports = roleSetup
|
package/src/setLevel.js
CHANGED
|
@@ -1,71 +1,70 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* @param {
|
|
7
|
-
* @param {string}
|
|
8
|
-
* @param {string}
|
|
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
|
-
user.
|
|
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
|
-
module.exports = setLevel
|
|
1
|
+
const levels = require('../src/models/level.js')
|
|
2
|
+
let { roleSetup } = require('../simplyxp')
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {Discord.Message} message
|
|
6
|
+
* @param {string} userID
|
|
7
|
+
* @param {string} guildID
|
|
8
|
+
* @param {string} level
|
|
9
|
+
*/
|
|
10
|
+
async function setLevel(message, userID, guildID, level) {
|
|
11
|
+
if (!userID) throw new Error('[XP] User ID was not provided.')
|
|
12
|
+
|
|
13
|
+
if (!guildID) throw new Error('[XP] Guild ID was not provided.')
|
|
14
|
+
|
|
15
|
+
if (!level) throw new Error('[XP] Level amount is not provided.')
|
|
16
|
+
|
|
17
|
+
let { client } = message
|
|
18
|
+
|
|
19
|
+
const user = await levels.findOne({ user: userID, guild: guildID })
|
|
20
|
+
|
|
21
|
+
if (!user) {
|
|
22
|
+
const newUser = new levels({
|
|
23
|
+
user: userID,
|
|
24
|
+
guild: guildID,
|
|
25
|
+
xp: 0,
|
|
26
|
+
level: 0
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
await newUser
|
|
30
|
+
.save()
|
|
31
|
+
.catch((e) => console.log(`[XP] Failed to save new user to database`))
|
|
32
|
+
|
|
33
|
+
let xp = (level * 10) ** 2
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
level: level,
|
|
37
|
+
exp: xp
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
let level1 = user.level
|
|
41
|
+
|
|
42
|
+
user.xp = (level * 10) ** 2
|
|
43
|
+
user.level = Math.floor(0.1 * Math.sqrt(user.xp))
|
|
44
|
+
|
|
45
|
+
await user
|
|
46
|
+
.save()
|
|
47
|
+
.catch((e) =>
|
|
48
|
+
console.log(`[XP] Failed to set Level | User: ${userID} | Err: ${e}`)
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
if (level1 !== level) {
|
|
52
|
+
let data = {
|
|
53
|
+
xp: user.xp,
|
|
54
|
+
level: user.level,
|
|
55
|
+
userID,
|
|
56
|
+
guildID
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let role = await roleSetup.find(client, guildID, level)
|
|
60
|
+
|
|
61
|
+
client.emit('levelUp', message, data, role)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
level: user.level,
|
|
66
|
+
xp: user.xp
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
module.exports = setLevel
|