simply-xp 1.3.7 → 2.0.0-beta.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.
Files changed (55) hide show
  1. package/FUNDING.yml +2 -2
  2. package/README.md +73 -43
  3. package/lib/src/add.d.ts +35 -0
  4. package/lib/src/add.js +23 -0
  5. package/lib/src/cards.d.ts +158 -0
  6. package/lib/src/cards.js +35 -0
  7. package/lib/src/charts.d.ts +27 -0
  8. package/lib/src/charts.js +10 -0
  9. package/lib/src/connect.d.ts +29 -0
  10. package/lib/src/connect.js +41 -0
  11. package/lib/src/create.d.ts +12 -0
  12. package/lib/src/create.js +11 -0
  13. package/lib/src/fetch.d.ts +12 -0
  14. package/lib/src/fetch.js +11 -0
  15. package/lib/src/functions/database.d.ts +164 -0
  16. package/lib/src/functions/database.js +78 -0
  17. package/lib/src/functions/https.d.ts +26 -0
  18. package/lib/src/functions/https.js +15 -0
  19. package/lib/src/functions/utilities.d.ts +79 -0
  20. package/lib/src/functions/utilities.js +43 -0
  21. package/lib/src/functions/xplogs.d.ts +81 -0
  22. package/lib/src/functions/xplogs.js +15 -0
  23. package/lib/src/leaderboard.d.ts +11 -0
  24. package/lib/src/leaderboard.js +10 -0
  25. package/lib/src/migrate.d.ts +27 -0
  26. package/lib/src/migrate.js +19 -0
  27. package/lib/src/remove.d.ts +29 -0
  28. package/lib/src/remove.js +23 -0
  29. package/lib/src/reset.d.ts +12 -0
  30. package/lib/src/reset.js +12 -0
  31. package/lib/src/roleSetup.d.ts +78 -0
  32. package/lib/src/roleSetup.js +47 -0
  33. package/lib/src/set.d.ts +26 -0
  34. package/lib/src/set.js +23 -0
  35. package/lib/xp.d.ts +62 -0
  36. package/lib/xp.js +56 -0
  37. package/package.json +49 -35
  38. package/index.d.ts +0 -108
  39. package/simplyxp.js +0 -33
  40. package/src/Fonts/Baloo-Regular.ttf +0 -0
  41. package/src/addLevel.js +0 -66
  42. package/src/addXP.js +0 -104
  43. package/src/charts.js +0 -92
  44. package/src/connect.js +0 -21
  45. package/src/create.js +0 -28
  46. package/src/fetch.js +0 -74
  47. package/src/leaderboard.js +0 -51
  48. package/src/lvlRole.js +0 -53
  49. package/src/models/level.js +0 -10
  50. package/src/models/lvlrole.js +0 -8
  51. package/src/rank.js +0 -295
  52. package/src/reset.js +0 -22
  53. package/src/roleSetup.js +0 -121
  54. package/src/setLevel.js +0 -42
  55. package/src/setXP.js +0 -23
