HiddenPhox/src/modules/codePreviews.js

106 lines
2.8 KiB
JavaScript
Raw Normal View History

2022-05-07 22:49:07 +00:00
const events = require("../lib/events.js");
const {hasFlag} = require("../lib/guildSettings.js");
2022-05-07 22:58:15 +00:00
const fetch = require("node-fetch");
2022-05-07 22:49:07 +00:00
const REGEX_GITHUB =
/(?:\s|^)https?:\/\/(www\.)?github\.com\/.+?\/.+?\/blob\/([a-zA-Z0-9-_.#/]*)/g;
const REGEX_GITLAB =
/(?:\s|^)https?:\/\/(www\.)?gitlab\.com\/.+?\/.+?\/-\/blob\/([a-zA-Z0-9-_.#/]*)/g;
const REGEX_GITEA =
/(?:\s|^)https?:\/\/.+?\/.+?\/.+?\/src\/branch\/([a-zA-Z0-9-_.#/]*)/g;
function unindent(str) {
str = str.replace(/\t/g, " ");
const minIndent =
str
.match(/^ *(?=\S)/gm)
?.reduce((prev, curr) => Math.min(prev, curr.length), Infinity) ?? 0;
if (!minIndent) return str;
return str.replace(new RegExp(`^ {${minIndent}}`, "gm"), "");
}
events.add("messageCreate", "codePreviews", async function (msg) {
if (!msg.guildID) return;
if (!hasFlag(msg.guildID, "codePreviews")) return;
let out = "";
const githubLinks = msg.content.match(REGEX_GITHUB);
const gitlabLinks = msg.content.match(REGEX_GITLAB);
const giteaLinks = msg.content.match(REGEX_GITEA);
2022-05-07 22:51:27 +00:00
if (githubLinks?.length) {
for (const link of githubLinks) {
const rawLink = link.replace("/blob/", "/raw/");
const file = await fetch(rawLink).then((res) => res.text());
const lines = file.replace(/\r/g, "").split("\n");
2022-05-07 22:49:07 +00:00
2022-05-07 22:51:27 +00:00
const fileName = link.substring(
link.lastIndexOf("/") + 1,
link.indexOf("#") == -1 ? link.length : link.indexOf("#")
);
const fileType =
fileName.indexOf(".") == -1
? ""
: fileName.substring(fileName.indexOf(".") + 1);
2022-05-07 22:49:07 +00:00
2022-05-07 23:06:31 +00:00
const lineStr = link.match(/#L\d+(-L\d+)?/)?.[0];
2022-05-07 22:51:27 +00:00
let startLine, endLine;
let entireFile = false;
2022-05-07 22:49:07 +00:00
2022-05-07 22:51:27 +00:00
if (lineStr) {
2022-05-07 23:06:31 +00:00
let [start, end] = link.match(/L\d+/g);
2022-05-07 22:51:27 +00:00
start = parseInt(start.replace("L", ""));
if (!end) {
startLine = endLine = start;
} else {
end = parseInt(end.replace("L", ""));
startLine = start;
endLine = end;
}
2022-05-07 22:49:07 +00:00
} else {
2022-05-07 22:51:27 +00:00
entireFile = true;
startLine = 1;
endLine = lines.length;
2022-05-07 22:49:07 +00:00
}
2022-05-07 22:51:27 +00:00
const whichLines = entireFile
? ""
: startLine == endLine
? "Line " + startLine
: "Lines " + startLine + "-" + endLine;
2022-05-07 22:49:07 +00:00
2022-05-07 22:51:27 +00:00
startLine--;
endLine--;
2022-05-07 22:49:07 +00:00
2022-05-07 22:51:27 +00:00
const targetLines = (
2022-05-07 23:02:34 +00:00
entireFile ? lines : lines.slice(startLine, endLine + 1)
2022-05-07 22:51:27 +00:00
).join("\n");
2022-05-07 22:49:07 +00:00
2022-05-07 22:51:27 +00:00
out +=
"**" +
fileName +
": **" +
whichLines +
"\n```" +
fileType +
"\n" +
unindent(targetLines) +
"\n```\n";
}
2022-05-07 22:49:07 +00:00
}
2022-05-07 23:10:31 +00:00
if (out !== "") {
await msg.edit({flags: 1 << 2});
await msg.channel.createMessage({
2022-05-07 22:49:07 +00:00
content: out,
allowedMentions: {
repliedUser: false,
},
messageReference: {
messageID: msg.id,
},
});
2022-05-07 23:10:31 +00:00
}
2022-05-07 22:49:07 +00:00
});