codePreviews: null check

This commit is contained in:
Cynthia Foxwell 2022-05-07 16:51:27 -06:00
parent 98a3dfe26c
commit e0d67ff501

View file

@ -28,63 +28,65 @@ events.add("messageCreate", "codePreviews", async function (msg) {
const gitlabLinks = msg.content.match(REGEX_GITLAB); const gitlabLinks = msg.content.match(REGEX_GITLAB);
const giteaLinks = msg.content.match(REGEX_GITEA); const giteaLinks = msg.content.match(REGEX_GITEA);
for (const link of githubLinks) { if (githubLinks?.length) {
const rawLink = link.replace("/blob/", "/raw/"); for (const link of githubLinks) {
const file = await fetch(rawLink).then((res) => res.text()); const rawLink = link.replace("/blob/", "/raw/");
const lines = file.replace(/\r/g, "").split("\n"); const file = await fetch(rawLink).then((res) => res.text());
const lines = file.replace(/\r/g, "").split("\n");
const fileName = link.substring( const fileName = link.substring(
link.lastIndexOf("/") + 1, link.lastIndexOf("/") + 1,
link.indexOf("#") == -1 ? link.length : link.indexOf("#") link.indexOf("#") == -1 ? link.length : link.indexOf("#")
); );
const fileType = const fileType =
fileName.indexOf(".") == -1 fileName.indexOf(".") == -1
? "" ? ""
: fileName.subString(fileName.indexOf(".") + 1); : fileName.subString(fileName.indexOf(".") + 1);
const lineStr = link.match(/#L\d+?(-L\d+?)?/)[0]; const lineStr = link.match(/#L\d+?(-L\d+?)?/)[0];
let startLine, endLine; let startLine, endLine;
let entireFile = false; let entireFile = false;
if (lineStr) { if (lineStr) {
let [start, end] = link.match(/L\d+?/g); let [start, end] = link.match(/L\d+?/g);
start = parseInt(start.replace("L", "")); start = parseInt(start.replace("L", ""));
if (!end) { if (!end) {
startLine = endLine = start; startLine = endLine = start;
} else {
end = parseInt(end.replace("L", ""));
startLine = start;
endLine = end;
}
} else { } else {
end = parseInt(end.replace("L", "")); entireFile = true;
startLine = start; startLine = 1;
endLine = end; endLine = lines.length;
} }
} else {
entireFile = true; const whichLines = entireFile
startLine = 1; ? ""
endLine = lines.length; : 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";
} }
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 !== "") if (out !== "")