apexify.js 2.4.4 → 3.0.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.
Files changed (71) hide show
  1. package/README.md +9 -715
  2. package/dataSQL/data.sql +5 -0
  3. package/dataSQL/dataName.sql +0 -0
  4. package/dataSQL/example_table.sql +1 -0
  5. package/index.js +3 -78
  6. package/lib/ai/apexAI.js +2 -2
  7. package/lib/{discord/events/handler → ai/buttons}/tools.js +1 -1
  8. package/lib/utils.js +2 -72
  9. package/package.json +2 -81
  10. package/lib/canvas/themes/level ara.ttf +0 -0
  11. package/lib/canvas/themes/levelFont.ttf +0 -0
  12. package/lib/canvas/themes/levels-card.js +0 -884
  13. package/lib/canvas/themes/music-card.js +0 -342
  14. package/lib/canvas/themes/numbers.ttf +0 -0
  15. package/lib/canvas/themes/tajawal.ttf +0 -0
  16. package/lib/database/MongoDB.js +0 -126
  17. package/lib/database/NanoDB.js +0 -1341
  18. package/lib/database/mongoDb/aggregate.js +0 -120
  19. package/lib/database/mongoDb/countDocs.js +0 -115
  20. package/lib/database/mongoDb/createCollection.js +0 -125
  21. package/lib/database/mongoDb/dataSize.js +0 -89
  22. package/lib/database/mongoDb/distinct.js +0 -110
  23. package/lib/database/mongoDb/drop.js +0 -76
  24. package/lib/database/mongoDb/find.js +0 -122
  25. package/lib/database/mongoDb/geoNear.js +0 -91
  26. package/lib/database/mongoDb/index.js +0 -71
  27. package/lib/database/mongoDb/listCollections.js +0 -81
  28. package/lib/database/mongoDb/migrateAndPrune.js +0 -89
  29. package/lib/database/mongoDb/migrateData.js +0 -79
  30. package/lib/database/mongoDb/remove.js +0 -73
  31. package/lib/database/mongoDb/removeMany.js +0 -73
  32. package/lib/database/mongoDb/removeManyExcept.js +0 -91
  33. package/lib/database/mongoDb/removeSpecific.js +0 -93
  34. package/lib/database/mongoDb/save.js +0 -94
  35. package/lib/database/mongoDb/searchMany.js +0 -109
  36. package/lib/database/mongoDb/textSearch.js +0 -88
  37. package/lib/database/mongoDb/updateAll.js +0 -80
  38. package/lib/database/mongoDb/updateAllExcept.js +0 -108
  39. package/lib/database/mongoDb/updateData.js +0 -106
  40. package/lib/database/nanoDb/fetchData.js +0 -39
  41. package/lib/database/nanoDb/removeField.js +0 -46
  42. package/lib/database/nanoDb/saveData.js +0 -68
  43. package/lib/database/nanoDb/updateFilter.js +0 -24
  44. package/lib/database/ready-schemas.js +0 -245
  45. package/lib/database/utils.js +0 -58
  46. package/lib/discord/discord-build/commands-(prefix)/music/functions/buttons.js +0 -361
  47. package/lib/discord/discord-build/commands-(prefix)/music/functions/end.js +0 -58
  48. package/lib/discord/discord-build/commands-(prefix)/music/functions/start.js +0 -115
  49. package/lib/discord/discord-build/commands-(prefix)/music/play.js +0 -152
  50. package/lib/discord/discord-build/commands-(prefix)/music/previous.js +0 -82
  51. package/lib/discord/discord-build/commands-(prefix)/music/resume.js +0 -93
  52. package/lib/discord/discord-build/commands-(prefix)/music/skip.js +0 -82
  53. package/lib/discord/discord-build/commands-(prefix)/music/stop.js +0 -79
  54. package/lib/discord/discord-build/components/buttons.js +0 -75
  55. package/lib/discord/discord-build/components/menus.js +0 -81
  56. package/lib/discord/discord-build/components/paginator.js +0 -156
  57. package/lib/discord/discord-build/components/permsChecker.js +0 -81
  58. package/lib/discord/discord-build/levelingSystem/cLevel.js +0 -829
  59. package/lib/discord/discord-build/levelingSystem/cLevelsArray.json +0 -104
  60. package/lib/discord/discord-build/utils.js +0 -23
  61. package/lib/discord/events/eventer.js +0 -145
  62. package/lib/discord/events/prefixRegister.js +0 -148
  63. package/lib/discord/events/prefixResponder.js +0 -163
  64. package/lib/discord/events/slashRegister.js +0 -183
  65. package/lib/discord/events/slashResponder.js +0 -108
  66. package/lib/discord/events/starter.js +0 -441
  67. package/lib/discord/functions/perms.js +0 -19
  68. package/lib/discord/utils.js +0 -6
  69. package/lib/general-functions/discord/typeWriter.js +0 -77
  70. package/lib/general-functions/utils.js +0 -19
  71. /package/lib/{discord/events/handler → ai/buttons}/drawMenu.js +0 -0
