simply-xp 1.1.2 → 1.1.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/simplyxp.js CHANGED
@@ -1,851 +1,31 @@
1
- let Discord = require('discord.js')
2
- const mongoose = require('mongoose')
3
- const levels = require('./models/level.js')
4
- const lrole = require('./models/lvlrole.js')
5
- const { join } = require('path')
6
-
7
- /**
8
- * @param {string} db
9
- * @param {import('./index').connectOptions} options
10
- */
11
-
12
- async function connect(db, options = []) {
13
- if (!db) throw new Error('[XP] Database URL was not provided')
14
-
15
- mongoose.connect(db, {
16
- useNewUrlParser: true,
17
- useUnifiedTopology: true
18
- })
19
-
20
- if (options.notify === false) return
21
- else return console.log('{ XP } Database Connected')
22
- }
23
-
24
- /**
25
- * @param {string} userID
26
- * @param {string} guildID
27
- */
28
-
29
- async function create(userID, guildID) {
30
- if (!userID) throw new Error('[XP] User ID was not provided.')
31
-
32
- if (!guildID) throw new Error('[XP] User ID was not provided.')
33
-
34
- let uzer = await levels.findOne({ user: userID, guild: guildID })
35
-
36
- if (uzer) return false
37
-
38
- const newuser = new levels({
39
- user: userID,
40
- guild: guildID
41
- })
42
- await newuser
43
- .save()
44
- .catch((e) => console.log(`[XP] Failed to save new use to database`))
45
-
46
- return true
47
- }
48
-
49
- /**
50
- * @param {string} userID
51
- * @param {string} guildID
52
- * @param {string} xp
53
- */
54
-
55
- async function addXP(message, userID, guildID, xp) {
56
- if (!userID) throw new Error('[XP] User ID was not provided.')
57
-
58
- if (!guildID) throw new Error('[XP] Guild ID was not provided.')
59
-
60
- if (!xp) throw new Error('[XP] XP amount is not provided.')
61
-
62
- let { client } = message
63
-
64
- let min
65
- let max
66
- if (xp.min) {
67
- if (!xp.max)
68
- throw new Error(
69
- '[XP] XP min amount is provided but max amount is not provided.'
70
- )
71
-
72
- min = Number(xp.min)
73
-
74
- if (Number(xp.min).toString() === 'NaN')
75
- throw new Error('[XP] XP amount (min) is not a number.')
76
- }
77
-
78
- if (xp.max) {
79
- if (!xp.min)
80
- throw new Error(
81
- '[XP] XP max amount is provided but min amount is not provided.'
82
- )
83
-
84
- max = Number(xp.max)
85
-
86
- if (Number(xp.max).toString() === 'NaN')
87
- throw new Error('[XP] XP amount (max) is not a number.')
88
- }
89
-
90
- if (xp.min && xp.max) {
91
- let randomNumber = Math.floor(Math.random() * (max - min) + min)
92
-
93
- xp = randomNumber
94
- }
95
-
96
- const user = await levels.findOne({ user: userID, guild: guildID })
97
-
98
- let lvl = Math.floor(0.1 * Math.sqrt(xp))
99
-
100
- if (!user) {
101
- const newUser = new levels({
102
- user: userID,
103
- guild: guildID,
104
- xp: xp,
105
- level: lvl
106
- })
107
-
108
- await newUser
109
- .save()
110
- .catch((e) => console.log(`[XP] Failed to save new user to database`))
111
-
112
- return {
113
- level: 0,
114
- exp: 0
115
- }
116
- }
117
- let level1 = user.level
118
-
119
- user.xp += parseInt(xp, 10)
120
- user.level = Math.floor(0.1 * Math.sqrt(user.xp))
121
-
122
- await user
123
- .save()
124
- .catch((e) =>
125
- console.log(`[XP] Failed to add XP | User: ${userID} | Err: ${e}`)
126
- )
127
-
128
- let level = user.level
129
-
130
- xp = user.xp
131
-
132
- if (user.xp === 0 || Math.sign(user.xp) === -1) {
133
- xp = 0
134
- }
135
-
136
- if (level1 !== level) {
137
- let data = {
138
- xp,
139
- level,
140
- userID,
141
- guildID
142
- }
143
-
144
- client.emit('levelUp', message, data)
145
- }
146
-
147
- return {
148
- level,
149
- xp
150
- }
151
- }
152
-
153
- /**
154
- * @param {string} userID
155
- * @param {string} guildID
156
- * @param {string} xp
157
- */
158
-
159
- async function setXP(userID, guildID, xp) {
160
- if (!userID) throw new Error('[XP] User ID was not provided.')
161
-
162
- if (!guildID) throw new Error('[XP] Guild ID was not provided.')
163
-
164
- if (!xp) throw new Error('[XP] XP amount is not provided.')
165
-
166
- if (Number(xp).toString() === 'NaN')
167
- throw new Error('[XP] XP amount is not a number.')
168
-
169
- const user = await levels.findOne({ user: userID, guild: guildID })
170
-
171
- let lvl = Math.floor(0.1 * Math.sqrt(xp))
172
-
173
- if (!user) {
174
- const newUser = new levels({
175
- user: userID,
176
- guild: guildID,
177
- xp: xp,
178
- level: lvl
179
- })
180
-
181
- await newUser
182
- .save()
183
- .catch((e) => console.log(`[XP] Failed to save new use to database`))
184
-
185
- return {
186
- xp: 0
187
- }
188
- }
189
- user.xp = xp
190
- user.level = Math.floor(0.1 * Math.sqrt(user.xp))
191
-
192
- await user
193
- .save()
194
- .catch((e) =>
195
- console.log(`[XP] Failed to set XP | User: ${userID} | Err: ${e}`)
196
- )
197
-
198
- return { xp }
199
- }
200
-
201
- /**
202
- * @param {Discord.Client} client
203
- * @param {string} guildID
204
- */
205
-
206
- async function leaderboard(client, guildID, limit) {
207
- if (!guildID) throw new Error('[XP] Guild ID was not provided.')
208
-
209
- let g = client.guilds.cache.get(guildID)
210
-
211
- let wo = g.members.cache.size
212
-
213
- let leaderboard = await levels
214
- .find({
215
- guild: guildID
216
- })
217
- .sort([['xp', 'descending']])
218
- .exec()
219
-
220
- const led = []
221
-
222
- function shortener(count) {
223
- const COUNT_ABBRS = ['', 'k', 'M', 'T']
224
-
225
- const i = 0 === count ? count : Math.floor(Math.log(count) / Math.log(1000))
226
- let result = parseFloat((count / Math.pow(1000, i)).toFixed(2))
227
- result += `${COUNT_ABBRS[i]}`
228
- return result
229
- }
230
-
231
- leaderboard.map((key) => {
232
- let user = g.members.cache.get(key.user)
233
- if (key.xp === 0) return
234
-
235
- let pos =
236
- leaderboard.findIndex(
237
- (i) => i.guild === key.guild && i.user === key.user
238
- ) + 1
239
-
240
- if (limit) {
241
- if (pos > Number(limit)) return
242
- }
243
-
244
- let shortXP = shortener(key.xp)
245
-
246
- if (!user) return
247
-
248
- led.push({
249
- guildID: key.guild,
250
- userID: key.user,
251
- xp: key.xp,
252
- shortxp: shortXP,
253
- level: key.level,
254
- position: pos,
255
- username: user.user.username,
256
- tag: user.user.tag
257
- })
258
- })
259
-
260
- return led
261
- }
262
-
263
- /**
264
- * @param {string} userID
265
- * @param {string} guildID
266
- */
267
-
268
- async function fetch(userID, guildID) {
269
- if (!userID) throw new Error('[XP] User ID was not provided.')
270
-
271
- if (!guildID) throw new Error('[XP] Guild ID was not provided.')
272
-
273
- let user = await levels.findOne({
274
- user: userID,
275
- guild: guildID
276
- })
277
- if (!user) {
278
- user = new levels({
279
- user: userID,
280
- guild: guildID,
281
- xp: 0,
282
- level: 0
283
- })
284
-
285
- await user.save()
286
- }
287
-
288
- const leaderboard = await levels
289
- .find({
290
- guild: guildID
291
- })
292
- .sort([['xp', 'descending']])
293
- .exec()
294
-
295
- if (user === null)
296
- return {
297
- level: 0,
298
- xp: 0,
299
- reqxp: 100,
300
- rank: leaderboard.findIndex((i) => i.user === userID) + 1,
301
- shortxp: 0,
302
- shortreq: 100
303
- }
304
-
305
- user.position = leaderboard.findIndex((i) => i.user === userID) + 1
306
-
307
- let targetxp = user.level + 1
308
-
309
- let target = targetxp * targetxp * 100
310
-
311
- function shortener(count) {
312
- const COUNT_ABBRS = ['', 'k', 'M', 'T']
313
-
314
- const i = 0 === count ? count : Math.floor(Math.log(count) / Math.log(1000))
315
- let result = parseFloat((count / Math.pow(1000, i)).toFixed(2))
316
- result += `${COUNT_ABBRS[i]}`
317
- return result
318
- }
319
-
320
- let shortXP = shortener(user.xp)
321
-
322
- let shortReqXP = shortener(target)
323
-
324
- return {
325
- level: user.level,
326
- xp: user.xp,
327
- reqxp: target,
328
- rank: user.position,
329
- shortxp: shortXP,
330
- shortreq: shortReqXP
331
- }
332
- }
333
-
334
- /**
335
- * @param {Discord.Message} message
336
- * @param {import('./index').chartsOptions} options
337
- */
338
-
339
- async function charts(message, options = []) {
340
- let { client } = message
341
- const ChartJSImage = require('chart.js-image')
342
-
343
- let data = []
344
- let uzern = []
345
-
346
- await leaderboard(client, message.guild.id).then((e) => {
347
- e.forEach((m) => {
348
- if (m.position <= 5) {
349
- data.push(m.xp)
350
- uzern.push(m.tag)
351
- }
352
- })
353
- })
354
-
355
- const line_chart = ChartJSImage()
356
- .chart({
357
- type: options.type || 'bar',
358
- data: {
359
- labels: uzern,
360
- datasets: [
361
- {
362
- label: 'Leaderboards',
363
- data: data,
364
- backgroundColor: [
365
- 'rgba(255, 99, 132, 0.5)',
366
- 'rgba(255, 159, 64, 0.5)',
367
- 'rgba(255, 205, 86, 0.5)',
368
- 'rgba(75, 192, 192, 0.5)',
369
- 'rgba(54, 162, 235, 0.5)',
370
- 'rgba(153, 102, 255, 0.5)',
371
- 'rgb(201, 203, 207, 0.5)'
372
- ],
373
- borderColor: [
374
- 'rgb(255, 99, 132)',
375
- 'rgb(255, 159, 64)',
376
- 'rgb(255, 205, 86)',
377
- 'rgb(75, 192, 192)',
378
- 'rgb(54, 162, 235)',
379
- 'rgb(153, 102, 255)',
380
- 'rgb(201, 203, 207)'
381
- ],
382
- borderWidth: 2
383
- }
384
- ]
385
- },
386
- options: {
387
- plugins: {
388
- legend: {
389
- labels: {
390
- font: {
391
- family: 'Courier New'
392
- }
393
- }
394
- }
395
- },
396
- title: {
397
- display: true,
398
- text: 'XP Datasheet'
399
- }
400
- }
401
- })
402
- .backgroundColor(options.background || '#2F3136')
403
- .width(940) // 500px
404
- .height(520) // 300px
405
-
406
- const attachment = new Discord.MessageAttachment(
407
- line_chart.toURL(),
408
- `chart.png`
409
- )
410
- return attachment
411
- }
412
-
413
- /**
414
- * @param {Discord.Message} message
415
- * @param {string} userID
416
- * @param {string} guildID
417
- * @param {import('./index').rankOptions} options
418
- */
419
-
420
- async function rank(message, userID, guildID, options = []) {
421
- if (!userID) throw new Error('[XP] User ID was not provided.')
422
-
423
- if (!guildID) throw new Error('[XP] Guild ID was not provided.')
424
-
425
- const user = await levels.findOne({
426
- user: userID,
427
- guild: guildID
428
- })
429
- if (!user) throw new Error('[XP] NO_DATA | User has no XP data.')
430
-
431
- const leaderboard = await levels
432
- .find({
433
- guild: guildID
434
- })
435
- .sort([['xp', 'descending']])
436
- .exec()
437
-
438
- user.position = leaderboard.findIndex((i) => i.user === userID) + 1
439
-
440
- let targetxp = user.level + 1
441
-
442
- let target = targetxp * targetxp * 100
443
-
444
- return rankCard(message, {
445
- level: user.level,
446
- currentXP: user.xp,
447
- neededXP: target,
448
- rank: user.position,
449
- background: options.background,
450
- color: options.color,
451
- member: message.guild.members.cache.get(userID)?.user
452
- })
453
-
454
- async function rankCard(message, options = []) {
455
- try {
456
- const Canvas = require('canvas')
457
- const { registerFont } = require('canvas')
458
- registerFont('./Fonts/Poppins-SemiBold.ttf', {
459
- family: 'Poppins-Regular'
460
- })
461
- registerFont('./Fonts/Poppins-SemiBold.ttf', {
462
- family: 'Poppins-Bold'
463
- })
464
-
465
- function shortener(count) {
466
- const COUNT_ABBRS = ['', 'k', 'M', 'T']
467
-
468
- const i =
469
- 0 === count ? count : Math.floor(Math.log(count) / Math.log(1000))
470
- let result = parseFloat((count / Math.pow(1000, i)).toFixed(2))
471
- result += `${COUNT_ABBRS[i]}`
472
- return result
473
- }
474
-
475
- const member = options.member
476
-
477
- const canvas = Canvas.createCanvas(1080, 400),
478
- ctx = canvas.getContext('2d')
479
-
480
- const name = member.tag
481
- const noSymbols = (string) => string.replace(/[\u007f-\uffff]/g, '')
482
-
483
- let fsiz = '45px'
484
- if (message.guild.name.length >= 23) {
485
- fsiz = '38px'
486
- }
487
- if (message.guild.name.length >= 40) {
488
- fsiz = '28px'
489
- }
490
- if (message.guild.name.length >= 63) {
491
- fsiz = '22px'
492
- }
493
-
494
- let BackgroundRadius = '20',
495
- BackGroundImg =
496
- options.background ||
497
- 'https://media.discordapp.net/attachments/868506665102762034/876750913866461185/photo-1579546929518-9e396f3cc809.png?width=640&height=427',
498
- AttachmentName = 'rank.png',
499
- Username = noSymbols(name),
500
- AvatarRoundRadius = '50',
501
- DrawLayerColor = '#000000',
502
- DrawLayerOpacity = '0.4',
503
- BoxColor = options.color || '#096DD1',
504
- LevelBarFill = options.lvlbar || '#ffffff',
505
- LevelBarBackground = options.lvlbarBg || '#ffffff',
506
- Rank = options.rank,
507
- TextEXP = shortener(options.currentXP) + ' xp',
508
- LvlText = `Level ${shortener(options.level)}`,
509
- BarRadius = '20',
510
- TextXpNeded = '{current}/{needed}',
511
- CurrentXP = options.currentXP,
512
- NeededXP = options.neededXP
513
-
514
- ctx.beginPath()
515
- ctx.moveTo(0 + Number(BackgroundRadius), 0)
516
- ctx.lineTo(0 + 1080 - Number(BackgroundRadius), 0)
517
- ctx.quadraticCurveTo(0 + 1080, 0, 0 + 1080, 0 + Number(BackgroundRadius))
518
- ctx.lineTo(0 + 1080, 0 + 400 - Number(BackgroundRadius))
519
- ctx.quadraticCurveTo(
520
- 0 + 1080,
521
- 0 + 400,
522
- 0 + 1080 - Number(BackgroundRadius),
523
- 0 + 400
524
- )
525
-
526
- ctx.lineTo(0 + Number(BackgroundRadius), 0 + 400)
527
- ctx.quadraticCurveTo(0, 0 + 400, 0, 0 + 400 - Number(BackgroundRadius))
528
- ctx.lineTo(0, 0 + Number(BackgroundRadius))
529
- ctx.quadraticCurveTo(0, 0, 0 + Number(BackgroundRadius), 0)
530
- ctx.closePath()
531
- ctx.clip()
532
- ctx.fillStyle = '#000000'
533
- ctx.fillRect(0, 0, 1080, 400)
534
- let background = await Canvas.loadImage(BackGroundImg)
535
- ctx.globalAlpha = 0.7
536
- ctx.drawImage(background, 0, 0, 1080, 400)
537
- ctx.restore()
538
-
539
- ctx.fillStyle = DrawLayerColor
540
- ctx.globalAlpha = DrawLayerOpacity
541
- ctx.fillRect(40, 0, 240, canvas.height)
542
- ctx.globalAlpha = 1
543
-
544
- function RoundedBox(ctx, x, y, width, height, radius) {
545
- ctx.beginPath()
546
- ctx.moveTo(x + radius, y)
547
- ctx.lineTo(x + width - radius, y)
548
- ctx.quadraticCurveTo(x + width, y, x + width, y + radius)
549
- ctx.lineTo(x + width, y + height - radius)
550
- ctx.quadraticCurveTo(
551
- x + width,
552
- y + height,
553
- x + width - radius,
554
- y + height
555
- )
556
- ctx.lineTo(x + radius, y + height)
557
- ctx.quadraticCurveTo(x, y + height, x, y + height - radius)
558
- ctx.lineTo(x, y + radius)
559
- ctx.quadraticCurveTo(x, y, x + radius, y)
560
- ctx.closePath()
561
- }
562
-
563
- let avatar = await Canvas.loadImage(
564
- member.displayAvatarURL({ dynamic: true, format: 'png' })
565
- )
566
- ctx.save()
567
- RoundedBox(ctx, 40 + 30, 30, 180, 180, Number(AvatarRoundRadius))
568
- ctx.strokeStyle = BoxColor
569
- ctx.lineWidth = '10'
570
- ctx.stroke()
571
- ctx.clip()
572
- ctx.drawImage(avatar, 40 + 30, 30, 180, 180)
573
- ctx.restore()
574
-
575
- ctx.save()
576
- RoundedBox(ctx, 40 + 30, 30 + 180 + 30 + 50 + 30, 180, 50, 20)
577
- ctx.strokeStyle = '#BFC85A22'
578
- ctx.stroke()
579
- ctx.clip()
580
- ctx.fillStyle = BoxColor
581
- ctx.globalAlpha = '1'
582
- ctx.fillRect(40 + 30, 30 + 180 + 30 + 50 + 30, 180, 50)
583
- ctx.globalAlpha = 1
584
- ctx.fillStyle = '#ffffff'
585
- ctx.font = '32px "Poppins-Bold"'
586
- ctx.textAlign = 'center'
587
- ctx.fillText(TextEXP, 40 + 30 + 180 / 2, 30 + 180 + 30 + 30 + 50 + 38)
588
- ctx.restore()
589
-
590
- ctx.save()
591
- RoundedBox(ctx, 40 + 30, 30 + 180 + 30, 180, 50, 20)
592
- ctx.strokeStyle = '#BFC85A22'
593
- ctx.stroke()
594
- ctx.clip()
595
- ctx.fillStyle = BoxColor
596
- ctx.globalAlpha = '1'
597
- ctx.fillRect(40 + 30, 30 + 180 + 30, 180, 50, 50)
598
- ctx.globalAlpha = 1
599
- ctx.fillStyle = '#ffffff'
600
- ctx.font = '32px "Poppins-Bold"'
601
- ctx.textAlign = 'center'
602
- ctx.fillText(LvlText, 40 + 30 + 180 / 2, 30 + 180 + 30 + 38)
603
- ctx.restore()
604
-
605
- ctx.save()
606
- ctx.textAlign = 'left'
607
- ctx.fillStyle = '#ffffff'
608
- ctx.shadowColor = '#000000'
609
- ctx.shadowBlur = 15
610
- ctx.shadowOffsetX = 1
611
- ctx.shadowOffsetY = 1
612
- ctx.font = '39px "Poppins-Bold"'
613
- ctx.fillText(Username, 390, 80)
614
- ctx.restore()
615
-
616
- ctx.save()
617
- ctx.textAlign = 'right'
618
- ctx.fillStyle = '#ffffff'
619
- ctx.shadowColor = '#000000'
620
- ctx.shadowBlur = 15
621
- ctx.shadowOffsetX = 1
622
- ctx.shadowOffsetY = 1
623
- ctx.font = '55px "Poppins-Bold"'
624
- ctx.fillText('#' + Rank, canvas.width - 50 - 5, 80)
625
- ctx.restore()
626
-
627
- ctx.save()
628
- RoundedBox(ctx, 390, 305, 660, 70, Number(20))
629
- ctx.strokeStyle = '#BFC85A22'
630
- ctx.stroke()
631
- ctx.clip()
632
- ctx.fillStyle = '#ffffff'
633
- ctx.font = `${fsiz} "Poppins-Bold"`
634
- ctx.textAlign = 'center'
635
- ctx.fillText(message.guild.name, 60 + 660, 355)
636
- ctx.globalAlpha = '0.2'
637
- ctx.fillRect(390, 305, 660, 70)
638
- ctx.restore()
639
-
640
- ctx.save()
641
- RoundedBox(ctx, 390, 145, 660, 50, Number(BarRadius))
642
- ctx.strokeStyle = '#BFC85A22'
643
- ctx.stroke()
644
- ctx.clip()
645
- ctx.fillStyle = LevelBarBackground
646
- ctx.globalAlpha = '0.2'
647
- ctx.fillRect(390, 145, 660, 50, 50)
648
- ctx.restore()
649
-
650
- const percent = (100 * CurrentXP) / NeededXP
651
- const progress = (percent * 660) / 100
652
-
653
- ctx.save()
654
- RoundedBox(ctx, 390, 145, progress, 50, Number(BarRadius))
655
- ctx.strokeStyle = '#BFC85A22'
656
- ctx.stroke()
657
- ctx.clip()
658
- ctx.fillStyle = LevelBarFill
659
- ctx.globalAlpha = '0.5'
660
- ctx.fillRect(390, 145, progress, 50, 50)
661
- ctx.restore()
662
-
663
- ctx.save()
664
- ctx.textAlign = 'left'
665
- ctx.fillStyle = '#ffffff'
666
- ctx.globalAlpha = '0.8'
667
- ctx.font = '30px "Poppins-Bold"'
668
- ctx.fillText('Next Level: ' + shortener(NeededXP) + ' xp', 390, 230)
669
- ctx.restore()
670
-
671
- const latestXP = Number(CurrentXP) - Number(NeededXP)
672
- const textXPEdited = TextXpNeded.replace(/{needed}/g, shortener(NeededXP))
673
- .replace(/{current}/g, shortener(CurrentXP))
674
- .replace(/{latest}/g, latestXP)
675
- ctx.textAlign = 'center'
676
- ctx.fillStyle = '#474747'
677
- ctx.globalAlpha = 1
678
- ctx.font = '30px "Poppins-Bold"'
679
- ctx.fillText(textXPEdited, 730, 180)
680
-
681
- const attachment = new Discord.MessageAttachment(
682
- canvas.toBuffer(),
683
- AttachmentName
684
- )
685
-
686
- return attachment
687
- } catch (err) {
688
- console.log(`[XP] Error Occured. | rankCard | Error: ${err.stack}`)
689
- }
690
- }
691
- }
692
-
693
- class roleSetup {
694
- /**
695
- * @param {Discord.Client} client
696
- * @param {string} guildID
697
- * @param {import('./index').lvladdOptions} options
698
- */
699
-
700
- static async add(client, guildID, options = []) {
701
- let rol = await lrole.findOne({
702
- gid: guildID,
703
- lvlrole: {
704
- lvl: options.level,
705
- role: options.role
706
- }
707
- })
708
-
709
- let g = client.guilds.cache.get(guildID)
710
-
711
- let roll = g.roles.cache.find((r) => r.id === options.role)
712
-
713
- if (roll) {
714
- if (rol) throw new Error('Level Already Exist. Use delete')
715
- else if (!rol) {
716
- let newrol = await lrole.findOne({
717
- gid: guildID
718
- })
719
-
720
- if (!newrol) {
721
- newrol = new lrole({
722
- gid: guildID,
723
- lvlrole: []
724
- })
725
-
726
- await newrol.save()
727
- }
728
-
729
- newrol.lvlrole.push({ lvl: options.level, role: options.role })
730
-
731
- await newrol
732
- .save()
733
- .catch((e) =>
734
- console.log(`[XP] Failed to add lvlrole to database | ${e}`)
735
- )
736
-
737
- return 'Added the role to levelRole'
738
- }
739
- } else {
740
- throw new Error(
741
- 'Role ID is invalid. | ' +
742
- `Guild ID: ${guildID} | Role ID: ${options.role}`
743
- )
744
- }
745
- }
746
-
747
- /**
748
- * @param {Discord.Client} client
749
- * @param {string} guildID
750
- * @param {import('./index').lvlremoveOptions} options
751
- */
752
-
753
- static async remove(client, guildID, options = []) {
754
- let rol = await lrole.find({
755
- gid: guildID
756
- })
757
-
758
- if (!rol || rol.length === 0)
759
- throw new Error('Level role with this level does not exist')
760
- rol = rol[0].lvlrole.find((item) => item.lvl === options.level) || undefined
761
-
762
- if (rol) {
763
- let newrol = await lrole.findOneAndUpdate(
764
- {
765
- gid: guildID
766
- },
767
- {
768
- $pull: { lvlrole: { lvl: options.level } }
769
- }
770
- )
771
-
772
- return 'Deleting the role from levelRole'
773
- } else throw new Error('Level role with this level does not exist')
774
- }
775
-
776
- static async fetch(client, guildID, options = []) {
777
- let rol = await lrole.find({
778
- gid: guildID
779
- })
780
-
781
- if (!rol || rol.length === 0)
782
- throw new Error('There is no levelRole in this guild')
783
- rol = rol[0].lvlrole.find((item) => item.lvl === options.level) || undefined
784
-
785
- if (rol) {
786
- return rol[0].lvlrole
787
- }
788
- }
789
- }
790
-
791
- /**
792
- * @param {Discord.Message} message
793
- * @param {string} userID
794
- * @param {string} guildID
795
- */
796
-
797
- async function lvlRole(message, userID, guildID) {
798
- let e = await lrole.find({
799
- gid: guildID
800
- })
801
-
802
- if (!e) return
803
-
804
- let user = await levels.findOne({
805
- user: userID,
806
- guild: guildID
807
- })
808
- if (!user) {
809
- const newuser = new levels({
810
- user: userID,
811
- guild: guildID
812
- })
813
-
814
- await newuser
815
- .save()
816
- .catch((e) => console.log(`[XP] Failed to save new user to database`))
817
- }
818
-
819
- e.forEach((ee) => {
820
- ee = ee.lvlrole
821
-
822
- ee.forEach((xd) => {
823
- if (user && user.level >= Number(xd.lvl)) {
824
- let u = message.guild.members.cache.get(userID)
825
-
826
- let real = message.guild.roles.cache.find((r) => r.id === xd.role)
827
- if (!real) return
828
- else {
829
- u.roles.add(real).catch((err) => {
830
- message.channel.send(
831
- '[XP] ERROR: Role is higher than me. `MISSING_PERMISSIONS`'
832
- )
833
- })
834
- }
835
- }
836
- })
837
- })
838
- }
839
-
840
- module.exports = {
841
- connect: connect,
842
- create: create,
843
- charts: charts,
844
- addXP: addXP,
845
- rank: rank,
846
- fetch: fetch,
847
- setXP: setXP,
848
- leaderboard: leaderboard,
849
- roleSetup: roleSetup,
850
- lvlRole: lvlRole
851
- }
1
+ try {
2
+ require('discord.js')
3
+ } catch (e) {
4
+ throw new Error('Discord.JS is required for this package to run')
5
+ }
6
+
7
+ module.exports.roleSetup = require('./src/roleSetup')
8
+
9
+ module.exports.addLevel = require('./src/addLevel')
10
+
11
+ module.exports.addXP = require('./src/addXP')
12
+
13
+ module.exports.charts = require('./src/charts')
14
+
15
+ module.exports.connect = require('./src/connect')
16
+
17
+ module.exports.create = require('./src/create')
18
+
19
+ module.exports.fetch = require('./src/fetch')
20
+
21
+ module.exports.leaderboard = require('./src/leaderboard')
22
+
23
+ module.exports.lvlRole = require('./src/lvlRole')
24
+
25
+ module.exports.rank = require('./src/rank')
26
+
27
+ module.exports.setLevel = require('./src/setLevel')
28
+
29
+ module.exports.setXP = require('./src/setXP')
30
+
31
+ module.exports.reset = require('./src/reset')