eval/index.ts

61 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-12-18 07:51:46 -05:00
import { ApplicationCommandInputType, ApplicationCommandOptionType, ApplicationCommandType, sendBotMessage } from "@api/Commands";
import { Devs } from "@utils/constants";
import { getCurrentChannel, sendMessage } from "@utils/discord";
import definePlugin from "@utils/types";
export default definePlugin({
name: "Eval",
description: "Adds a / command to evaluate JavaScript on your client",
authors: [Devs.nin0dev],
commands: [{
name: "eval",
description: "Evaluate JavaScript in your client (USE WITH EXTREME CAUTION)",
async execute(args, ctx) {
const console: any = {
_lines: [] as string[],
_log(...things: string[]) {
this._lines.push(
...things
.join(" ")
.split("\n")
);
}
};
console.log = console.error = console.warn = console.info = console._log.bind(console);
let script = args[0].value.replace(/(^`{3}(js|javascript)?|`{3}$)/g, "");
if (script.includes("await")) script = `(async () => { ${script} })()`;
try {
var result = await eval(script);
} catch (e: any) {
var result = e;
}
if (args[1] && args[1].value) {
sendMessage(getCurrentChannel()!.id, {
content: "```js\n" + result + "\n```"
});
}
else {
sendBotMessage(getCurrentChannel()!.id, {
content: "```js\n" + result + "\n```"
});
}
},
type: ApplicationCommandType.CHAT_INPUT,
inputType: ApplicationCommandInputType.BUILT_IN,
options: [{
name: "code",
description: "The code to run",
required: true,
type: ApplicationCommandOptionType.STRING
}, {
name: "send",
description: "Send the output in chat (default to false)",
type: ApplicationCommandOptionType.BOOLEAN,
required: false
}]
}]
});