From 1e1f537dc6b7aa92ef44537f0fa3bb50ede50cbd Mon Sep 17 00:00:00 2001 From: EvolutionX Date: Thu, 25 Aug 2022 07:04:06 +0530 Subject: [PATCH 1/2] feat: anticrash and eval improvements --- src/commands/eval.ts | 74 +++++++++++++++++--------------- src/events/uncaughtException.ts | 10 +++++ src/events/unhandledRejection.ts | 10 +++++ src/index.ts | 5 ++- 4 files changed, 63 insertions(+), 36 deletions(-) create mode 100644 src/events/uncaughtException.ts create mode 100644 src/events/unhandledRejection.ts diff --git a/src/commands/eval.ts b/src/commands/eval.ts index 1b08576..7865f96 100644 --- a/src/commands/eval.ts +++ b/src/commands/eval.ts @@ -1,7 +1,7 @@ -import { CommandType, commandModule, Context } from "@sern/handler"; +import { commandModule, CommandType } from "@sern/handler"; import { Client, Collection, EmbedBuilder } from "discord.js"; -import { inspect } from "util"; import { fetch } from "undici"; +import { inspect } from "util"; import { ownerOnly } from "../plugins/ownerOnly.js"; import type { Data } from "./plugin.js"; @@ -11,8 +11,44 @@ export default commandModule({ plugins: [ownerOnly()], alias: ["ev"], execute: async (ctx, args) => { - const [type, code] = args; + let code: string[] | string = args[1]; + + code = code.join(" ") as string; + if (code.includes("await")) { + const ar = code.split(";"); + const last = ar.pop(); + code = `(async () => {\n${ar.join(";\n")}\nreturn ${ + last?.trim() ?? " " + }\n\n})();`; + } const { channel, guild, client, user, member, message: msg } = ctx; + if ( + ["TOKEN", "process.env", "token"].some((e) => code.includes(e)) && + ctx.user.id !== "697795666373640213" + ) + return ctx.message.react("❌"); + + let result: unknown | string; + + try { + result = eval(code); + } catch (error) { + result = error; + } + if (result instanceof Promise) result = await result; + if (typeof result !== "string") { + result = inspect(result, { + depth: 0, + }); + } + + result = "```js\n" + result + "\n```"; + + if ((result as string).length > 2000) { + channel!.send("Result is too long to send"); + } + + ctx.channel!.send({ content: result as string }); function send(id: string, ping: boolean = false) { const channel = client.channels.cache.get(id); @@ -30,40 +66,10 @@ export default commandModule({ ) .setFooter({ text: "Supports DJS v14.2 and above" }) .setTimestamp(); - const content = ping ? '@everyone' : null; + const content = ping ? "@everyone" : null; channel.isTextBased() && channel.send({ content, embeds: [embed] }); return "Done sir"; } - - if (type !== "text") return; - if ( - (code.join(" ").includes("send") || code.join(" ").includes("reply")) && - ctx.user.id !== "697795666373640213" - ) - return; - - if (code.join(" ").includes("process.env")) return; - if (code.join(" ").includes("token")) return; - let result: unknown | string; - - try { - result = eval(code.join(" ")); - } catch (error) { - result = error; - } - if (result instanceof Promise) result = await result; - if (typeof result !== "string") { - result = inspect(result, { - depth: 0, - }); - } - - result = "```js\n" + result + "\n```"; - - if ((result as string).length > 2000) { - channel!.send("Result is too long to send"); - } - ctx.channel!.send({ content: result as string }); }, }); diff --git a/src/events/uncaughtException.ts b/src/events/uncaughtException.ts new file mode 100644 index 0000000..9e3dd53 --- /dev/null +++ b/src/events/uncaughtException.ts @@ -0,0 +1,10 @@ +import { eventModule, EventType } from "@sern/handler"; + +export default eventModule({ + emitter: 'process', + name: 'uncaughtException', + type: EventType.External, + execute(r) { + console.log(r) + }, +}) \ No newline at end of file diff --git a/src/events/unhandledRejection.ts b/src/events/unhandledRejection.ts new file mode 100644 index 0000000..7171b52 --- /dev/null +++ b/src/events/unhandledRejection.ts @@ -0,0 +1,10 @@ +import { eventModule, EventType } from "@sern/handler"; + +export default eventModule({ + emitter: "process", + name: "unhandledRejection", + type: EventType.External, + execute(r) { + console.log(r); + }, +}); diff --git a/src/index.ts b/src/index.ts index dec3679..593709c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { Client, GatewayIntentBits, Partials, ActivityType } from "discord.js"; +import { ActivityType, Client, GatewayIntentBits, Partials } from "discord.js"; import { Sern, SernEmitter } from "@sern/handler"; import "dotenv/config"; @@ -13,6 +13,7 @@ const client = new Client({ partials: [Partials.GuildMember, Partials.GuildMember, Partials.Message], }); +Sern.addExternal(process); Sern.init({ client, sernEmitter: new SernEmitter(), @@ -29,4 +30,4 @@ client.once("ready", (client) => { console.log(`[✅]: Logged in as ${client.user.username}`); }); -client.login(); +await client.login(); From 57156ab6af684e4ab6b3aea5bf24890a0bee49b9 Mon Sep 17 00:00:00 2001 From: EvolutionX Date: Thu, 25 Aug 2022 08:45:24 +0530 Subject: [PATCH 2/2] chore: address requested changes --- src/commands/eval.ts | 2 +- src/events/uncaughtException.ts | 7 +++---- src/events/unhandledRejection.ts | 1 - 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/commands/eval.ts b/src/commands/eval.ts index 7865f96..1dc39f7 100644 --- a/src/commands/eval.ts +++ b/src/commands/eval.ts @@ -50,7 +50,7 @@ export default commandModule({ ctx.channel!.send({ content: result as string }); - function send(id: string, ping: boolean = false) { + function send(id: string, ping = false) { const channel = client.channels.cache.get(id); if (!channel) return; const embed = new EmbedBuilder() diff --git a/src/events/uncaughtException.ts b/src/events/uncaughtException.ts index 9e3dd53..bc94065 100644 --- a/src/events/uncaughtException.ts +++ b/src/events/uncaughtException.ts @@ -1,10 +1,9 @@ import { eventModule, EventType } from "@sern/handler"; export default eventModule({ - emitter: 'process', - name: 'uncaughtException', + emitter: "process", type: EventType.External, execute(r) { - console.log(r) + console.log(r); }, -}) \ No newline at end of file +}); diff --git a/src/events/unhandledRejection.ts b/src/events/unhandledRejection.ts index 7171b52..bc94065 100644 --- a/src/events/unhandledRejection.ts +++ b/src/events/unhandledRejection.ts @@ -2,7 +2,6 @@ import { eventModule, EventType } from "@sern/handler"; export default eventModule({ emitter: "process", - name: "unhandledRejection", type: EventType.External, execute(r) { console.log(r);