wip code preview thing
This commit is contained in:
parent
002092bcfd
commit
98a3dfe26c
1 changed files with 100 additions and 0 deletions
100
src/modules/codePreviews.js
Normal file
100
src/modules/codePreviews.js
Normal file
|
@ -0,0 +1,100 @@
|
|||
const events = require("../lib/events.js");
|
||||
const {hasFlag} = require("../lib/guildSettings.js");
|
||||
|
||||
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);
|
||||
|
||||
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");
|
||||
|
||||
const fileName = link.substring(
|
||||
link.lastIndexOf("/") + 1,
|
||||
link.indexOf("#") == -1 ? link.length : link.indexOf("#")
|
||||
);
|
||||
const fileType =
|
||||
fileName.indexOf(".") == -1
|
||||
? ""
|
||||
: fileName.subString(fileName.indexOf(".") + 1);
|
||||
|
||||
const lineStr = link.match(/#L\d+?(-L\d+?)?/)[0];
|
||||
let startLine, endLine;
|
||||
let entireFile = false;
|
||||
|
||||
if (lineStr) {
|
||||
let [start, end] = link.match(/L\d+?/g);
|
||||
start = parseInt(start.replace("L", ""));
|
||||
if (!end) {
|
||||
startLine = endLine = start;
|
||||
} else {
|
||||
end = parseInt(end.replace("L", ""));
|
||||
startLine = start;
|
||||
endLine = end;
|
||||
}
|
||||
} else {
|
||||
entireFile = true;
|
||||
startLine = 1;
|
||||
endLine = lines.length;
|
||||
}
|
||||
|
||||
const whichLines = entireFile
|
||||
? ""
|
||||
: startLine == endLine
|
||||
? "Line " + startLine
|
||||
: "Lines " + startLine + "-" + endLine;
|
||||
|
||||
startLine--;
|
||||
endLine--;
|
||||
|
||||
const targetLines = (
|
||||
entireFile ? lines : lines.slice(startLine, endLine)
|
||||
).join("\n");
|
||||
|
||||
out +=
|
||||
"**" +
|
||||
fileName +
|
||||
": **" +
|
||||
whichLines +
|
||||
"\n```" +
|
||||
fileType +
|
||||
"\n" +
|
||||
unindent(targetLines) +
|
||||
"\n```\n";
|
||||
}
|
||||
|
||||
if (out !== "")
|
||||
msg.channel.createMessage({
|
||||
content: out,
|
||||
allowedMentions: {
|
||||
repliedUser: false,
|
||||
},
|
||||
messageReference: {
|
||||
messageID: msg.id,
|
||||
},
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue