-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
getLeaderboardInfo.js
55 lines (47 loc) · 1.7 KB
/
getLeaderboardInfo.js
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
module.exports = async (d) => {
const data = d.util.aoiFunc(d);
if (data.err) return d.error(data.err);
const [variable, id, type = "guild", format, table = d.client.db.tables[0]] = data.inside.splits;
if (!d.client.variableManager.has(variable, table)) return d.aoiError.fnError(d, "custom", {}, `Variable "${variable}" Not Found`);
if (!type || (type.toLowerCase() !== "guild" && type.toLowerCase() !== "user" && type.toLowerCase() !== "global")) return d.aoiError.fnError(d, "custom", {}, `Invalid type, must be "guild", "user" or "global"`);
let key;
let user;
if (type === "guild") {
key = `${variable}_${id}`
user = await d.util.getGuild(d, id);
} else if (type === "user") {
key = `${variable}_${id}_${d.guild?.id}`
user = await d.util.getUser(d, id);
} else if (type === "global") {
key = `${variable}_${id}`
user = await d.util.getUser(d, id);
}
let db = await d.client.db.findMany(table, (data) => {
return data.key.startsWith(key.split("_")[0]) && (type === "guild" ? data.key.split("_")[2] === key.split("_")[2] : true)
});
switch (format) {
case "top":
data.result = db.sort((a, b) => b.value - a.value).findIndex((x) => x.key === key) + 1 || 0;
break;
case "value":
data.result = db.find(x => x.key === key)?.value || 0;
break;
case "id":
data.result = user.id || null;
break;
case "username":
data.result = user?.username || null;
break;
case "name":
data.result = user.name || null;
break;
case "tag":
data.result = user?.tag;
break;
default:
data.result = user?.username || user?.name || null;
}
return {
code: d.util.setCode(data),
};
};