codePreviews: impl gitlab and gitea support
This commit is contained in:
		
							parent
							
								
									e8b4ffe358
								
							
						
					
					
						commit
						a4d84e6c84
					
				
					 1 changed files with 68 additions and 54 deletions
				
			
		| 
						 | 
					@ -19,20 +19,8 @@ function unindent(str) {
 | 
				
			||||||
  return str.replace(new RegExp(`^ {${minIndent}}`, "gm"), "");
 | 
					  return str.replace(new RegExp(`^ {${minIndent}}`, "gm"), "");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
events.add("messageCreate", "codePreviews", async function (msg) {
 | 
					async function processFile(link) {
 | 
				
			||||||
  if (!msg.guildID) return;
 | 
					  const file = await fetch(link).then((res) => res.text());
 | 
				
			||||||
  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);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  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 lines = file.replace(/\r/g, "").split("\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const fileName = link.substring(
 | 
					  const fileName = link.substring(
 | 
				
			||||||
| 
						 | 
					@ -44,17 +32,15 @@ events.add("messageCreate", "codePreviews", async function (msg) {
 | 
				
			||||||
      ? ""
 | 
					      ? ""
 | 
				
			||||||
      : 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);
 | 
					    const [start, end] = link.match(/\d+/g);
 | 
				
			||||||
        start = parseInt(start.replace("L", ""));
 | 
					 | 
				
			||||||
    if (!end) {
 | 
					    if (!end) {
 | 
				
			||||||
      startLine = endLine = start;
 | 
					      startLine = endLine = start;
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
          end = parseInt(end.replace("L", ""));
 | 
					 | 
				
			||||||
      startLine = start;
 | 
					      startLine = start;
 | 
				
			||||||
      endLine = end;
 | 
					      endLine = end;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -77,7 +63,7 @@ events.add("messageCreate", "codePreviews", async function (msg) {
 | 
				
			||||||
    entireFile ? lines : lines.slice(startLine, endLine + 1)
 | 
					    entireFile ? lines : lines.slice(startLine, endLine + 1)
 | 
				
			||||||
  ).join("\n");
 | 
					  ).join("\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      out +=
 | 
					  return (
 | 
				
			||||||
    "**" +
 | 
					    "**" +
 | 
				
			||||||
    fileName +
 | 
					    fileName +
 | 
				
			||||||
    ": **" +
 | 
					    ": **" +
 | 
				
			||||||
| 
						 | 
					@ -86,7 +72,35 @@ events.add("messageCreate", "codePreviews", async function (msg) {
 | 
				
			||||||
    fileType +
 | 
					    fileType +
 | 
				
			||||||
    "\n" +
 | 
					    "\n" +
 | 
				
			||||||
    unindent(targetLines) +
 | 
					    unindent(targetLines) +
 | 
				
			||||||
        "\n```\n";
 | 
					    "\n```\n"
 | 
				
			||||||
 | 
					  );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (githubLinks?.length) {
 | 
				
			||||||
 | 
					    for (const link of githubLinks) {
 | 
				
			||||||
 | 
					      out += await processFile(link.replace("/blob/", "/raw/"));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (gitlabLinks?.length) {
 | 
				
			||||||
 | 
					    for (const link of gitlabLinks) {
 | 
				
			||||||
 | 
					      out += await processFile(link.replace("/blob/", "/raw/"));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (giteaLinks?.length) {
 | 
				
			||||||
 | 
					    for (const link of giteaLinks) {
 | 
				
			||||||
 | 
					      out += await processFile(link.replace("/src/", "/raw/"));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue