-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (141 loc) · 5.13 KB
/
index.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* Vizality Related Items */
const { Plugin } = require("@vizality/entities");
const { getModule } = require("@vizality/webpack");
/* Eval command specific */
const { inspect } = require("util");
const {
functions: { isPromise, type, parse },
} = require("./util");
const { Script, createContext } = require("vm");
/* Settings react file */
const Settings = require("./Settings.jsx");
module.exports = class EvalCommand extends Plugin {
start() {
// All of the settings. Looks ugly but idk how else to make this look "pretty"
const replace = this.settings.get("tokenReplacer", "[REDACTED]");
const format = this.settings.get(
"evalFormat",
"⏱️ Took {time}{n}🔍 Typeof {type}{n}{output}"
);
const safeEval = this.settings.get("safeEval", false);
const autoCompleteAmount = this.settings.get("autoCompleteAmount", 25);
const allowAutoComplete = this.settings.get("autoCompleteToggle", true);
// Eval command, of course
vizality.api.commands.registerCommand({
command: "eval",
aliases: ["evaluate"],
description: "Evaluates any JavaScript code",
usage: "{c} [ code ] [ -depth=<number> ]",
category: "Util",
async executor(args) {
// Check for arguments
if (!args.length)
return {
send: false,
result: `Invalid usage! Valid Usage: \`${this.usage.replace(
"{c}",
vizality.api.commands.prefix + this.command
)}\``,
};
// Parse flags and phrases
const { flags, phrases } = parse(
args.join(" ").replace(/```js/, "").replace(/```/, "").trim()
);
// @TODO: fix issue: https://github.com/Sxmurai/eval-plugin/issues/3
if (!this.messages.includes(phrases.join(" ")) && allowAutoComplete)
this.messages.unshift(phrases.join(" "));
// Check for depth.
let depth = flags.get("depth") || 0;
if (isNaN(depth)) depth = 0;
// Try/catch for error handling.
try {
if (safeEval) {
let hrtime = process.hrtime();
const evaluated = new Script(phrases.join(" ")).runInContext(
createContext()
);
hrtime = process.hrtime(hrtime);
const inspected = inspect(evaluated, false, depth);
return {
send: false,
result: format
.replace(
new RegExp("{time}", "gi"),
`${hrtime[0] > 0 ? `${hrtime[0]}s ` : ""}${
hrtime[1] / 1000000
}ms`
)
.replace(new RegExp("{type}", "gi"), type(evaluated))
.replace(
new RegExp(`{output}`, "gi"),
`\`\`\`js\n${inspected.toString().substring(0, 1950)}\`\`\``
)
.replace(new RegExp("{n}", "gi"), "\n")
.replace(
new RegExp("{input}", "gi"),
`\`\`\`js\n${phrases.join(" ")}\`\`\``
)
.substring(0, 1999),
};
}
const start = process.hrtime();
let toEval = eval(phrases.join(" "));
let hr = process.hrtime(start);
if (isPromise(toEval)) {
toEval = await toEval;
hr = process.hrtime(start);
}
let returned = inspect(toEval, false, depth).toString().replace(getModule("getToken").getToken(), replace);
return {
send: false,
result: format
.replace(
new RegExp("{time}", "gi"),
`${hr[0] > 0 ? `${hr[0]}s ` : ""}${hr[1] / 1000000}ms`
)
.replace(new RegExp("{type}", "gi"), type(toEval))
.replace(
new RegExp(`{output}`, "gi"),
`\`\`\`js\n${returned.toString().substring(0, 1950)}\`\`\``
)
.replace(new RegExp("{n}", "gi"), "\n")
.replace(
new RegExp("{input}", "gi"),
`\`\`\`js\n${phrases.join(" ")}\`\`\``
)
.replace(new RegExp(`{depth}`, "gi"), depth)
.substring(0, 1999),
};
} catch (error) {
return {
send: false,
result: `\`\`\`js\n${error}\`\`\``,
};
}
},
});
// @TODO: improve this garbage
if (allowAutoComplete) {
vizality.api.commands.commands["eval"].messages = [];
const messages = vizality.api.commands.commands["eval"].messages;
setInterval(() => {
if (messages.length > autoCompleteAmount)
messages.slice(0, autoCompleteAmount);
}, 5000);
vizality.api.commands.commands["eval"].autocomplete = (args) => {
if (!args || !args.length) return false;
return {
header: `Auto Complete`,
commands: messages
.filter((msg) =>
msg.toLowerCase().includes(args.join(" ").toLowerCase())
)
.map((key) => ({ command: key, description: "" })),
};
};
}
}
stop() {
vizality.api.commands.unregisterCommand("eval");
}
};