diff --git a/src/modules/codePreviews.js b/src/modules/codePreviews.js index 69f1b8e..8fa7140 100644 --- a/src/modules/codePreviews.js +++ b/src/modules/codePreviews.js @@ -21,7 +21,9 @@ function unindent(str) { } async function processFile(link) { - const file = await fetch(link).then((res) => res.text()); + const res = await fetch(link); + if (!res.ok) return ""; + const file = await res.text(); const lines = file.replace(/\r/g, "").split("\n"); const fileName = link.substring( @@ -47,7 +49,7 @@ async function processFile(link) { } } else { entireFile = true; - startLine = 1; + startLine = 0; endLine = lines.length; } @@ -57,24 +59,13 @@ async function processFile(link) { ? "Line " + startLine : "Lines " + startLine + "-" + endLine; - startLine--; - endLine--; - const targetLines = ( - entireFile ? lines : lines.slice(startLine, endLine + 1) + entireFile ? lines.slice(0, 30) : lines.slice(startLine - 1, endLine) ).join("\n"); - return ( - "**" + - fileName + - ": **" + - whichLines + - "\n```" + - fileType + - "\n" + - unindent(targetLines) + - "\n```\n" - ); + return `**${fileName}: **${whichLines}\n\`\`\`${fileType}\n${unindent( + targetLines + )}${entireFile ? `\n... (${lines.length - 30} lines left)` : ""}\n\`\`\``; } events.add("messageCreate", "codePreviews", async function (msg) { @@ -82,7 +73,7 @@ events.add("messageCreate", "codePreviews", async function (msg) { if (!msg.guildID) return; if (!(await hasFlag(msg.guildID, "codePreviews"))) return; - let out = ""; + const files = []; const githubLinks = msg.content.match(REGEX_GITHUB); const gitlabLinks = msg.content.match(REGEX_GITLAB); @@ -90,35 +81,58 @@ events.add("messageCreate", "codePreviews", async function (msg) { if (githubLinks?.length) { for (const link of githubLinks) { - out += await processFile(link.replace("/blob/", "/raw/")); + files.push(await processFile(link.replace("/blob/", "/raw/"))); } } if (gitlabLinks?.length) { for (const link of gitlabLinks) { - out += await processFile(link.replace("/blob/", "/raw/")); + files.push(await processFile(link.replace("/blob/", "/raw/"))); } } if (giteaLinks?.length) { for (const link of giteaLinks) { - out += await processFile(link.replace("/src/", "/raw/")); + files.push(await processFile(link.replace("/src/", "/raw/"))); } } - if (out.length > 2000) return; - - if (out !== "") { + let out = ""; + const allFiles = files.join("\n").trim(); + if (allFiles !== "" && allFiles.length <= 2000) { await msg.edit({flags: MessageFlags.SUPPRESS_EMBEDS}).catch(() => {}); + } + for (let i = 0; i < files.length; i++) { + const file = files[i]; + if (file === "") continue; - await msg.channel.createMessage({ - content: out, - allowedMentions: { - repliedUser: false, - }, - messageReference: { - messageID: msg.id, - }, - }); + if (out.length + file.length > 2000) { + await msg.channel.createMessage({ + content: out, + allowedMentions: { + repliedUser: false, + }, + messageReference: { + messageID: msg.id, + }, + }); + + out = file; + } else { + out += "\n" + file; + out = out.trim(); + } + + if (i == files.length - 1 && out.length <= 2000) { + await msg.channel.createMessage({ + content: out, + allowedMentions: { + repliedUser: false, + }, + messageReference: { + messageID: msg.id, + }, + }); + } } });