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