package/src/rank.js DELETED
@@ -1,295 +0,0 @@
1
- const levels = require("../src/models/level.js");
2
- const {join} = require("path");
3
-
4
- /**
5
- * @param {Discord.Message} message
6
- * @param {string} userID
7
- * @param {string} guildID
8
- * @param {import("../index").rankOptions} options
9
- */
10
-
11
- async function rank(message, userID, guildID, options = []) {
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
- const user = await levels.findOne({
17
- user: userID,
18
- guild: guildID
19
- });
20
- if (!user) throw new Error("[XP] NO_DATA | User has no XP data.");
21
-
22
- const leaderboard = await levels
23
- .find({
24
- guild: guildID
25
- })
26
- .sort([["xp", "descending"]])
27
- .exec();
28
-
29
- user.position = leaderboard.findIndex((i) => i.user === userID) + 1;
30
-
31
- let targetxp = user.level + 1;
32
-
33
- let target = targetxp * targetxp * 100;
34
-
35
- return rankCard(message, {
36
- level: user.level,
37
- currentXP: user.xp,
38
- neededXP: target,
39
- rank: user.position,
40
- background: options.background,
41
- color: options.color,
42
- lvlbar: options.lvlbar,
43
- lvlbarBg: options.lvlbarBg,
44
- member: message.guild.members.cache.get(userID)?.user
45
- });
46
-
47
- async function rankCard(message, options = []) {
48
- try {
49
- const Canvas = require("@napi-rs/canvas");
50
- Canvas.GlobalFonts.registerFromPath(
51
- join(__dirname, "Fonts", "Baloo-Regular.ttf"),
52
- "Sans Serif"
53
- );
54
-
55
- const member = options.member;
56
-
57
- const canvas = Canvas.createCanvas(1080, 400),
58
- ctx = canvas.getContext("2d");
59
-
60
- const name = member.tag;
61
- const noSymbols = (string) => string.replace(/[\u007f-\uffff]/g, "");
62
-
63
- let fsiz = "45px";
64
- if (message.guild.name.length >= 23) {
65
- fsiz = "38px";
66
- }
67
- if (message.guild.name.length >= 40) {
68
- fsiz = "28px";
69
- }
70
- if (message.guild.name.length >= 63) {
71
- fsiz = "22px";
72
- }
73
-
74
- let BackgroundRadius = "20",
75
- BackGroundImg =
76
- options.background ||
77
- "https://i.ibb.co/QQvMqf7/gradient.jpg",
78
- AttachmentName = "rank.png",
79
- AttachmentDesc = "Rank Card",
80
- Username = noSymbols(name),
81
- AvatarRoundRadius = "50",
82
- DrawLayerColor = "#000000",
83
- DrawLayerOpacity = 0.4,
84
- BoxColor = options.color || "#096DD1",
85
- LevelBarFill = options.lvlbar || "#ffffff",
86
- LevelBarBackground = options.lvlbarBg || "#ffffff",
87
- Rank = options.rank,
88
- TextEXP = shortener(options.currentXP) + " XP",
89
- LvlText = `Level ${shortener(options.level)}`,
90
- BarRadius = "20",
91
- TextXpNeded = "{current}/{needed}",
92
- CurrentXP = options.currentXP,
93
- NeededXP = options.neededXP;
94
-
95
- ctx.beginPath();
96
- ctx.moveTo(Number(BackgroundRadius), 0);
97
- ctx.lineTo(1080 - Number(BackgroundRadius), 0);
98
- ctx.quadraticCurveTo(1080, 0, 1080, Number(BackgroundRadius));
99
- ctx.lineTo(1080, 400 - Number(BackgroundRadius));
100
- ctx.quadraticCurveTo(
101
- 1080,
102
- 400,
103
- 1080 - Number(BackgroundRadius),
104
- 400
105
- );
106
-
107
- ctx.lineTo(Number(BackgroundRadius), 400);
108
- ctx.quadraticCurveTo(0, 400, 0, 400 - Number(BackgroundRadius));
109
- ctx.lineTo(0, Number(BackgroundRadius));
110
- ctx.quadraticCurveTo(0, 0, Number(BackgroundRadius), 0);
111
- ctx.closePath();
112
- ctx.clip();
113
- ctx.fillStyle = "#000000";
114
- ctx.fillRect(0, 0, 1080, 400);
115
- let background = await Canvas.loadImage(BackGroundImg);
116
- ctx.globalAlpha = 0.7;
117
- ctx.drawImage(background, 0, 0, 1080, 400);
118
- ctx.restore();
119
-
120
- ctx.fillStyle = DrawLayerColor;
121
- ctx.globalAlpha = DrawLayerOpacity;
122
- ctx.fillRect(40, 0, 240, canvas.height);
123
- ctx.globalAlpha = 1;
124
-
125
- let avatar = await Canvas.loadImage(member.displayAvatarURL());
126
- ctx.save();
127
- RoundedBox(ctx, 70, 30, 180, 180, Number(AvatarRoundRadius));
128
- ctx.strokeStyle = BoxColor;
129
- ctx.lineWidth = 15;
130
- ctx.stroke();
131
- ctx.clip();
132
- ctx.drawImage(avatar, 70, 30, 180, 180);
133
- ctx.restore();
134
-
135
- ctx.save();
136
- RoundedBox(ctx, 70, 240 + 50 + 30, 180, 50, 20);
137
- ctx.strokeStyle = "#BFC85A22";
138
- ctx.stroke();
139
- ctx.clip();
140
- ctx.fillStyle = BoxColor;
141
- ctx.globalAlpha = 1;
142
- ctx.fillRect(70, 320, 180, 50);
143
- ctx.globalAlpha = 1;
144
- ctx.fillStyle = "#ffffff";
145
- ctx.font = "32px \"Sans Serif\"";
146
- ctx.textAlign = "center";
147
- ctx.fillText(TextEXP, 160, 358);
148
- ctx.restore();
149
-
150
- ctx.save();
151
- RoundedBox(ctx, 70, 240, 180, 50, 20);
152
- ctx.strokeStyle = "#BFC85A22";
153
- ctx.stroke();
154
- ctx.clip();
155
- ctx.fillStyle = BoxColor;
156
- ctx.globalAlpha = 1;
157
- ctx.fillRect(70, 240, 180, 50, 50);
158
- ctx.globalAlpha = 1;
159
- ctx.fillStyle = "#ffffff";
160
- ctx.font = "32px \"Sans Serif\"";
161
- ctx.textAlign = "center";
162
- ctx.fillText(LvlText, 70 + 180 / 2, 278);
163
- ctx.restore();
164
-
165
- ctx.save();
166
- ctx.textAlign = "left";
167
- ctx.fillStyle = "#ffffff";
168
- ctx.shadowColor = "#000000";
169
- ctx.shadowBlur = 15;
170
- ctx.shadowOffsetX = 1;
171
- ctx.shadowOffsetY = 1;
172
- ctx.font = "39px \"Sans Serif\"";
173
- ctx.fillText(Username, 390, 80);
174
- ctx.restore();
175
-
176
- ctx.save();
177
- ctx.textAlign = "right";
178
- ctx.fillStyle = "#ffffff";
179
- ctx.shadowColor = "#000000";
180
- ctx.shadowBlur = 15;
181
- ctx.shadowOffsetX = 1;
182
- ctx.shadowOffsetY = 1;
183
- ctx.font = "55px \"Sans Serif\"";
184
- ctx.fillText("#" + Rank, canvas.width - 55, 80);
185
- ctx.restore();
186
-
187
- ctx.save();
188
- RoundedBox(ctx, 390, 305, 660, 70, Number(20));
189
- ctx.strokeStyle = "#BFC85A22";
190
- ctx.stroke();
191
- ctx.clip();
192
- ctx.fillStyle = "#ffffff";
193
- ctx.font = `${fsiz} "Sans Serif"`;
194
- ctx.textAlign = "center";
195
- ctx.fillText(message.guild.name, 720, 355);
196
- ctx.globalAlpha = 0.2;
197
- ctx.fillRect(390, 305, 660, 70);
198
- ctx.restore();
199
-
200
- ctx.save();
201
- RoundedBox(ctx, 390, 145, 660, 50, Number(BarRadius));
202
- ctx.strokeStyle = "#BFC85A22";
203
- ctx.stroke();
204
- ctx.clip();
205
- ctx.fillStyle = LevelBarBackground;
206
- ctx.globalAlpha = 0.2;
207
- ctx.fillRect(390, 145, 660, 50, 50);
208
- ctx.restore();
209
-
210
- const percent = (100 * CurrentXP) / NeededXP;
211
- const progress = (percent * 660) / 100;
212
-
213
- ctx.save();
214
- RoundedBox(ctx, 390, 145, progress, 50, Number(BarRadius));
215
- ctx.strokeStyle = "#BFC85A22";
216
- ctx.stroke();
217
- ctx.clip();
218
- ctx.fillStyle = LevelBarFill;
219
- ctx.globalAlpha = 0.5;
220
- ctx.fillRect(390, 145, progress, 50, 50);
221
- ctx.restore();
222
-
223
- ctx.save();
224
- ctx.textAlign = "left";
225
- ctx.fillStyle = "#ffffff";
226
- ctx.globalAlpha = 0.8;
227
- ctx.font = "30px \"Sans Serif\"";
228
- ctx.fillText("Next Level: " + shortener(NeededXP) + " XP", 390, 230);
229
- ctx.restore();
230
-
231
- const latestXP = Number(CurrentXP) - Number(NeededXP);
232
- const textXPEdited = TextXpNeded.replace(/{needed}/g, shortener(NeededXP).toString())
233
- .replace(/{current}/g, shortener(CurrentXP).toString())
234
- .replace(/{latest}/g, latestXP.toString());
235
- ctx.textAlign = "center";
236
- ctx.fillStyle = "#474747";
237
- ctx.globalAlpha = 1;
238
- ctx.font = "30px \"Sans Serif\"";
239
- ctx.fillText(textXPEdited, 730, 180);
240
-
241
- return {
242
- attachment: canvas.toBuffer("image/webp"),
243
- description: AttachmentDesc,
244
- name: AttachmentName
245
- };
246
- } catch (err) {
247
- console.log(`[XP] Error Occured. | rankCard | Error: ${err.stack}`);
248
- }
249
- }
250
- }
251
-
252
- function RoundedBox(ctx, x, y, width, height, radius) {
253
- ctx.beginPath();
254
- ctx.moveTo(x + radius, y);
255
- ctx.lineTo(x + width - radius, y);
256
- ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
257
- ctx.lineTo(x + width, y + height - radius);
258
- ctx.quadraticCurveTo(
259
- x + width,
260
- y + height,
261
- x + width - radius,
262
- y + height
263
- );
264
- ctx.lineTo(x + radius, y + height);
265
- ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
266
- ctx.lineTo(x, y + radius);
267
- ctx.quadraticCurveTo(x, y, x + radius, y);
268
- ctx.closePath();
269
- }
270
-
271
- function shortener(count) {
272
- const COUNT_ABBRS = [
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
- ];
287
-
288
- const i = 0 === count ? count : Math.floor(Math.log(count) / Math.log(1000));
289
- let result = parseFloat((count / Math.pow(1000, i)).toFixed(2));
290
- result += `${COUNT_ABBRS[i]}`;
291
- return result;
292
- }
293
-
294
- module.exports = rank;
295
-
package/src/reset.js DELETED
@@ -1,22 +0,0 @@
1
- const levels = require("../src/models/level.js");
2
-
3
- /**
4
- * @param {string} userID
5
- * @param {string} guildID
6
- */
7
-
8
- async function reset(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
- await levels
14
- .findOneAndUpdate({user: userID, guild: guildID}, {xp: 0, level: 0})
15
- .catch((err) => {
16
- throw new Error(err);
17
- });
18
-
19
- return {user: userID, guild: guildID, xp: 0, level: 0};
20
- }
21
-
22
- module.exports = reset;
package/src/roleSetup.js DELETED
@@ -1,121 +0,0 @@
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
- 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) return;
113
- rol = rol[0].lvlrole.filter((i) => i.lvl == level) || undefined;
114
-
115
- if (rol) {
116
- return rol;
117
- }
118
- }
119
- }
120
-
121
- module.exports = roleSetup;
package/src/setLevel.js DELETED
@@ -1,42 +0,0 @@
1
- const levels = require("../src/models/level.js");
2
- const {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
- if (!guildID) throw new Error("[XP] Guild ID was not provided.");
13
- if (!level || isNaN(Number(level))) throw new Error("[XP] Invalid level amount.");
14
-
15
- const {client} = message;
16
-
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
- );
22
-
23
- if (user.level !== level) {
24
- let data = {
25
- xp: user.xp,
26
- level: user.level,
27
- userID,
28
- guildID
29
- };
30
-
31
- let role = await roleSetup.find(client, guildID, level);
32
-
33
- client.emit("levelUp", message, data, role);
34
- }
35
-
36
- return {
37
- level: user.level,
38
- xp: user.xp
39
- };
40
- }
41
-
42
- module.exports = setLevel;
package/src/setXP.js DELETED
@@ -1,23 +0,0 @@
1
- const levels = require("../src/models/level.js");
2
-
3
- async function setXP(userID, guildID, xp) {
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}`));
18
- }
19
-
20
- return {xp};
21
- }
22
-
23
- module.exports = setXP;