simply-xp 1.2.0 → 1.3.0-dev-1
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 +55 -55
- package/package.json +2 -2
- package/simplyxp.js +3 -1
- package/src/addLevel.js +70 -71
- package/src/addXP.js +111 -112
- package/src/charts.js +83 -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/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
|