simply-xp 1.3.5-beta-7 → 1.3.5
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 +56 -56
- package/index.d.ts +1 -0
- package/package.json +54 -53
- package/simplyxp.js +17 -15
- package/src/addLevel.js +7 -7
- package/src/addXP.js +13 -13
- package/src/charts.js +29 -29
- package/src/connect.js +6 -7
- package/src/create.js +5 -5
- package/src/fetch.js +5 -5
- package/src/leaderboard.js +9 -10
- package/src/lvlRole.js +4 -4
- package/src/models/level.js +6 -6
- package/src/models/lvlrole.js +4 -4
- package/src/rank.js +73 -73
- package/src/reset.js +5 -5
- package/src/roleSetup.js +23 -23
- package/src/setLevel.js +14 -42
- package/src/setXP.js +17 -45
- package/.eslintrc.json +0 -29
- package/.github/CODE_OF_CONDUCT.md +0 -128
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -31
- package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
- package/.github/SECURITY.md +0 -25
- package/.github/pull_request_template.md +0 -8
- package/.github/workflows/codeql-analysis.yml +0 -71
- package/.idea/discord.xml +0 -7
- package/.idea/misc.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/simply-xp.iml +0 -9
- package/.idea/vcs.xml +0 -6
package/src/lvlRole.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const levels = require(
|
|
2
|
-
const lrole = require(
|
|
1
|
+
const levels = require("../src/models/level.js");
|
|
2
|
+
const lrole = require("../src/models/lvlrole.js");
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @param {Discord.Message} message
|
|
@@ -26,7 +26,7 @@ async function lvlRole(message, userID, guildID) {
|
|
|
26
26
|
|
|
27
27
|
await newuser
|
|
28
28
|
.save()
|
|
29
|
-
.catch(() => console.log(
|
|
29
|
+
.catch(() => console.log("[XP] Failed to save new user to database"));
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
e.forEach((ee) => {
|
|
@@ -41,7 +41,7 @@ async function lvlRole(message, userID, guildID) {
|
|
|
41
41
|
else {
|
|
42
42
|
u.roles.add(real).catch(() => {
|
|
43
43
|
message.channel.send(
|
|
44
|
-
|
|
44
|
+
"[XP] ERROR: Role is higher than me. `MISSING_PERMISSIONS`"
|
|
45
45
|
);
|
|
46
46
|
});
|
|
47
47
|
}
|
package/src/models/level.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
const mongoose = require(
|
|
1
|
+
const mongoose = require("mongoose");
|
|
2
2
|
|
|
3
3
|
const Levelz = new mongoose.Schema({
|
|
4
|
-
user: {
|
|
5
|
-
guild: {
|
|
6
|
-
xp: {
|
|
7
|
-
level: {
|
|
4
|
+
user: {type: String, unique: true},
|
|
5
|
+
guild: {type: String},
|
|
6
|
+
xp: {type: Number, default: 0},
|
|
7
|
+
level: {type: Number, default: 0}
|
|
8
8
|
});
|
|
9
9
|
|
|
10
|
-
module.exports = mongoose.model(
|
|
10
|
+
module.exports = mongoose.model("Simply-XP", Levelz);
|
package/src/models/lvlrole.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
const mongoose = require(
|
|
1
|
+
const mongoose = require("mongoose");
|
|
2
2
|
|
|
3
3
|
const rol = new mongoose.Schema({
|
|
4
|
-
gid: {
|
|
5
|
-
lvlrole: {
|
|
4
|
+
gid: {type: String},
|
|
5
|
+
lvlrole: {type: Array}
|
|
6
6
|
});
|
|
7
7
|
|
|
8
|
-
module.exports = mongoose.model(
|
|
8
|
+
module.exports = mongoose.model("Simply-XP-LevelRole", rol);
|
package/src/rank.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
const levels = require(
|
|
2
|
-
const {join} = require(
|
|
1
|
+
const levels = require("../src/models/level.js");
|
|
2
|
+
const {join} = require("path");
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @param {Discord.Message} message
|
|
6
6
|
* @param {string} userID
|
|
7
7
|
* @param {string} guildID
|
|
8
|
-
* @param {import(
|
|
8
|
+
* @param {import("../index").rankOptions} options
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
async function rank(message, userID, guildID, options = []) {
|
|
12
|
-
if (!userID) throw new Error(
|
|
12
|
+
if (!userID) throw new Error("[XP] User ID was not provided.");
|
|
13
13
|
|
|
14
|
-
if (!guildID) throw new Error(
|
|
14
|
+
if (!guildID) throw new Error("[XP] Guild ID was not provided.");
|
|
15
15
|
|
|
16
16
|
const user = await levels.findOne({
|
|
17
17
|
user: userID,
|
|
18
18
|
guild: guildID
|
|
19
19
|
});
|
|
20
|
-
if (!user) throw new Error(
|
|
20
|
+
if (!user) throw new Error("[XP] NO_DATA | User has no XP data.");
|
|
21
21
|
|
|
22
22
|
const leaderboard = await levels
|
|
23
23
|
.find({
|
|
24
24
|
guild: guildID
|
|
25
25
|
})
|
|
26
|
-
.sort([[
|
|
26
|
+
.sort([["xp", "descending"]])
|
|
27
27
|
.exec();
|
|
28
28
|
|
|
29
29
|
user.position = leaderboard.findIndex((i) => i.user === userID) + 1;
|
|
@@ -46,49 +46,49 @@ async function rank(message, userID, guildID, options = []) {
|
|
|
46
46
|
|
|
47
47
|
async function rankCard(message, options = []) {
|
|
48
48
|
try {
|
|
49
|
-
const Canvas = require(
|
|
49
|
+
const Canvas = require("@napi-rs/canvas");
|
|
50
50
|
Canvas.GlobalFonts.registerFromPath(
|
|
51
|
-
join(__dirname,
|
|
52
|
-
|
|
51
|
+
join(__dirname, "Fonts", "Baloo-Regular.ttf"),
|
|
52
|
+
"Sans Serif"
|
|
53
53
|
);
|
|
54
54
|
|
|
55
55
|
const member = options.member;
|
|
56
56
|
|
|
57
57
|
const canvas = Canvas.createCanvas(1080, 400),
|
|
58
|
-
ctx = canvas.getContext(
|
|
58
|
+
ctx = canvas.getContext("2d");
|
|
59
59
|
|
|
60
60
|
const name = member.tag;
|
|
61
|
-
const noSymbols = (string) => string.replace(/[\u007f-\uffff]/g,
|
|
61
|
+
const noSymbols = (string) => string.replace(/[\u007f-\uffff]/g, "");
|
|
62
62
|
|
|
63
|
-
let fsiz =
|
|
63
|
+
let fsiz = "45px";
|
|
64
64
|
if (message.guild.name.length >= 23) {
|
|
65
|
-
fsiz =
|
|
65
|
+
fsiz = "38px";
|
|
66
66
|
}
|
|
67
67
|
if (message.guild.name.length >= 40) {
|
|
68
|
-
fsiz =
|
|
68
|
+
fsiz = "28px";
|
|
69
69
|
}
|
|
70
70
|
if (message.guild.name.length >= 63) {
|
|
71
|
-
fsiz =
|
|
71
|
+
fsiz = "22px";
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
let BackgroundRadius =
|
|
74
|
+
let BackgroundRadius = "20",
|
|
75
75
|
BackGroundImg =
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
AttachmentName =
|
|
79
|
-
AttachmentDesc =
|
|
76
|
+
options.background ||
|
|
77
|
+
"https://i.ibb.co/QQvMqf7/gradient.jpg",
|
|
78
|
+
AttachmentName = "rank.png",
|
|
79
|
+
AttachmentDesc = "Rank Card",
|
|
80
80
|
Username = noSymbols(name),
|
|
81
|
-
AvatarRoundRadius =
|
|
82
|
-
DrawLayerColor =
|
|
81
|
+
AvatarRoundRadius = "50",
|
|
82
|
+
DrawLayerColor = "#000000",
|
|
83
83
|
DrawLayerOpacity = 0.4,
|
|
84
|
-
BoxColor = options.color ||
|
|
85
|
-
LevelBarFill = options.lvlbar ||
|
|
86
|
-
LevelBarBackground = options.lvlbarBg ||
|
|
84
|
+
BoxColor = options.color || "#096DD1",
|
|
85
|
+
LevelBarFill = options.lvlbar || "#ffffff",
|
|
86
|
+
LevelBarBackground = options.lvlbarBg || "#ffffff",
|
|
87
87
|
Rank = options.rank,
|
|
88
|
-
TextEXP = shortener(options.currentXP) +
|
|
88
|
+
TextEXP = shortener(options.currentXP) + " XP",
|
|
89
89
|
LvlText = `Level ${shortener(options.level)}`,
|
|
90
|
-
BarRadius =
|
|
91
|
-
TextXpNeded =
|
|
90
|
+
BarRadius = "20",
|
|
91
|
+
TextXpNeded = "{current}/{needed}",
|
|
92
92
|
CurrentXP = options.currentXP,
|
|
93
93
|
NeededXP = options.neededXP;
|
|
94
94
|
|
|
@@ -110,7 +110,7 @@ async function rank(message, userID, guildID, options = []) {
|
|
|
110
110
|
ctx.quadraticCurveTo(0, 0, Number(BackgroundRadius), 0);
|
|
111
111
|
ctx.closePath();
|
|
112
112
|
ctx.clip();
|
|
113
|
-
ctx.fillStyle =
|
|
113
|
+
ctx.fillStyle = "#000000";
|
|
114
114
|
ctx.fillRect(0, 0, 1080, 400);
|
|
115
115
|
let background = await Canvas.loadImage(BackGroundImg);
|
|
116
116
|
ctx.globalAlpha = 0.7;
|
|
@@ -134,64 +134,64 @@ async function rank(message, userID, guildID, options = []) {
|
|
|
134
134
|
|
|
135
135
|
ctx.save();
|
|
136
136
|
RoundedBox(ctx, 70, 240 + 50 + 30, 180, 50, 20);
|
|
137
|
-
ctx.strokeStyle =
|
|
137
|
+
ctx.strokeStyle = "#BFC85A22";
|
|
138
138
|
ctx.stroke();
|
|
139
139
|
ctx.clip();
|
|
140
140
|
ctx.fillStyle = BoxColor;
|
|
141
141
|
ctx.globalAlpha = 1;
|
|
142
142
|
ctx.fillRect(70, 320, 180, 50);
|
|
143
143
|
ctx.globalAlpha = 1;
|
|
144
|
-
ctx.fillStyle =
|
|
145
|
-
ctx.font =
|
|
146
|
-
ctx.textAlign =
|
|
144
|
+
ctx.fillStyle = "#ffffff";
|
|
145
|
+
ctx.font = "32px \"Sans Serif\"";
|
|
146
|
+
ctx.textAlign = "center";
|
|
147
147
|
ctx.fillText(TextEXP, 160, 358);
|
|
148
148
|
ctx.restore();
|
|
149
149
|
|
|
150
150
|
ctx.save();
|
|
151
151
|
RoundedBox(ctx, 70, 240, 180, 50, 20);
|
|
152
|
-
ctx.strokeStyle =
|
|
152
|
+
ctx.strokeStyle = "#BFC85A22";
|
|
153
153
|
ctx.stroke();
|
|
154
154
|
ctx.clip();
|
|
155
155
|
ctx.fillStyle = BoxColor;
|
|
156
156
|
ctx.globalAlpha = 1;
|
|
157
157
|
ctx.fillRect(70, 240, 180, 50, 50);
|
|
158
158
|
ctx.globalAlpha = 1;
|
|
159
|
-
ctx.fillStyle =
|
|
160
|
-
ctx.font =
|
|
161
|
-
ctx.textAlign =
|
|
159
|
+
ctx.fillStyle = "#ffffff";
|
|
160
|
+
ctx.font = "32px \"Sans Serif\"";
|
|
161
|
+
ctx.textAlign = "center";
|
|
162
162
|
ctx.fillText(LvlText, 70 + 180 / 2, 278);
|
|
163
163
|
ctx.restore();
|
|
164
164
|
|
|
165
165
|
ctx.save();
|
|
166
|
-
ctx.textAlign =
|
|
167
|
-
ctx.fillStyle =
|
|
168
|
-
ctx.shadowColor =
|
|
166
|
+
ctx.textAlign = "left";
|
|
167
|
+
ctx.fillStyle = "#ffffff";
|
|
168
|
+
ctx.shadowColor = "#000000";
|
|
169
169
|
ctx.shadowBlur = 15;
|
|
170
170
|
ctx.shadowOffsetX = 1;
|
|
171
171
|
ctx.shadowOffsetY = 1;
|
|
172
|
-
ctx.font =
|
|
172
|
+
ctx.font = "39px \"Sans Serif\"";
|
|
173
173
|
ctx.fillText(Username, 390, 80);
|
|
174
174
|
ctx.restore();
|
|
175
175
|
|
|
176
176
|
ctx.save();
|
|
177
|
-
ctx.textAlign =
|
|
178
|
-
ctx.fillStyle =
|
|
179
|
-
ctx.shadowColor =
|
|
177
|
+
ctx.textAlign = "right";
|
|
178
|
+
ctx.fillStyle = "#ffffff";
|
|
179
|
+
ctx.shadowColor = "#000000";
|
|
180
180
|
ctx.shadowBlur = 15;
|
|
181
181
|
ctx.shadowOffsetX = 1;
|
|
182
182
|
ctx.shadowOffsetY = 1;
|
|
183
|
-
ctx.font =
|
|
184
|
-
ctx.fillText(
|
|
183
|
+
ctx.font = "55px \"Sans Serif\"";
|
|
184
|
+
ctx.fillText("#" + Rank, canvas.width - 55, 80);
|
|
185
185
|
ctx.restore();
|
|
186
186
|
|
|
187
187
|
ctx.save();
|
|
188
188
|
RoundedBox(ctx, 390, 305, 660, 70, Number(20));
|
|
189
|
-
ctx.strokeStyle =
|
|
189
|
+
ctx.strokeStyle = "#BFC85A22";
|
|
190
190
|
ctx.stroke();
|
|
191
191
|
ctx.clip();
|
|
192
|
-
ctx.fillStyle =
|
|
192
|
+
ctx.fillStyle = "#ffffff";
|
|
193
193
|
ctx.font = `${fsiz} "Sans Serif"`;
|
|
194
|
-
ctx.textAlign =
|
|
194
|
+
ctx.textAlign = "center";
|
|
195
195
|
ctx.fillText(message.guild.name, 720, 355);
|
|
196
196
|
ctx.globalAlpha = 0.2;
|
|
197
197
|
ctx.fillRect(390, 305, 660, 70);
|
|
@@ -199,7 +199,7 @@ async function rank(message, userID, guildID, options = []) {
|
|
|
199
199
|
|
|
200
200
|
ctx.save();
|
|
201
201
|
RoundedBox(ctx, 390, 145, 660, 50, Number(BarRadius));
|
|
202
|
-
ctx.strokeStyle =
|
|
202
|
+
ctx.strokeStyle = "#BFC85A22";
|
|
203
203
|
ctx.stroke();
|
|
204
204
|
ctx.clip();
|
|
205
205
|
ctx.fillStyle = LevelBarBackground;
|
|
@@ -212,7 +212,7 @@ async function rank(message, userID, guildID, options = []) {
|
|
|
212
212
|
|
|
213
213
|
ctx.save();
|
|
214
214
|
RoundedBox(ctx, 390, 145, progress, 50, Number(BarRadius));
|
|
215
|
-
ctx.strokeStyle =
|
|
215
|
+
ctx.strokeStyle = "#BFC85A22";
|
|
216
216
|
ctx.stroke();
|
|
217
217
|
ctx.clip();
|
|
218
218
|
ctx.fillStyle = LevelBarFill;
|
|
@@ -221,25 +221,25 @@ async function rank(message, userID, guildID, options = []) {
|
|
|
221
221
|
ctx.restore();
|
|
222
222
|
|
|
223
223
|
ctx.save();
|
|
224
|
-
ctx.textAlign =
|
|
225
|
-
ctx.fillStyle =
|
|
224
|
+
ctx.textAlign = "left";
|
|
225
|
+
ctx.fillStyle = "#ffffff";
|
|
226
226
|
ctx.globalAlpha = 0.8;
|
|
227
|
-
ctx.font =
|
|
228
|
-
ctx.fillText(
|
|
227
|
+
ctx.font = "30px \"Sans Serif\"";
|
|
228
|
+
ctx.fillText("Next Level: " + shortener(NeededXP) + " XP", 390, 230);
|
|
229
229
|
ctx.restore();
|
|
230
230
|
|
|
231
231
|
const latestXP = Number(CurrentXP) - Number(NeededXP);
|
|
232
232
|
const textXPEdited = TextXpNeded.replace(/{needed}/g, shortener(NeededXP).toString())
|
|
233
233
|
.replace(/{current}/g, shortener(CurrentXP).toString())
|
|
234
234
|
.replace(/{latest}/g, latestXP.toString());
|
|
235
|
-
ctx.textAlign =
|
|
236
|
-
ctx.fillStyle =
|
|
235
|
+
ctx.textAlign = "center";
|
|
236
|
+
ctx.fillStyle = "#474747";
|
|
237
237
|
ctx.globalAlpha = 1;
|
|
238
|
-
ctx.font =
|
|
238
|
+
ctx.font = "30px \"Sans Serif\"";
|
|
239
239
|
ctx.fillText(textXPEdited, 730, 180);
|
|
240
240
|
|
|
241
241
|
return {
|
|
242
|
-
attachment: canvas.toBuffer(
|
|
242
|
+
attachment: canvas.toBuffer("image/webp"),
|
|
243
243
|
description: AttachmentDesc,
|
|
244
244
|
name: AttachmentName
|
|
245
245
|
};
|
|
@@ -270,19 +270,19 @@ function RoundedBox(ctx, x, y, width, height, radius) {
|
|
|
270
270
|
|
|
271
271
|
function shortener(count) {
|
|
272
272
|
const COUNT_ABBRS = [
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
273
|
+
"",
|
|
274
|
+
"k",
|
|
275
|
+
"M",
|
|
276
|
+
"B",
|
|
277
|
+
"T",
|
|
278
|
+
"Q",
|
|
279
|
+
"Q+",
|
|
280
|
+
"S",
|
|
281
|
+
"S+",
|
|
282
|
+
"O",
|
|
283
|
+
"N",
|
|
284
|
+
"D",
|
|
285
|
+
"U"
|
|
286
286
|
];
|
|
287
287
|
|
|
288
288
|
const i = 0 === count ? count : Math.floor(Math.log(count) / Math.log(1000));
|
package/src/reset.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const levels = require(
|
|
1
|
+
const levels = require("../src/models/level.js");
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @param {string} userID
|
|
@@ -6,17 +6,17 @@ const levels = require('../src/models/level.js');
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
async function reset(userID, guildID) {
|
|
9
|
-
if (!userID) throw new Error(
|
|
9
|
+
if (!userID) throw new Error("[XP] User ID was not provided.");
|
|
10
10
|
|
|
11
|
-
if (!guildID) throw new Error(
|
|
11
|
+
if (!guildID) throw new Error("[XP] User ID was not provided.");
|
|
12
12
|
|
|
13
13
|
await levels
|
|
14
|
-
.findOneAndUpdate({
|
|
14
|
+
.findOneAndUpdate({user: userID, guild: guildID}, {xp: 0, level: 0})
|
|
15
15
|
.catch((err) => {
|
|
16
16
|
throw new Error(err);
|
|
17
17
|
});
|
|
18
18
|
|
|
19
|
-
return {
|
|
19
|
+
return {user: userID, guild: guildID, xp: 0, level: 0};
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
module.exports = reset;
|
package/src/roleSetup.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
const lrole = require(
|
|
1
|
+
const lrole = require("../src/models/lvlrole.js");
|
|
2
2
|
|
|
3
3
|
class roleSetup {
|
|
4
4
|
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
* @param {Discord.Client} client
|
|
6
|
+
* @param {string} guildID
|
|
7
|
+
* @param {import("../index").lvladdOptions} options
|
|
8
|
+
*/
|
|
9
9
|
|
|
10
10
|
static async add(client, guildID, options = []) {
|
|
11
11
|
let rol = await lrole.findOne({
|
|
@@ -21,7 +21,7 @@ class roleSetup {
|
|
|
21
21
|
let roll = g.roles.cache.find((r) => r.id === options.role);
|
|
22
22
|
|
|
23
23
|
if (roll) {
|
|
24
|
-
if (rol) throw new Error(
|
|
24
|
+
if (rol) throw new Error("Level Already Exist. Use delete");
|
|
25
25
|
else if (!rol) {
|
|
26
26
|
let newrol = await lrole.findOne({
|
|
27
27
|
gid: guildID
|
|
@@ -36,7 +36,7 @@ class roleSetup {
|
|
|
36
36
|
await newrol.save();
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
newrol.lvlrole.push({
|
|
39
|
+
newrol.lvlrole.push({lvl: options.level, role: options.role});
|
|
40
40
|
|
|
41
41
|
await newrol
|
|
42
42
|
.save()
|
|
@@ -48,17 +48,17 @@ class roleSetup {
|
|
|
48
48
|
}
|
|
49
49
|
} else {
|
|
50
50
|
throw new Error(
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
"Role ID is invalid. | " +
|
|
52
|
+
`Guild ID: ${guildID} | Role ID: ${options.role}`
|
|
53
53
|
);
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
58
|
+
* @param {Discord.Client} client
|
|
59
|
+
* @param {string} guildID
|
|
60
|
+
* @param {import("../index").lvlremoveOptions} options
|
|
61
|
+
*/
|
|
62
62
|
|
|
63
63
|
static async remove(client, guildID, options = []) {
|
|
64
64
|
let rol = await lrole.find({
|
|
@@ -66,7 +66,7 @@ class roleSetup {
|
|
|
66
66
|
});
|
|
67
67
|
|
|
68
68
|
if (!rol || rol.length === 0)
|
|
69
|
-
throw new Error(
|
|
69
|
+
throw new Error("Level role with this level does not exist");
|
|
70
70
|
rol = rol[0].lvlrole.find((item) => item.lvl === options.level) || undefined;
|
|
71
71
|
|
|
72
72
|
if (rol) {
|
|
@@ -75,18 +75,18 @@ class roleSetup {
|
|
|
75
75
|
gid: guildID
|
|
76
76
|
},
|
|
77
77
|
{
|
|
78
|
-
$pull: {
|
|
78
|
+
$pull: {lvlrole: {lvl: options.level}}
|
|
79
79
|
}
|
|
80
80
|
);
|
|
81
81
|
|
|
82
82
|
return true;
|
|
83
|
-
} else throw new Error(
|
|
83
|
+
} else throw new Error("Level role with this level does not exist");
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
/**
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
* @param {Discord.Client} client
|
|
88
|
+
* @param {string} guildID
|
|
89
|
+
*/
|
|
90
90
|
|
|
91
91
|
static async fetch(client, guildID) {
|
|
92
92
|
let rol = await lrole.find({
|
|
@@ -99,10 +99,10 @@ class roleSetup {
|
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
/**
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
102
|
+
* @param {Discord.Client} client
|
|
103
|
+
* @param {string} guildID
|
|
104
|
+
* @param {string} level
|
|
105
|
+
*/
|
|
106
106
|
|
|
107
107
|
static async find(client, guildID, level) {
|
|
108
108
|
let rol = await lrole.find({
|
package/src/setLevel.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const levels = require(
|
|
2
|
-
|
|
1
|
+
const levels = require("../src/models/level.js");
|
|
2
|
+
const {roleSetup} = require("../simplyxp");
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @param {Discord.Message} message
|
|
@@ -8,47 +8,19 @@ let { roleSetup } = require('../simplyxp');
|
|
|
8
8
|
* @param {string} level
|
|
9
9
|
*/
|
|
10
10
|
async function setLevel(message, userID, guildID, level) {
|
|
11
|
-
if (!userID) throw new Error(
|
|
11
|
+
if (!userID) throw new Error("[XP] User ID was not provided.");
|
|
12
|
+
if (!guildID) throw new Error("[XP] Guild ID was not provided.");
|
|
13
|
+
if (!level || isNaN(Number(level))) throw new Error("[XP] Invalid level amount.");
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
const {client} = message;
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
const user = await levels.findOneAndUpdate(
|
|
18
|
+
{user: userID, guild: guildID},
|
|
19
|
+
{xp: (level * 10) ** 2, level: Math.floor(0.1 * Math.sqrt((level * 10) ** 2))},
|
|
20
|
+
{upsert: true, new: true}
|
|
21
|
+
);
|
|
16
22
|
|
|
17
|
-
|
|
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(() => 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) {
|
|
23
|
+
if (user.level !== level) {
|
|
52
24
|
let data = {
|
|
53
25
|
xp: user.xp,
|
|
54
26
|
level: user.level,
|
|
@@ -58,7 +30,7 @@ async function setLevel(message, userID, guildID, level) {
|
|
|
58
30
|
|
|
59
31
|
let role = await roleSetup.find(client, guildID, level);
|
|
60
32
|
|
|
61
|
-
client.emit(
|
|
33
|
+
client.emit("levelUp", message, data, role);
|
|
62
34
|
}
|
|
63
35
|
|
|
64
36
|
return {
|
|
@@ -67,4 +39,4 @@ async function setLevel(message, userID, guildID, level) {
|
|
|
67
39
|
};
|
|
68
40
|
}
|
|
69
41
|
|
|
70
|
-
module.exports = setLevel;
|
|
42
|
+
module.exports = setLevel;
|
package/src/setXP.js
CHANGED
|
@@ -1,51 +1,23 @@
|
|
|
1
|
-
const levels = require(
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @param {string} userID
|
|
5
|
-
* @param {string} guildID
|
|
6
|
-
* @param {string} xp
|
|
7
|
-
*/
|
|
1
|
+
const levels = require("../src/models/level.js");
|
|
8
2
|
|
|
9
3
|
async function setXP(userID, guildID, xp) {
|
|
10
|
-
if (!userID) throw new Error(
|
|
11
|
-
|
|
12
|
-
if (!
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const newUser = new levels({
|
|
25
|
-
user: userID,
|
|
26
|
-
guild: guildID,
|
|
27
|
-
xp: xp,
|
|
28
|
-
level: lvl
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
await newUser
|
|
32
|
-
.save()
|
|
33
|
-
.catch(() => console.log('[XP] Failed to save new use to database'));
|
|
34
|
-
|
|
35
|
-
return {
|
|
36
|
-
xp: 0
|
|
37
|
-
};
|
|
4
|
+
if (!userID) throw new Error("[XP] User ID was not provided.");
|
|
5
|
+
if (!guildID) throw new Error("[XP] Guild ID was not provided.");
|
|
6
|
+
if (!xp || isNaN(Number(xp))) throw new Error("[XP] Invalid XP amount.");
|
|
7
|
+
|
|
8
|
+
const user = await levels.findOneAndUpdate(
|
|
9
|
+
{user: userID, guild: guildID},
|
|
10
|
+
{xp: xp},
|
|
11
|
+
{upsert: true}
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
const lvl = Math.floor(0.1 * Math.sqrt(xp));
|
|
15
|
+
if (user.level !== lvl) {
|
|
16
|
+
user.level = lvl;
|
|
17
|
+
await user.save().catch((e) => console.log(`[XP] Failed to set XP | User: ${userID} | Err: ${e}`));
|
|
38
18
|
}
|
|
39
|
-
user.xp = xp;
|
|
40
|
-
user.level = Math.floor(0.1 * Math.sqrt(user.xp));
|
|
41
|
-
|
|
42
|
-
await user
|
|
43
|
-
.save()
|
|
44
|
-
.catch((e) =>
|
|
45
|
-
console.log(`[XP] Failed to set XP | User: ${userID} | Err: ${e}`)
|
|
46
|
-
);
|
|
47
19
|
|
|
48
|
-
return {
|
|
20
|
+
return {xp};
|
|
49
21
|
}
|
|
50
22
|
|
|
51
|
-
module.exports = setXP;
|
|
23
|
+
module.exports = setXP;
|
package/.eslintrc.json
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"env": {
|
|
3
|
-
"commonjs": true,
|
|
4
|
-
"es2021": true,
|
|
5
|
-
"node": true
|
|
6
|
-
},
|
|
7
|
-
"extends": "eslint:recommended",
|
|
8
|
-
"parserOptions": {
|
|
9
|
-
"ecmaVersion": "latest"
|
|
10
|
-
},
|
|
11
|
-
"rules": {
|
|
12
|
-
"indent": [
|
|
13
|
-
"error",
|
|
14
|
-
"tab"
|
|
15
|
-
],
|
|
16
|
-
"linebreak-style": [
|
|
17
|
-
"error",
|
|
18
|
-
"unix"
|
|
19
|
-
],
|
|
20
|
-
"quotes": [
|
|
21
|
-
"error",
|
|
22
|
-
"single"
|
|
23
|
-
],
|
|
24
|
-
"semi": [
|
|
25
|
-
"error",
|
|
26
|
-
"always"
|
|
27
|
-
]
|
|
28
|
-
}
|
|
29
|
-
}
|