simply-xp 1.1.5-beta → 1.1.7
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/.github/ISSUE_TEMPLATE/bug_report.md +31 -31
- package/.prettierrc.json +8 -8
- package/README.md +62 -62
- package/index.d.ts +107 -107
- package/package.json +52 -52
- package/src/Fonts/Baloo-Regular.ttf +0 -0
- package/src/addLevel.js +71 -71
- package/src/addXP.js +112 -112
- package/src/charts.js +83 -83
- package/src/connect.js +20 -20
- package/src/create.js +28 -28
- package/src/fetch.js +74 -74
- package/src/leaderboard.js +70 -70
- package/src/lvlRole.js +54 -54
- package/src/models/level.js +10 -10
- package/src/models/lvlrole.js +8 -8
- package/src/rank.js +282 -285
- package/src/reset.js +28 -28
- package/src/roleSetup.js +122 -124
- package/src/setLevel.js +71 -71
- package/src/setXP.js +51 -51
- package/src/Fonts/Poppins-Regular.ttf +0 -0
- package/src/Fonts/Poppins-SemiBold.ttf +0 -0
package/src/addXP.js
CHANGED
|
@@ -1,112 +1,112 @@
|
|
|
1
|
-
let Discord = require('discord.js')
|
|
2
|
-
const levels = require('../src/models/level.js')
|
|
3
|
-
let { roleSetup } = require('../simplyxp')
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @param {Discord.Message} message
|
|
7
|
-
* @param {string} userID
|
|
8
|
-
* @param {string} guildID
|
|
9
|
-
* @param {number} xp
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
async function addXP(message, userID, guildID, xp) {
|
|
13
|
-
if (!userID) throw new Error('[XP] User ID was not provided.')
|
|
14
|
-
|
|
15
|
-
if (!guildID) throw new Error('[XP] Guild ID was not provided.')
|
|
16
|
-
|
|
17
|
-
if (!xp) throw new Error('[XP] XP amount is not provided.')
|
|
18
|
-
|
|
19
|
-
let { client } = message
|
|
20
|
-
|
|
21
|
-
let min
|
|
22
|
-
let max
|
|
23
|
-
if (xp.min) {
|
|
24
|
-
if (!xp.max)
|
|
25
|
-
throw new Error(
|
|
26
|
-
'[XP] XP min amount is provided but max amount is not provided.'
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
min = Number(xp.min)
|
|
30
|
-
|
|
31
|
-
if (Number(xp.min).toString() === 'NaN')
|
|
32
|
-
throw new Error('[XP] XP amount (min) is not a number.')
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (xp.max) {
|
|
36
|
-
if (!xp.min)
|
|
37
|
-
throw new Error(
|
|
38
|
-
'[XP] XP max amount is provided but min amount is not provided.'
|
|
39
|
-
)
|
|
40
|
-
|
|
41
|
-
max = Number(xp.max)
|
|
42
|
-
|
|
43
|
-
if (Number(xp.max).toString() === 'NaN')
|
|
44
|
-
throw new Error('[XP] XP amount (max) is not a number.')
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (xp.min && xp.max) {
|
|
48
|
-
let randomNumber = Math.floor(Math.random() * (max - min) + min)
|
|
49
|
-
|
|
50
|
-
xp = randomNumber
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const user = await levels.findOne({ user: userID, guild: guildID })
|
|
54
|
-
|
|
55
|
-
let lvl = Math.floor(0.1 * Math.sqrt(xp))
|
|
56
|
-
|
|
57
|
-
if (!user) {
|
|
58
|
-
const newUser = new levels({
|
|
59
|
-
user: userID,
|
|
60
|
-
guild: guildID,
|
|
61
|
-
xp: xp,
|
|
62
|
-
level: lvl
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
await newUser
|
|
66
|
-
.save()
|
|
67
|
-
.catch((e) => console.log(`[XP] Failed to save new user to database`))
|
|
68
|
-
|
|
69
|
-
return {
|
|
70
|
-
level: 0,
|
|
71
|
-
exp: 0
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
let level1 = user.level
|
|
75
|
-
|
|
76
|
-
user.xp += parseInt(xp, 10)
|
|
77
|
-
user.level = Math.floor(0.1 * Math.sqrt(user.xp))
|
|
78
|
-
|
|
79
|
-
await user
|
|
80
|
-
.save()
|
|
81
|
-
.catch((e) =>
|
|
82
|
-
console.log(`[XP] Failed to add XP | User: ${userID} | Err: ${e}`)
|
|
83
|
-
)
|
|
84
|
-
|
|
85
|
-
let level = user.level
|
|
86
|
-
|
|
87
|
-
xp = user.xp
|
|
88
|
-
|
|
89
|
-
if (user.xp === 0 || Math.sign(user.xp) === -1) {
|
|
90
|
-
xp = 0
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (level1 !== level) {
|
|
94
|
-
let data = {
|
|
95
|
-
xp,
|
|
96
|
-
level,
|
|
97
|
-
userID,
|
|
98
|
-
guildID
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
let role = await roleSetup.find(client, guildID, level)
|
|
102
|
-
|
|
103
|
-
client.emit('levelUp', message, data, role)
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return {
|
|
107
|
-
level,
|
|
108
|
-
xp
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
module.exports = addXP
|
|
1
|
+
let Discord = require('discord.js')
|
|
2
|
+
const levels = require('../src/models/level.js')
|
|
3
|
+
let { roleSetup } = require('../simplyxp')
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param {Discord.Message} message
|
|
7
|
+
* @param {string} userID
|
|
8
|
+
* @param {string} guildID
|
|
9
|
+
* @param {number} xp
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
async function addXP(message, userID, guildID, xp) {
|
|
13
|
+
if (!userID) throw new Error('[XP] User ID was not provided.')
|
|
14
|
+
|
|
15
|
+
if (!guildID) throw new Error('[XP] Guild ID was not provided.')
|
|
16
|
+
|
|
17
|
+
if (!xp) throw new Error('[XP] XP amount is not provided.')
|
|
18
|
+
|
|
19
|
+
let { client } = message
|
|
20
|
+
|
|
21
|
+
let min
|
|
22
|
+
let max
|
|
23
|
+
if (xp.min) {
|
|
24
|
+
if (!xp.max)
|
|
25
|
+
throw new Error(
|
|
26
|
+
'[XP] XP min amount is provided but max amount is not provided.'
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
min = Number(xp.min)
|
|
30
|
+
|
|
31
|
+
if (Number(xp.min).toString() === 'NaN')
|
|
32
|
+
throw new Error('[XP] XP amount (min) is not a number.')
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (xp.max) {
|
|
36
|
+
if (!xp.min)
|
|
37
|
+
throw new Error(
|
|
38
|
+
'[XP] XP max amount is provided but min amount is not provided.'
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
max = Number(xp.max)
|
|
42
|
+
|
|
43
|
+
if (Number(xp.max).toString() === 'NaN')
|
|
44
|
+
throw new Error('[XP] XP amount (max) is not a number.')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (xp.min && xp.max) {
|
|
48
|
+
let randomNumber = Math.floor(Math.random() * (max - min) + min)
|
|
49
|
+
|
|
50
|
+
xp = randomNumber
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const user = await levels.findOne({ user: userID, guild: guildID })
|
|
54
|
+
|
|
55
|
+
let lvl = Math.floor(0.1 * Math.sqrt(xp))
|
|
56
|
+
|
|
57
|
+
if (!user) {
|
|
58
|
+
const newUser = new levels({
|
|
59
|
+
user: userID,
|
|
60
|
+
guild: guildID,
|
|
61
|
+
xp: xp,
|
|
62
|
+
level: lvl
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
await newUser
|
|
66
|
+
.save()
|
|
67
|
+
.catch((e) => console.log(`[XP] Failed to save new user to database`))
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
level: 0,
|
|
71
|
+
exp: 0
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
let level1 = user.level
|
|
75
|
+
|
|
76
|
+
user.xp += parseInt(xp, 10)
|
|
77
|
+
user.level = Math.floor(0.1 * Math.sqrt(user.xp))
|
|
78
|
+
|
|
79
|
+
await user
|
|
80
|
+
.save()
|
|
81
|
+
.catch((e) =>
|
|
82
|
+
console.log(`[XP] Failed to add XP | User: ${userID} | Err: ${e}`)
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
let level = user.level
|
|
86
|
+
|
|
87
|
+
xp = user.xp
|
|
88
|
+
|
|
89
|
+
if (user.xp === 0 || Math.sign(user.xp) === -1) {
|
|
90
|
+
xp = 0
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (level1 !== level) {
|
|
94
|
+
let data = {
|
|
95
|
+
xp,
|
|
96
|
+
level,
|
|
97
|
+
userID,
|
|
98
|
+
guildID
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
let role = await roleSetup.find(client, guildID, level)
|
|
102
|
+
|
|
103
|
+
client.emit('levelUp', message, data, role)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
level,
|
|
108
|
+
xp
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
module.exports = addXP
|
package/src/charts.js
CHANGED
|
@@ -1,83 +1,83 @@
|
|
|
1
|
-
let Discord = require('discord.js')
|
|
2
|
-
let leaderboard = require('./leaderboard')
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @param {Discord.Message} message
|
|
6
|
-
* @param {import('../index').chartsOptions} options
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
async function charts(message, options = []) {
|
|
10
|
-
let { client } = message
|
|
11
|
-
const ChartJSImage = require('chart.js-image')
|
|
12
|
-
|
|
13
|
-
let data = []
|
|
14
|
-
let uzern = []
|
|
15
|
-
|
|
16
|
-
await leaderboard(client, message.guild.id).then((e) => {
|
|
17
|
-
e.forEach((m) => {
|
|
18
|
-
if (m.position <= 5) {
|
|
19
|
-
data.push(m.xp)
|
|
20
|
-
uzern.push(m.tag)
|
|
21
|
-
}
|
|
22
|
-
})
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
const line_chart = ChartJSImage()
|
|
26
|
-
.chart({
|
|
27
|
-
type: options.type || 'bar',
|
|
28
|
-
data: {
|
|
29
|
-
labels: uzern,
|
|
30
|
-
datasets: [
|
|
31
|
-
{
|
|
32
|
-
label: 'Leaderboards',
|
|
33
|
-
data: data,
|
|
34
|
-
backgroundColor: [
|
|
35
|
-
'rgba(255, 99, 132, 0.5)',
|
|
36
|
-
'rgba(255, 159, 64, 0.5)',
|
|
37
|
-
'rgba(255, 205, 86, 0.5)',
|
|
38
|
-
'rgba(75, 192, 192, 0.5)',
|
|
39
|
-
'rgba(54, 162, 235, 0.5)',
|
|
40
|
-
'rgba(153, 102, 255, 0.5)',
|
|
41
|
-
'rgb(201, 203, 207, 0.5)'
|
|
42
|
-
],
|
|
43
|
-
borderColor: [
|
|
44
|
-
'rgb(255, 99, 132)',
|
|
45
|
-
'rgb(255, 159, 64)',
|
|
46
|
-
'rgb(255, 205, 86)',
|
|
47
|
-
'rgb(75, 192, 192)',
|
|
48
|
-
'rgb(54, 162, 235)',
|
|
49
|
-
'rgb(153, 102, 255)',
|
|
50
|
-
'rgb(201, 203, 207)'
|
|
51
|
-
],
|
|
52
|
-
borderWidth: 2
|
|
53
|
-
}
|
|
54
|
-
]
|
|
55
|
-
},
|
|
56
|
-
options: {
|
|
57
|
-
plugins: {
|
|
58
|
-
legend: {
|
|
59
|
-
labels: {
|
|
60
|
-
font: {
|
|
61
|
-
family: 'Courier New'
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
},
|
|
66
|
-
title: {
|
|
67
|
-
display: true,
|
|
68
|
-
text: 'XP Datasheet'
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
})
|
|
72
|
-
.backgroundColor(options.background || '#2F3136')
|
|
73
|
-
.width(940) // 500px
|
|
74
|
-
.height(520) // 300px
|
|
75
|
-
|
|
76
|
-
const attachment = new Discord.MessageAttachment(
|
|
77
|
-
line_chart.toURL(),
|
|
78
|
-
`chart.png`
|
|
79
|
-
)
|
|
80
|
-
return attachment
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
module.exports = charts
|
|
1
|
+
let Discord = require('discord.js')
|
|
2
|
+
let leaderboard = require('./leaderboard')
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {Discord.Message} message
|
|
6
|
+
* @param {import('../index').chartsOptions} options
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
async function charts(message, options = []) {
|
|
10
|
+
let { client } = message
|
|
11
|
+
const ChartJSImage = require('chart.js-image')
|
|
12
|
+
|
|
13
|
+
let data = []
|
|
14
|
+
let uzern = []
|
|
15
|
+
|
|
16
|
+
await leaderboard(client, message.guild.id).then((e) => {
|
|
17
|
+
e.forEach((m) => {
|
|
18
|
+
if (m.position <= 5) {
|
|
19
|
+
data.push(m.xp)
|
|
20
|
+
uzern.push(m.tag)
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
const line_chart = ChartJSImage()
|
|
26
|
+
.chart({
|
|
27
|
+
type: options.type || 'bar',
|
|
28
|
+
data: {
|
|
29
|
+
labels: uzern,
|
|
30
|
+
datasets: [
|
|
31
|
+
{
|
|
32
|
+
label: 'Leaderboards',
|
|
33
|
+
data: data,
|
|
34
|
+
backgroundColor: [
|
|
35
|
+
'rgba(255, 99, 132, 0.5)',
|
|
36
|
+
'rgba(255, 159, 64, 0.5)',
|
|
37
|
+
'rgba(255, 205, 86, 0.5)',
|
|
38
|
+
'rgba(75, 192, 192, 0.5)',
|
|
39
|
+
'rgba(54, 162, 235, 0.5)',
|
|
40
|
+
'rgba(153, 102, 255, 0.5)',
|
|
41
|
+
'rgb(201, 203, 207, 0.5)'
|
|
42
|
+
],
|
|
43
|
+
borderColor: [
|
|
44
|
+
'rgb(255, 99, 132)',
|
|
45
|
+
'rgb(255, 159, 64)',
|
|
46
|
+
'rgb(255, 205, 86)',
|
|
47
|
+
'rgb(75, 192, 192)',
|
|
48
|
+
'rgb(54, 162, 235)',
|
|
49
|
+
'rgb(153, 102, 255)',
|
|
50
|
+
'rgb(201, 203, 207)'
|
|
51
|
+
],
|
|
52
|
+
borderWidth: 2
|
|
53
|
+
}
|
|
54
|
+
]
|
|
55
|
+
},
|
|
56
|
+
options: {
|
|
57
|
+
plugins: {
|
|
58
|
+
legend: {
|
|
59
|
+
labels: {
|
|
60
|
+
font: {
|
|
61
|
+
family: 'Courier New'
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
title: {
|
|
67
|
+
display: true,
|
|
68
|
+
text: 'XP Datasheet'
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
.backgroundColor(options.background || '#2F3136')
|
|
73
|
+
.width(940) // 500px
|
|
74
|
+
.height(520) // 300px
|
|
75
|
+
|
|
76
|
+
const attachment = new Discord.MessageAttachment(
|
|
77
|
+
line_chart.toURL(),
|
|
78
|
+
`chart.png`
|
|
79
|
+
)
|
|
80
|
+
return attachment
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
module.exports = charts
|
package/src/connect.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
const mongoose = require('mongoose')
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @param {string} db
|
|
5
|
-
* @param {import('../index').connectOptions} options
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
async function connect(db, options = []) {
|
|
9
|
-
if (!db) throw new Error('[XP] Database URL was not provided')
|
|
10
|
-
|
|
11
|
-
mongoose.connect(db, {
|
|
12
|
-
useNewUrlParser: true,
|
|
13
|
-
useUnifiedTopology: true
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
if (options.notify === false) return
|
|
17
|
-
else return console.log('{ XP } Database Connected')
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
module.exports = connect
|
|
1
|
+
const mongoose = require('mongoose')
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {string} db
|
|
5
|
+
* @param {import('../index').connectOptions} options
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
async function connect(db, options = []) {
|
|
9
|
+
if (!db) throw new Error('[XP] Database URL was not provided')
|
|
10
|
+
|
|
11
|
+
mongoose.connect(db, {
|
|
12
|
+
useNewUrlParser: true,
|
|
13
|
+
useUnifiedTopology: true
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
if (options.notify === false) return
|
|
17
|
+
else return console.log('{ XP } Database Connected')
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = connect
|
package/src/create.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
const levels = require('../src/models/level.js')
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @param {string} userID
|
|
5
|
-
* @param {string} guildID
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
async function create(userID, guildID) {
|
|
9
|
-
if (!userID) throw new Error('[XP] User ID was not provided.')
|
|
10
|
-
|
|
11
|
-
if (!guildID) throw new Error('[XP] User ID was not provided.')
|
|
12
|
-
|
|
13
|
-
let uzer = await levels.findOne({ user: userID, guild: guildID })
|
|
14
|
-
|
|
15
|
-
if (uzer) return
|
|
16
|
-
|
|
17
|
-
const newuser = new levels({
|
|
18
|
-
user: userID,
|
|
19
|
-
guild: guildID
|
|
20
|
-
})
|
|
21
|
-
await newuser
|
|
22
|
-
.save()
|
|
23
|
-
.catch((e) => console.log(`[XP] Failed to save new use to database`))
|
|
24
|
-
|
|
25
|
-
return true
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
module.exports = create
|
|
1
|
+
const levels = require('../src/models/level.js')
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {string} userID
|
|
5
|
+
* @param {string} guildID
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
async function create(userID, guildID) {
|
|
9
|
+
if (!userID) throw new Error('[XP] User ID was not provided.')
|
|
10
|
+
|
|
11
|
+
if (!guildID) throw new Error('[XP] User ID was not provided.')
|
|
12
|
+
|
|
13
|
+
let uzer = await levels.findOne({ user: userID, guild: guildID })
|
|
14
|
+
|
|
15
|
+
if (uzer) return
|
|
16
|
+
|
|
17
|
+
const newuser = new levels({
|
|
18
|
+
user: userID,
|
|
19
|
+
guild: guildID
|
|
20
|
+
})
|
|
21
|
+
await newuser
|
|
22
|
+
.save()
|
|
23
|
+
.catch((e) => console.log(`[XP] Failed to save new use to database`))
|
|
24
|
+
|
|
25
|
+
return true
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
module.exports = create
|
package/src/fetch.js
CHANGED
|
@@ -1,74 +1,74 @@
|
|
|
1
|
-
const levels = require('../src/models/level.js')
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @param {string} userID
|
|
5
|
-
* @param {string} guildID
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
async function fetch(userID, guildID) {
|
|
9
|
-
if (!userID) throw new Error('[XP] User ID was not provided.')
|
|
10
|
-
|
|
11
|
-
if (!guildID) throw new Error('[XP] Guild ID was not provided.')
|
|
12
|
-
|
|
13
|
-
let user = await levels.findOne({
|
|
14
|
-
user: userID,
|
|
15
|
-
guild: guildID
|
|
16
|
-
})
|
|
17
|
-
if (!user) {
|
|
18
|
-
user = new levels({
|
|
19
|
-
user: userID,
|
|
20
|
-
guild: guildID,
|
|
21
|
-
xp: 0,
|
|
22
|
-
level: 0
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
await user.save()
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const leaderboard = await levels
|
|
29
|
-
.find({
|
|
30
|
-
guild: guildID
|
|
31
|
-
})
|
|
32
|
-
.sort([['xp', 'descending']])
|
|
33
|
-
.exec()
|
|
34
|
-
|
|
35
|
-
if (user === null)
|
|
36
|
-
return {
|
|
37
|
-
level: 0,
|
|
38
|
-
xp: 0,
|
|
39
|
-
reqxp: 100,
|
|
40
|
-
rank: leaderboard.findIndex((i) => i.user === userID) + 1,
|
|
41
|
-
shortxp: 0,
|
|
42
|
-
shortreq: 100
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
user.position = leaderboard.findIndex((i) => i.user === userID) + 1
|
|
46
|
-
|
|
47
|
-
let targetxp = user.level + 1
|
|
48
|
-
|
|
49
|
-
let target = targetxp * targetxp * 100
|
|
50
|
-
|
|
51
|
-
function shortener(count) {
|
|
52
|
-
const COUNT_ABBRS = ['', 'k', 'M', 'T']
|
|
53
|
-
|
|
54
|
-
const i = 0 === count ? count : Math.floor(Math.log(count) / Math.log(1000))
|
|
55
|
-
let result = parseFloat((count / Math.pow(1000, i)).toFixed(2))
|
|
56
|
-
result += `${COUNT_ABBRS[i]}`
|
|
57
|
-
return result
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
let shortXP = shortener(user.xp)
|
|
61
|
-
|
|
62
|
-
let shortReqXP = shortener(target)
|
|
63
|
-
|
|
64
|
-
return {
|
|
65
|
-
level: user.level,
|
|
66
|
-
xp: user.xp,
|
|
67
|
-
reqxp: target,
|
|
68
|
-
rank: user.position,
|
|
69
|
-
shortxp: shortXP,
|
|
70
|
-
shortreq: shortReqXP
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
module.exports = fetch
|
|
1
|
+
const levels = require('../src/models/level.js')
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {string} userID
|
|
5
|
+
* @param {string} guildID
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
async function fetch(userID, guildID) {
|
|
9
|
+
if (!userID) throw new Error('[XP] User ID was not provided.')
|
|
10
|
+
|
|
11
|
+
if (!guildID) throw new Error('[XP] Guild ID was not provided.')
|
|
12
|
+
|
|
13
|
+
let user = await levels.findOne({
|
|
14
|
+
user: userID,
|
|
15
|
+
guild: guildID
|
|
16
|
+
})
|
|
17
|
+
if (!user) {
|
|
18
|
+
user = new levels({
|
|
19
|
+
user: userID,
|
|
20
|
+
guild: guildID,
|
|
21
|
+
xp: 0,
|
|
22
|
+
level: 0
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
await user.save()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const leaderboard = await levels
|
|
29
|
+
.find({
|
|
30
|
+
guild: guildID
|
|
31
|
+
})
|
|
32
|
+
.sort([['xp', 'descending']])
|
|
33
|
+
.exec()
|
|
34
|
+
|
|
35
|
+
if (user === null)
|
|
36
|
+
return {
|
|
37
|
+
level: 0,
|
|
38
|
+
xp: 0,
|
|
39
|
+
reqxp: 100,
|
|
40
|
+
rank: leaderboard.findIndex((i) => i.user === userID) + 1,
|
|
41
|
+
shortxp: 0,
|
|
42
|
+
shortreq: 100
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
user.position = leaderboard.findIndex((i) => i.user === userID) + 1
|
|
46
|
+
|
|
47
|
+
let targetxp = user.level + 1
|
|
48
|
+
|
|
49
|
+
let target = targetxp * targetxp * 100
|
|
50
|
+
|
|
51
|
+
function shortener(count) {
|
|
52
|
+
const COUNT_ABBRS = ['', 'k', 'M', 'T']
|
|
53
|
+
|
|
54
|
+
const i = 0 === count ? count : Math.floor(Math.log(count) / Math.log(1000))
|
|
55
|
+
let result = parseFloat((count / Math.pow(1000, i)).toFixed(2))
|
|
56
|
+
result += `${COUNT_ABBRS[i]}`
|
|
57
|
+
return result
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let shortXP = shortener(user.xp)
|
|
61
|
+
|
|
62
|
+
let shortReqXP = shortener(target)
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
level: user.level,
|
|
66
|
+
xp: user.xp,
|
|
67
|
+
reqxp: target,
|
|
68
|
+
rank: user.position,
|
|
69
|
+
shortxp: shortXP,
|
|
70
|
+
shortreq: shortReqXP
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
module.exports = fetch
|