/
findInCache.js
50 lines (45 loc) · 1.74 KB
/
findInCache.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
module.exports = async (d) => {
const data = d.util.aoiFunc(d);
if (data.err) return d.error(data.err);
let [type, name, prop, value, findType = "===", returnValue = "$default"] =
data.inside.splits;
prop = prop.trim() === "" ? "" : "?." + prop;
findType = ["includes", "startsWith", "endsWith"].includes(findType)
? findType
: [">=", "==", "===", "<=", "<", ">"].includes(findType)
? findType
: d.aoiError.fnError(
d,
"custom",
{inside: data.inside},
"Invalid FindType Provided In",
);
try {
if (["includes", "startsWith", "endsWith"].includes(findType)) {
data.result = eval(
`d.client.cacheManager.caches[type][name].find(x => (prop.trim() === "" ? x : x${prop})[findType]("${value}"))`,
);
data.result =
typeof data.result === "object"
? returnValue === "$default"
? JSON.stringify(data.result, null, 2)
: eval(`data.result?.${returnValue}`)
: data.result;
} else {
data.result =
eval(`d.client.cacheManager.caches[type][name].find(x => (prop.trim() === "" ? x : x${prop}) ${findType} "${value}")`);
data.result =
typeof data.result === "object"
? returnValue === "$default"
? JSON.stringify(data.result, null, 2)
: eval(`data.result?.${returnValue}`)
: data.result;
}
} catch (e) {
console.error(e);
data.result = "";
}
return {
code: d.util.setCode(data),
};
};