@@ -1,829 +0,0 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
- return new (P || (P = Promise))(function (resolve, reject) {
20
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
- step((generator = generator.apply(thisArg, _arguments || [])).next());
24
- });
25
- };
26
- var __generator = (this && this.__generator) || function (thisArg, body) {
27
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
- function verb(n) { return function (v) { return step([n, v]); }; }
30
- function step(op) {
31
- if (f) throw new TypeError("Generator is already executing.");
32
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
33
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
- if (y = 0, t) op = [op[0] & 2, t.value];
35
- switch (op[0]) {
36
- case 0: case 1: t = op; break;
37
- case 4: _.label++; return { value: op[1], done: false };
38
- case 5: _.label++; y = op[1]; op = [0]; continue;
39
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
- default:
41
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
- if (t[2]) _.ops.pop();
46
- _.trys.pop(); continue;
47
- }
48
- op = body.call(thisArg, _);
49
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
- }
52
- };
53
- Object.defineProperty(exports, "__esModule", { value: true });
54
- exports.LevelingSystem = void 0;
55
- var events_1 = require("events");
56
- var utils_1 = require("../../../database/utils");
57
- var levels_card_1 = require("../../../canvas/themes/levels-card");
58
- var cLevelsArray_json_1 = require("./cLevelsArray.json");
59
- var LevelingSystem = /** @class */ (function (_super) {
60
- __extends(LevelingSystem, _super);
61
- function LevelingSystem(config) {
62
- var _this = _super.call(this) || this;
63
- _this.configCollection = 'configCollection'; // Replace with your actual collection name
64
- _this.levelsCollection = 'levelsCollection'; // Replace with your actual collection name
65
- _this.XpCount = (config === null || config === void 0 ? void 0 : config.XpCount) || 5;
66
- _this.rate = (config === null || config === void 0 ? void 0 : config.rate) || 2;
67
- _this.channelId = (config === null || config === void 0 ? void 0 : config.channelId) || null;
68
- _this.guildId = (config === null || config === void 0 ? void 0 : config.guildId) || null;
69
- _this.levelsArray = (config === null || config === void 0 ? void 0 : config.levelsArray) || cLevelsArray_json_1.default;
70
- _this.levelingMessage = (config === null || config === void 0 ? void 0 : config.levelUpMessage) || 'Congrates {user} you level up to level {level}.';
71
- return _this;
72
- }
73
- LevelingSystem.prototype.setupConfig = function () {
74
- return __awaiter(this, void 0, void 0, function () {
75
- var _i, _a, levelObj, configExists, error_1;
76
- return __generator(this, function (_b) {
77
- switch (_b.label) {
78
- case 0:
79
- _b.trys.push([0, 6, , 7]);
80
- if (!this.guildId || !this.channelId) {
81
- throw new Error('Error: Please provide guildId and channelId');
82
- }
83
- if (!Array.isArray(this.levelsArray)) {
84
- throw new Error('Error: levelsArray must be an array');
85
- }
86
- for (_i = 0, _a = this.levelsArray; _i < _a.length; _i++) {
87
- levelObj = _a[_i];
88
- if (typeof levelObj !== 'object' || !('level' in levelObj) || !('xpCount' in levelObj)) {
89
- throw new Error('Error: Invalid structure in levelsArray. Each object should have "level" and "xpCount" properties.');
90
- }
91
- }
92
- return [4 /*yield*/, (0, utils_1.find)(this.configCollection, { guildId: this.guildId })];
93
- case 1:
94
- configExists = _b.sent();
95
- if (!!configExists) return [3 /*break*/, 3];
96
- return [4 /*yield*/, (0, utils_1.updateData)(this.configCollection, { guildId: this.guildId }, {
97
- $set: {
98
- XpCount: this.XpCount,
99
- rate: this.rate,
100
- channelId: this.channelId,
101
- guildId: this.guildId,
102
- levelUpMessage: this.levelingMessage,
103
- levelsArray: this.levelsArray,
104
- },
105
- })];
106
- case 2:
107
- _b.sent();
108
- return [3 /*break*/, 5];
109
- case 3: return [4 /*yield*/, (0, utils_1.updateData)(this.configCollection, { guildId: this.guildId }, {
110
- $set: {
111
- XpCount: this.XpCount,
112
- rate: this.rate,
113
- channelId: this.channelId,
114
- guildId: this.guildId,
115
- levelUpMessage: this.levelingMessage,
116
- levelsArray: this.levelsArray,
117
- },
118
- })];
119
- case 4:
120
- _b.sent();
121
- _b.label = 5;
122
- case 5: return [3 /*break*/, 7];
123
- case 6:
124
- error_1 = _b.sent();
125
- console.error('Error in setupConfig:', error_1);
126
- this.emit('error', error_1.message);
127
- return [3 /*break*/, 7];
128
- case 7: return [2 /*return*/];
129
- }
130
- });
131
- });
132
- };
133
- LevelingSystem.prototype.xpCard = function (context, guildId, userId, version) {
134
- if (version === void 0) { version = 1; }
135
- return __awaiter(this, void 0, void 0, function () {
136
- var search, fetchServer, rank, sortedLevels, userRank, userIndex, nextLevelIndex, nextLevelData, card, error_2;
137
- return __generator(this, function (_a) {
138
- switch (_a.label) {
139
- case 0:
140
- _a.trys.push([0, 5, , 6]);
141
- if (!context) {
142
- throw new Error('Error: Interaction/Message parameter is required. Usage: xpCard(interaction, guildId, userId). You can replace interaction with message if prefix.');
143
- }
144
- if (!guildId) {
145
- throw new Error('Error: GuildId parameter is required. Usage: xpCard(guildId, userId).');
146
- }
147
- if (!userId) {
148
- throw new Error('Error: UserId parameter is required. Usage: xpCard(guildId, userId).');
149
- }
150
- return [4 /*yield*/, (0, utils_1.find)(this.levelsCollection, { userId: userId, guildId: guildId })];
151
- case 1:
152
- search = _a.sent();
153
- return [4 /*yield*/, (0, utils_1.find)(this.configCollection, { guildId: guildId })];
154
- case 2:
155
- fetchServer = _a.sent();
156
- if (!search) {
157
- return [2 /*return*/, 'No data found for this user'];
158
- }
159
- return [4 /*yield*/, (0, utils_1.searchMany)([
160
- { collectionName: this.levelsCollection, displayment: null, filter: { guildId: guildId } },
161
- ])];
162
- case 3:
163
- rank = _a.sent();
164
- sortedLevels = rank.levelsCollection.sort(function (a, b) {
165
- return b.xpCount - a.xpCount;
166
- });
167
- userRank = void 0;
168
- userIndex = sortedLevels.findIndex(function (user) { return user.userId === userId; });
169
- if (userIndex !== -1) {
170
- userRank = userIndex + 1;
171
- }
172
- else {
173
- userRank = 'unknown';
174
- }
175
- nextLevelIndex = search.level + 1;
176
- nextLevelData = void 0;
177
- if (nextLevelIndex < fetchServer.levelsArray.length) {
178
- nextLevelData = fetchServer.levelsArray[nextLevelIndex];
179
- }
180
- else {
181
- nextLevelData = { level: 'max', xpCount: 'max' };
182
- }
183
- return [4 /*yield*/, (0, levels_card_1.xpRank)(context, guildId, userId, search.level, userRank, search.xpCount, nextLevelData.xpCount, fetchServer.levelsArray, version, search.bannerURL)];
184
- case 4:
185
- card = _a.sent();
186
- if (!card)
187
- return [2 /*return*/, 'An Error occurred while drawing users xpCard.'];
188
- return [2 /*return*/, card];
189
- case 5:
190
- error_2 = _a.sent();
191
- console.log(error_2.message);
192
- throw new Error("An error occurred while sending xpCard.");
193
- case 6: return [2 /*return*/];
194
- }
195
- });
196
- });
197
- };
198
- LevelingSystem.prototype.disableLevel = function (guildId) {
199
- return __awaiter(this, void 0, void 0, function () {
200
- var search, removeData, success, error_3;
201
- return __generator(this, function (_a) {
202
- switch (_a.label) {
203
- case 0:
204
- _a.trys.push([0, 3, , 4]);
205
- if (!guildId) {
206
- throw new Error('Error: GuildId parameter is required. Usage: disbaleLevel(guildId).');
207
- }
208
- return [4 /*yield*/, (0, utils_1.find)(this.configCollection, { guildId: guildId })];
209
- case 1:
210
- search = _a.sent();
211
- if (!search) {
212
- return [2 /*return*/, 'No data found for this server'];
213
- }
214
- return [4 /*yield*/, (0, utils_1.remove)(this.configCollection, { guildId: guildId })];
215
- case 2:
216
- removeData = _a.sent();
217
- success = {
218
- message: "Disabled LevelingSytem for serverId: ".concat(guildId, " Successfully."),
219
- results: removeData,
220
- };
221
- return [2 /*return*/, success];
222
- case 3:
223
- error_3 = _a.sent();
224
- console.log(error_3.message);
225
- throw new Error("Error: An error occurred while disbaling leveling system.");
226
- case 4: return [2 /*return*/];
227
- }
228
- });
229
- });
230
- };
231
- LevelingSystem.prototype.addXp = function (userId, guildId) {
232
- return __awaiter(this, void 0, void 0, function () {
233
- var fetchServer, userRecord, updatedUserRecord, levelUp, channelData, results, levelUpMessage, editedMessage, success, error_4;
234
- return __generator(this, function (_a) {
235
- switch (_a.label) {
236
- case 0:
237
- console.log(3)
238
-
239
- _a.trys.push([0, 10, , 11]);
240
- if (!guildId) {
241
- throw new Error('Error: GuildId parameter is required. Usage: addXp(userId, guildId).');
242
- }
243
- if (!userId) {
244
- throw new Error('Error: UserId parameter is required. Usage: addXp(userId, guildId).');
245
- }
246
- return [4 /*yield*/, (0, utils_1.find)(this.configCollection, { guildId: guildId })];
247
- case 1:
248
- fetchServer = _a.sent();
249
- if (!fetchServer) {
250
- return [2 /*return*/, "This server has no leveling system"];
251
- }
252
- return [4 /*yield*/, (0, utils_1.find)(this.levelsCollection, { userId: userId, guildId: guildId })];
253
- case 2:
254
- userRecord = _a.sent();
255
- updatedUserRecord = void 0;
256
- if (!!userRecord) return [3 /*break*/, 4];
257
- return [4 /*yield*/, (0, utils_1.updateData)(this.levelsCollection, { userId: userId, guildId: guildId }, {
258
- $set: {
259
- userId: userId,
260
- guildId: guildId,
261
- xpCount: fetchServer.XpCount * fetchServer.rate,
262
- level: 0,
263
- },
264
- })];
265
- case 3:
266
- console.log(2)
267
-
268
- updatedUserRecord = _a.sent();
269
- return [3 /*break*/, 9];
270
- case 4: return [4 /*yield*/, (0, utils_1.updateData)(this.levelsCollection, { userId: userId, guildId: guildId }, {
271
- $inc: { xpCount: fetchServer.XpCount * fetchServer.rate },
272
- })];
273
- case 5:
274
- updatedUserRecord = _a.sent();
275
- if (!(updatedUserRecord.data.xpCount >= fetchServer.levelsArray[fetchServer.levelsArray.length - 1].xpCount)) return [3 /*break*/, 7];
276
- return [4 /*yield*/, (0, utils_1.updateData)(this.levelsCollection, { userId: userId, guildId: guildId }, {
277
- $set: {
278
- xpCount: fetchServer.levelsArray[fetchServer.levelsArray.length - 1].xpCount,
279
- level: fetchServer.levelsArray[fetchServer.levelsArray.length - 1].level,
280
- },
281
- })];
282
- case 6:
283
- _a.sent();
284
- return [2 /*return*/, "".concat(userId, " reached maximum level")];
285
- case 7:
286
- levelUp = this.checkLevelUp(updatedUserRecord.data, fetchServer);
287
- channelData = fetchServer.channelId;
288
- if (!levelUp) return [3 /*break*/, 9];
289
- return [4 /*yield*/, (0, utils_1.updateData)(this.levelsCollection, { userId: userId, guildId: guildId }, {
290
- $set: { level: updatedUserRecord.data.level },
291
- })];
292
- case 8:
293
- console.log(1)
294
- results = _a.sent();
295
- levelUpMessage = fetchServer.levelUpMessage;
296
- editedMessage = levelUpMessage;
297
- if (editedMessage.includes('{user}')) {
298
- editedMessage = editedMessage.replace('{user}', "<@".concat(userId, ">"));
299
- }
300
- if (editedMessage.includes('{level}')) {
301
- editedMessage = editedMessage.replace('{level}', results.data.level);
302
- }
303
- this.emit('levelup', editedMessage, userId, results.data.level, results.data.xpCount, channelData, updatedUserRecord.data.guildId);
304
- _a.label = 9;
305
- case 9:
306
- success = {
307
- message: 'User gained xp.',
308
- newXp: updatedUserRecord.data.xpCount,
309
- newLevel: updatedUserRecord.data.level,
310
- data: updatedUserRecord.data,
311
- };
312
- return [2 /*return*/, success];
313
- case 10:
314
- error_4 = _a.sent();
315
- this.emit('error', error_4.message);
316
- throw error_4;
317
- case 11: return [2 /*return*/];
318
- }
319
- });
320
- });
321
- };
322
- LevelingSystem.prototype.setUserBanner = function (guildId, userId, bannerURL) {
323
- return __awaiter(this, void 0, void 0, function () {
324
- var saveData, error_5;
325
- return __generator(this, function (_a) {
326
- switch (_a.label) {
327
- case 0:
328
- _a.trys.push([0, 2, , 3]);
329
- if (!guildId) {
330
- throw new Error('Error: GuildId parameter is required. Usage: setUserBanner(guildId, userId, bannerURL).');
331
- }
332
- if (!userId) {
333
- throw new Error('Error: UserId parameter is required. Usage: setUserBanner(guildId, userId, bannerURL).');
334
- }
335
- if (!bannerURL || !bannerURL.startsWith('http')) {
336
- throw new Error('Error: BannerURL parameter is required. Usage: setUserBanner(guildId, userId, bannerURL).');
337
- }
338
- return [4 /*yield*/, (0, utils_1.updateData)(this.levelsCollection, { userId: userId, guildId: guildId }, { $set: { bannerURL: bannerURL } })];
339
- case 1:
340
- saveData = _a.sent();
341
- return [2 /*return*/, saveData.data];
342
- case 2:
343
- error_5 = _a.sent();
344
- console.error(error_5.message);
345
- throw new Error("Error: An error occurred while setting user banner.");
346
- case 3: return [2 /*return*/];
347
- }
348
- });
349
- });
350
- };
351
- LevelingSystem.prototype.updateUserLevel = function (userId, guildId, fetchServer) {
352
- return __awaiter(this, void 0, void 0, function () {
353
- var userRecord, userXp, updatedLevel, _i, _a, levelInfo, error_6;
354
- return __generator(this, function (_b) {
355
- switch (_b.label) {
356
- case 0:
357
- _b.trys.push([0, 3, , 4]);
358
- return [4 /*yield*/, (0, utils_1.find)(this.levelsCollection, { userId: userId, guildId: guildId })];
359
- case 1:
360
- userRecord = _b.sent();
361
- if (!userRecord) {
362
- console.error("User record not found for userId: ".concat(userId, ", guildId: ").concat(guildId));
363
- return [2 /*return*/];
364
- }
365
- userXp = userRecord.xpCount;
366
- updatedLevel = 0;
367
- for (_i = 0, _a = fetchServer.levelsArray; _i < _a.length; _i++) {
368
- levelInfo = _a[_i];
369
- if (userXp >= levelInfo.xpCount) {
370
- updatedLevel = levelInfo.level;
371
- }
372
- else {
373
- break;
374
- }
375
- }
376
- return [4 /*yield*/, (0, utils_1.updateData)(this.levelsCollection, { userId: userId, guildId: guildId }, {
377
- $set: { level: updatedLevel },
378
- })];
379
- case 2:
380
- _b.sent();
381
- return [2 /*return*/, updatedLevel];
382
- case 3:
383
- error_6 = _b.sent();
384
- console.error("Error updating user level:", error_6.message);
385
- throw error_6;
386
- case 4: return [2 /*return*/];
387
- }
388
- });
389
- });
390
- };
391
- LevelingSystem.prototype.checkLevelUp = function (userRecord, fetchServer) {
392
- console.log(5)
393
-
394
- var level = fetchServer.levelsArray.length - 1;
395
- while (level >= 0 && userRecord.xpCount <= fetchServer.levelsArray[level].xpCount) {
396
- level--;
397
- }
398
- if (level < 0) {
399
- console.error("Error: Invalid level data for user with xpCount: ".concat(userRecord.xpCount));
400
- this.emit('error', "Invalid level data for user with xpCount: ".concat(userRecord.xpCount));
401
- return false;
402
- }
403
- console.log(6)
404
-
405
- var nextLevelData = fetchServer.levelsArray[level];
406
- if (userRecord.level < nextLevelData.level) {
407
- userRecord.level = nextLevelData.level;
408
- return true;
409
- }
410
- return false;
411
- };
412
-
413
- LevelingSystem.prototype.userInfo = function (userId, guildId) {
414
- return __awaiter(this, void 0, void 0, function () {
415
- var userRecord, error_7;
416
- return __generator(this, function (_a) {
417
- switch (_a.label) {
418
- case 0:
419
- _a.trys.push([0, 2, , 3]);
420
- return [4 /*yield*/, (0, utils_1.find)(this.levelsCollection, { userId: userId, guildId: guildId })];
421
- case 1:
422
- userRecord = _a.sent();
423
- if (!userRecord) {
424
- return [2 /*return*/, 'No data found for this user.'];
425
- }
426
- return [2 /*return*/, {
427
- userId: userRecord.userId,
428
- XpCount: userRecord.xpCount,
429
- Level: userRecord.level,
430
- bannerURL: userRecord.bannerURL || "user doesn't have custom banner url",
431
- }];
432
- case 2:
433
- error_7 = _a.sent();
434
- console.error('Error in userInfo:', error_7);
435
- this.emit('error', error_7.message);
436
- return [2 /*return*/, null];
437
- case 3: return [2 /*return*/];
438
- }
439
- });
440
- });
441
- };
442
- LevelingSystem.prototype.checkConfig = function (guildId) {
443
- return __awaiter(this, void 0, void 0, function () {
444
- var configData, error_8;
445
- return __generator(this, function (_a) {
446
- switch (_a.label) {
447
- case 0:
448
- _a.trys.push([0, 2, , 3]);
449
- return [4 /*yield*/, (0, utils_1.find)(this.configCollection, { guildId: guildId })];
450
- case 1:
451
- configData = _a.sent();
452
- if (!configData) {
453
- return [2 /*return*/, 'No config data found for this server.'];
454
- }
455
- return [2 /*return*/, {
456
- XpCount: configData.XpCount,
457
- rate: configData.rate,
458
- channelId: configData.channelId,
459
- guildId: configData.guildId,
460
- levelsArray: configData.levelsArray,
461
- }];
462
- case 2:
463
- error_8 = _a.sent();
464
- console.error('Error in checkConfig:', error_8);
465
- this.emit('error', error_8.message);
466
- return [2 /*return*/, null];
467
- case 3: return [2 /*return*/];
468
- }
469
- });
470
- });
471
- };
472
- LevelingSystem.prototype.serverLeaderBoard = function (guildId, topUsers) {
473
- if (topUsers === void 0) { topUsers = 10; }
474
- return __awaiter(this, void 0, void 0, function () {
475
- var collectionFilters, search, usersList, sortedUsersList, topUsersData, transformedData, error_9;
476
- return __generator(this, function (_a) {
477
- switch (_a.label) {
478
- case 0:
479
- _a.trys.push([0, 2, , 3]);
480
- if (!guildId) {
481
- throw new Error("Error: GuildId must be provided in the parameter serverLeaderBoard(guildId).");
482
- }
483
- if (topUsers === 0 || topUsers < -1) {
484
- throw new Error('Error: serverLeaderBoard cant display 0 members.');
485
- }
486
- collectionFilters = [
487
- { collectionName: this.levelsCollection, displayment: topUsers, filter: { guildId: guildId } }
488
- ];
489
- return [4 /*yield*/, (0, utils_1.searchMany)(collectionFilters)];
490
- case 1:
491
- search = _a.sent();
492
- usersList = search.levelsCollection;
493
- if (!usersList) {
494
- console.log('No config data found for this server.');
495
- return [2 /*return*/, 'No config data found for this server.'];
496
- }
497
- if (!Array.isArray(usersList)) {
498
- console.error('Error in serverLeaderBoard: usersList is not an array');
499
- return [2 /*return*/, []];
500
- }
501
- sortedUsersList = usersList.sort(function (a, b) { return b.xpCount - a.xpCount; });
502
- topUsersData = void 0;
503
- if (topUsers === -1) {
504
- topUsersData = sortedUsersList;
505
- }
506
- else {
507
- topUsersData = sortedUsersList.slice(0, topUsers);
508
- }
509
- transformedData = topUsersData.map(function (user) { return ({
510
- userId: user.userId,
511
- xpCount: user.xpCount,
512
- level: user.level,
513
- guildId: user.guildId
514
- }); });
515
- return [2 /*return*/, transformedData];
516
- case 2:
517
- error_9 = _a.sent();
518
- console.error('Error in serverLeaderBoard:', error_9);
519
- this.emit('error', error_9.message);
520
- return [2 /*return*/, []];
521
- case 3: return [2 /*return*/];
522
- }
523
- });
524
- });
525
- };
526
- LevelingSystem.prototype.topGlobal = function (topUsers) {
527
- if (topUsers === void 0) { topUsers = 10; }
528
- return __awaiter(this, void 0, void 0, function () {
529
- var collectionFilters, search, usersList, sortedUsersList, topUsersData, transformedData, error_10;
530
- return __generator(this, function (_a) {
531
- switch (_a.label) {
532
- case 0:
533
- _a.trys.push([0, 2, , 3]);
534
- if (topUsers === 0 || topUsers < -1) {
535
- throw new Error('Error: leaderBoard cant display 0 members.');
536
- }
537
- collectionFilters = [
538
- { collectionName: this.levelsCollection, displayment: topUsers, filter: {} }
539
- ];
540
- return [4 /*yield*/, (0, utils_1.searchMany)(collectionFilters)];
541
- case 1:
542
- search = _a.sent();
543
- usersList = search.levelsCollection;
544
- if (!usersList) {
545
- return [2 /*return*/, 'No config data found for this server.'];
546
- }
547
- if (!Array.isArray(usersList)) {
548
- console.error('Error in leaderBoard: usersList is not an array');
549
- return [2 /*return*/, []];
550
- }
551
- sortedUsersList = usersList.sort(function (a, b) { return b.xpCount - a.xpCount; });
552
- topUsersData = void 0;
553
- if (topUsers === -1) {
554
- topUsersData = sortedUsersList;
555
- }
556
- else {
557
- topUsersData = sortedUsersList.slice(0, topUsers);
558
- }
559
- transformedData = topUsersData.map(function (user) { return ({
560
- userId: user.userId,
561
- XpCount: user.xpCount,
562
- Level: user.level,
563
- }); });
564
- return [2 /*return*/, transformedData];
565
- case 2:
566
- error_10 = _a.sent();
567
- console.error('Error in leaderBoard:', error_10);
568
- this.emit('error', error_10.message);
569
- return [2 /*return*/, []];
570
- case 3: return [2 /*return*/];
571
- }
572
- });
573
- });
574
- };
575
- LevelingSystem.prototype.levelUpCard = function (message, userId, guildId, options) {
576
- if (options === void 0) { options = {}; }
577
- return __awaiter(this, void 0, void 0, function () {
578
- var userRecord, attachment, error_11;
579
- return __generator(this, function (_a) {
580
- switch (_a.label) {
581
- case 0:
582
- if (!message) {
583
- throw new Error('Error: Please define message paramter in userCard(message, userId)');
584
- }
585
- if (!userId) {
586
- throw new Error('Error: Please define userId paramter in userCard(message, userId)');
587
- }
588
- _a.label = 1;
589
- case 1:
590
- _a.trys.push([1, 4, , 5]);
591
- return [4 /*yield*/, (0, utils_1.find)(this.levelsCollection, { userId: userId, guildId: guildId })];
592
- case 2:
593
- userRecord = _a.sent();
594
- if (!userRecord) {
595
- return [2 /*return*/, 'No data found for this user.'];
596
- }
597
- return [4 /*yield*/, (0, levels_card_1.levelCard)(message, userRecord)];
598
- case 3:
599
- attachment = _a.sent();
600
- return [2 /*return*/, attachment];
601
- case 4:
602
- error_11 = _a.sent();
603
- console.error(error_11.message);
604
- throw error_11;
605
- case 5: return [2 /*return*/];
606
- }
607
- });
608
- });
609
- };
610
- LevelingSystem.prototype.serverLeaderBoardCard = function (message, guildId, version, colorHex) {
611
- if (version === void 0) { version = 1; }
612
- if (colorHex === void 0) { colorHex = 'random'; }
613
- return __awaiter(this, void 0, void 0, function () {
614
- var fetchServer, leaderBoardData, card, error_12;
615
- return __generator(this, function (_a) {
616
- switch (_a.label) {
617
- case 0:
618
- if (!guildId) {
619
- throw new Error("Error: Message must be provided in the parameter leaderBoardCard(message, guildId).");
620
- }
621
- if (!guildId) {
622
- throw new Error("Error: GuildId must be provided in the parameter leaderBoardCard(message, guildId).");
623
- }
624
- _a.label = 1;
625
- case 1:
626
- _a.trys.push([1, 5, , 6]);
627
- return [4 /*yield*/, (0, utils_1.find)(this.configCollection, { guildId: guildId })];
628
- case 2:
629
- fetchServer = _a.sent();
630
- if (!fetchServer) {
631
- return [2 /*return*/, "This server has no leveling system"];
632
- }
633
- return [4 /*yield*/, this.serverLeaderBoard(guildId, 5)];
634
- case 3:
635
- leaderBoardData = _a.sent();
636
- return [4 /*yield*/, (0, levels_card_1.leaderBoard)(message, leaderBoardData, version, colorHex)];
637
- case 4:
638
- card = _a.sent();
639
- return [2 /*return*/, card];
640
- case 5:
641
- error_12 = _a.sent();
642
- console.error(error_12.message);
643
- return [3 /*break*/, 6];
644
- case 6: return [2 /*return*/];
645
- }
646
- });
647
- });
648
- };
649
- LevelingSystem.prototype.editXp = function (guildId, userId, xpAmount) {
650
- return __awaiter(this, void 0, void 0, function () {
651
- var search, searchServer, channelId, prevLevel, update, levelUp, results, fetchServer, levelUpMessage, editedMessage, success, error_13;
652
- return __generator(this, function (_a) {
653
- switch (_a.label) {
654
- case 0:
655
- _a.trys.push([0, 8, , 9]);
656
- if (!guildId) {
657
- throw new Error("Error: GuildId isn't provided. Please use editXp(guildId, userId, xpAmount).");
658
- }
659
- if (!userId) {
660
- throw new Error("Error: UserId isn't provided. Please use editXp(guildId, userId, xpAmount).");
661
- }
662
- if (!xpAmount || typeof xpAmount !== 'number') {
663
- throw new Error('Error: Please xpAmount editXp(guidId, userId, xpAmount). xpAmount must be a number.');
664
- }
665
- return [4 /*yield*/, (0, utils_1.find)(this.levelsCollection, { guildId: guildId, userId: userId })];
666
- case 1:
667
- search = _a.sent();
668
- if (!search) {
669
- return [2 /*return*/, 'This user has no leveling data'];
670
- }
671
- return [4 /*yield*/, (0, utils_1.find)(this.configCollection, { guildId: guildId })];
672
- case 2:
673
- searchServer = _a.sent();
674
- channelId = searchServer.channelId;
675
- prevLevel = search.level;
676
- return [4 /*yield*/, (0, utils_1.updateData)(this.levelsCollection, { guildId: guildId, userId: userId }, { $inc: { xpCount: xpAmount } })];
677
- case 3:
678
- update = _a.sent();
679
- return [4 /*yield*/, this.updateUserLevel(userId, guildId, searchServer)];
680
- case 4:
681
- levelUp = _a.sent();
682
- if (!(levelUp > prevLevel)) return [3 /*break*/, 7];
683
- return [4 /*yield*/, (0, utils_1.updateData)(this.levelsCollection, { userId: userId, guildId: guildId }, {
684
- $set: { level: update.data.level },
685
- })];
686
- case 5:
687
- results = _a.sent();
688
- return [4 /*yield*/, (0, utils_1.find)(this.configCollection, { guildId: guildId })];
689
- case 6:
690
- fetchServer = _a.sent();
691
- levelUpMessage = fetchServer.levelUpMessage;
692
- editedMessage = levelUpMessage;
693
- if (editedMessage.includes('{user}')) {
694
- editedMessage = editedMessage.replace('{user}', "<@".concat(userId, ">"));
695
- }
696
- if (editedMessage.includes('{level}')) {
697
- editedMessage = editedMessage.replace('{level}', results.data.level);
698
- }
699
- this.emit('levelup', editedMessage, userId, results.data.level, results.data.xpCount, channelId, results.data.guildId);
700
- _a.label = 7;
701
- case 7:
702
- success = {
703
- message: 'Updated user Xp successfully',
704
- newXp: update.data.xpCount,
705
- newLevel: levelUp,
706
- };
707
- return [2 /*return*/, success];
708
- case 8:
709
- error_13 = _a.sent();
710
- console.error("Error editing XP:", error_13.message);
711
- throw new Error("Failed to update user XP");
712
- case 9: return [2 /*return*/];
713
- }
714
- });
715
- });
716
- };
717
- LevelingSystem.prototype.removeUser = function (guildId, userId) {
718
- return __awaiter(this, void 0, void 0, function () {
719
- var search, deleteUser, success, error_14;
720
- return __generator(this, function (_a) {
721
- switch (_a.label) {
722
- case 0:
723
- _a.trys.push([0, 3, , 4]);
724
- if (!guildId) {
725
- throw new Error("Error: GuildId isn't provided. Please use removeUser(guildId, userId).");
726
- }
727
- if (!userId) {
728
- throw new Error("Error: UserId isn't provided. Please use removeUser(guildId, userId).");
729
- }
730
- return [4 /*yield*/, (0, utils_1.find)(this.levelsCollection, { guildId: guildId, userId: userId })];
731
- case 1:
732
- search = _a.sent();
733
- if (!search) {
734
- return [2 /*return*/, 'This user has no leveling data'];
735
- }
736
- return [4 /*yield*/, (0, utils_1.remove)(this.levelsCollection, { guildId: guildId, userId: userId })];
737
- case 2:
738
- deleteUser = _a.sent();
739
- success = {
740
- message: 'User removed successfully',
741
- results: deleteUser,
742
- };
743
- return [2 /*return*/, success];
744
- case 3:
745
- error_14 = _a.sent();
746
- console.error('Error while removing user from data due to:', error_14.message);
747
- throw new Error("Couldn't remove user from data.");
748
- case 4: return [2 /*return*/];
749
- }
750
- });
751
- });
752
- };
753
- LevelingSystem.prototype.liveServerLeaderboard = function (context, guildId, channelId, timer, version, colorHex) {
754
- if (timer === void 0) { timer = 10000; }
755
- if (version === void 0) { version = 1; }
756
- return __awaiter(this, void 0, void 0, function () {
757
- var fetchServer, existingConfig, card, channel, liveLeaderboardMessageId, existingMessage, _a, newMessage, error_15;
758
- var _this = this;
759
- return __generator(this, function (_b) {
760
- switch (_b.label) {
761
- case 0:
762
- _b.trys.push([0, 15, , 16]);
763
- if (!guildId) {
764
- throw new Error("Error: GuildId isn't provided. Please use liveServerLeaderboard(client, guildId, channelId).");
765
- }
766
- if (!channelId) {
767
- throw new Error("Error: ChannelId isn't provided. Please use liveServerLeaderboard(client, guildId, channelId).");
768
- }
769
- return [4 /*yield*/, (0, utils_1.find)(this.configCollection, { guildId: guildId })];
770
- case 1:
771
- fetchServer = _b.sent();
772
- if (!fetchServer) {
773
- return [2 /*return*/, "This server has no leveling system"];
774
- }
775
- return [4 /*yield*/, (0, utils_1.find)('liveLeaderBoard', { guildId: guildId, channelId: channelId })];
776
- case 2:
777
- existingConfig = _b.sent();
778
- return [4 /*yield*/, (0, utils_1.updateData)('liveLeaderBoard', { guildId: guildId, channelId: channelId }, { $set: { guildId: guildId, channelId: channelId, version: version } })];
779
- case 3:
780
- _b.sent();
781
- return [4 /*yield*/, this.serverLeaderBoardCard(context.client, guildId, version, colorHex)];
782
- case 4:
783
- card = _b.sent();
784
- channel = context.client.channels.cache.get(channelId);
785
- if (!channel) return [3 /*break*/, 13];
786
- liveLeaderboardMessageId = existingConfig === null || existingConfig === void 0 ? void 0 : existingConfig.messageId;
787
- if (!liveLeaderboardMessageId) return [3 /*break*/, 6];
788
- return [4 /*yield*/, channel.messages.fetch(liveLeaderboardMessageId).catch(function () { return null; })];
789
- case 5:
790
- _a = _b.sent();
791
- return [3 /*break*/, 7];
792
- case 6:
793
- _a = null;
794
- _b.label = 7;
795
- case 7:
796
- existingMessage = _a;
797
- if (!existingMessage) return [3 /*break*/, 9];
798
- return [4 /*yield*/, existingMessage.edit({ content: '# LIVE SERVER LEADERBOARD.', files: [card] })];
799
- case 8:
800
- _b.sent();
801
- return [3 /*break*/, 12];
802
- case 9: return [4 /*yield*/, channel.send({ content: '# LIVE SERVER LEADERBOARD.', files: [card] })];
803
- case 10:
804
- newMessage = _b.sent();
805
- return [4 /*yield*/, (0, utils_1.updateData)('liveLeaderBoard', { guildId: guildId, channelId: channelId }, { $set: { messageId: newMessage.id } })];
806
- case 11:
807
- _b.sent();
808
- _b.label = 12;
809
- case 12: return [3 /*break*/, 14];
810
- case 13:
811
- console.error('Channel not found');
812
- _b.label = 14;
813
- case 14:
814
- setTimeout(function () {
815
- _this.liveServerLeaderboard(context, guildId, channelId, timer, version, colorHex);
816
- }, timer);
817
- return [3 /*break*/, 16];
818
- case 15:
819
- error_15 = _b.sent();
820
- console.error('Error:', error_15.message);
821
- return [3 /*break*/, 16];
822
- case 16: return [2 /*return*/];
823
- }
824
- });
825
- });
826
- };
827
- return LevelingSystem;
828
- }(events_1.EventEmitter));
829
- exports.LevelingSystem = LevelingSystem;