codePreviews: null check
This commit is contained in:
		
							parent
							
								
									98a3dfe26c
								
							
						
					
					
						commit
						e0d67ff501
					
				
					 1 changed files with 53 additions and 51 deletions
				
			
		| 
						 | 
					@ -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 !== "")
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue