codePreviews: limit to 30 lines if no lines given, ignore non-ok, message splitting

This commit is contained in:
Cynthia Foxwell 2023-03-28 12:03:21 -06:00
parent 955ca1c739
commit 554ba3aae7

View file

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