Skip to content

Commit 72b3f34

Browse files
committed
feat: makes run command feel nicer without those complex cmd
1 parent 0d7ed7d commit 72b3f34

1 file changed

Lines changed: 25 additions & 17 deletions

File tree

src/commands/run.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Message } from "../../types/message"
1+
import { Message } from "../../types/message";
22
import log from "../components/utils/log";
33
import { exec } from "child_process";
44
import util from "util";
@@ -7,53 +7,61 @@ import fs from "fs/promises";
77
export const info = {
88
command: "run",
99
description: "Run a code snippet in a specified programming language.",
10-
usage: "run <language>\n<code>",
11-
example: "run python\nprint('Hello, World!')",
10+
usage: "run <language>",
11+
example: "run py",
1212
role: "admin",
1313
cooldown: 5000,
1414
};
1515

1616
export default async function (msg: Message) {
17-
const body = msg.body.trim();
18-
const match = body.match(/^run\s+(python|java|c|js|php)\s*\r?\n([\s\S]+)/i);
17+
const query = msg.body.replace(/^run\b\s*/i, "").trim();
18+
if (query.length !== 0) {
19+
if (!/^(py|java|c|js|php)$/i.test(query)) {
20+
await msg.reply(
21+
"Invalid argument. Please use one of the following:\n\npy, java, c, js or php",
22+
);
23+
return;
24+
}
25+
}
1926

20-
if (!match) {
21-
await msg.reply(
22-
"Please use the format:\n\nrun python\n<code>\n\nor\n\nrun java\n<code>"
23-
);
27+
if (!msg.hasQuotedMsg) {
28+
await msg.reply("This only works on qouted messages.");
2429
return;
2530
}
2631

27-
const lang = match[1].toLowerCase();
28-
const code = match[2];
32+
const qouted = await msg.getQuotedMessage();
33+
qouted.body = qouted.body
34+
.normalize("NFKC")
35+
.replace(/[\u0300-\u036f\u00b4\u0060\u005e\u007e]/g, "")
36+
.trim();
2937

3038
let command: string;
3139
let tempFile: string;
3240

3341
const tempDir = "./.temp";
3442
await fs.mkdir(tempDir, { recursive: true });
3543

36-
if (lang === "python") {
44+
if (query === "python") {
3745
tempFile = `${tempDir}/run.py`;
3846
command = `python3 "${tempFile}"`;
39-
} else if (lang === "java") {
47+
} else if (query === "java") {
4048
tempFile = `${tempDir}/run.java`;
4149
command = `javac "${tempFile}" && java -cp /tmp Code`;
42-
} else if (lang === "c") {
50+
} else if (query === "c") {
4351
tempFile = `${tempDir}/run.c`;
4452
command = `gcc "${tempFile}" -o /tmp/code && /tmp/code`;
45-
} else if (lang === "js") {
53+
} else if (query === "js") {
4654
tempFile = `${tempDir}/code.js`;
4755
command = `node "${tempFile}"`;
48-
} else if (lang === "php") {
56+
} else if (query === "php") {
4957
tempFile = `${tempDir}/run.php`;
5058
command = `php "${tempFile}"`;
5159
} else {
5260
await msg.reply("Unsupported language.");
5361
return;
5462
}
5563

56-
await fs.writeFile(tempFile, code);
64+
await fs.writeFile(tempFile, qouted.body);
5765

5866
const execPromise = util.promisify(exec);
5967

0 commit comments

Comments
 (0)