forked from onlyjasonhere/djs-selfbot-v9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeautify.js
54 lines (44 loc) · 1.37 KB
/
beautify.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
exports.run = async (client, msg, args) => {
let res;
try {
await msg.edit("Searching for code to beautify...");
res = await format(msg);
} catch(e) {
res = e;
}
return msg.edit(res);
};
exports.conf = {
enabled: true,
guildOnly: false,
aliases: ["pretty"]
};
exports.help = {
name: 'beautify',
description: 'Get the last JS code block and makes it *readable*',
usage: 'beautify [id (optional)]'
};
const { js_beautify } = require("js-beautify");
const reduceIndentation = (string) => {
let whitespace = string.match(/^(\s+)/);
if (!whitespace) return string;
whitespace = whitespace[0].replace("\n", "");
return string.split("\n").map(line => line.replace(whitespace, "")).join("\n");
};
const format = async (msg) => {
const messages = msg.channel.messages.array().reverse();
let code;
const codeRegex = /```(?:js|json|javascript)?\n?((?:\n|.)+?)\n?```/ig;
for (let m = 0; m < messages.length; m++) {
const message = messages[m];
const groups = codeRegex.exec(message.content);
if (groups && groups[1].length) {
code = groups[1];
break;
}
}
if (!code) throw new Error("No Javascript codeblock found.");
const beautifiedCode = js_beautify(code, { indent_size: 2, brace_style: "collapse", jslint_happy: true });
const str = await reduceIndentation(beautifiedCode);
return (`${"```js"}\n${str}\n${"```"}`);
};