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 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");
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");
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 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;
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;
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 {
end = parseInt(end.replace("L", ""));
startLine = start;
endLine = end;
entireFile = true;
startLine = 1;
endLine = lines.length;
}
} 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";
}
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 !== "")