2023-01-22 04:45:57 +00:00
|
|
|
const {MessageFlags} = require("@projectdysnomia/dysnomia").Constants;
|
2022-12-06 17:44:54 +00:00
|
|
|
|
2022-05-07 22:49:07 +00:00
|
|
|
const events = require("../lib/events.js");
|
|
|
|
const {hasFlag} = require("../lib/guildSettings.js");
|
|
|
|
|
|
|
|
const REGEX_GITHUB =
|
|
|
|
/(?:\s|^)https?:\/\/(www\.)?github\.com\/.+?\/.+?\/blob\/([a-zA-Z0-9-_.#/]*)/g;
|
|
|
|
const REGEX_GITLAB =
|
2022-05-22 19:21:57 +00:00
|
|
|
/(?:\s|^)https?:\/\/.+?\/.+?\/.+?\/-\/blob\/([a-zA-Z0-9-_.#/]*)/g;
|
2022-05-07 22:49:07 +00:00
|
|
|
const REGEX_GITEA =
|
|
|
|
/(?:\s|^)https?:\/\/.+?\/.+?\/.+?\/src\/branch\/([a-zA-Z0-9-_.#/]*)/g;
|
|
|
|
|
|
|
|
function unindent(str) {
|
|
|
|
str = str.replace(/\t/g, " ");
|
|
|
|
const minIndent =
|
|
|
|
str
|
|
|
|
.match(/^ *(?=\S)/gm)
|
|
|
|
?.reduce((prev, curr) => Math.min(prev, curr.length), Infinity) ?? 0;
|
|
|
|
if (!minIndent) return str;
|
|
|
|
return str.replace(new RegExp(`^ {${minIndent}}`, "gm"), "");
|
|
|
|
}
|
|
|
|
|
2022-05-07 23:42:22 +00:00
|
|
|
async function processFile(link) {
|
2023-03-28 18:03:21 +00:00
|
|
|
const res = await fetch(link);
|
|
|
|
if (!res.ok) return "";
|
2023-06-15 03:42:40 +00:00
|
|
|
if (!res.headers.get("Content-Type").startsWith("text/plain")) return "";
|
2023-03-28 18:03:21 +00:00
|
|
|
const file = await res.text();
|
2022-05-07 23:42:22 +00:00
|
|
|
const lines = file.replace(/\r/g, "").split("\n");
|
|
|
|
|
|
|
|
const fileName = link.substring(
|
|
|
|
link.lastIndexOf("/") + 1,
|
|
|
|
link.indexOf("#") == -1 ? link.length : link.indexOf("#")
|
|
|
|
);
|
|
|
|
const fileType =
|
2023-04-03 19:53:49 +00:00
|
|
|
fileName.lastIndexOf(".") == -1
|
2022-05-07 23:42:22 +00:00
|
|
|
? ""
|
2023-04-03 19:53:49 +00:00
|
|
|
: fileName.substring(fileName.lastIndexOf(".") + 1);
|
2022-05-07 23:42:22 +00:00
|
|
|
|
2023-08-04 02:24:01 +00:00
|
|
|
if (fileType == "md") return "";
|
|
|
|
|
2022-05-07 23:42:22 +00:00
|
|
|
const lineStr = link.match(/#L\d+(-L?\d+)?/)?.[0];
|
|
|
|
let startLine, endLine;
|
|
|
|
let entireFile = false;
|
|
|
|
|
|
|
|
if (lineStr) {
|
2022-06-29 03:29:30 +00:00
|
|
|
const [start, end] = lineStr.match(/\d+/g);
|
2022-05-07 23:42:22 +00:00
|
|
|
if (!end) {
|
|
|
|
startLine = endLine = start;
|
|
|
|
} else {
|
|
|
|
startLine = start;
|
|
|
|
endLine = end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
entireFile = true;
|
2023-03-28 18:03:21 +00:00
|
|
|
startLine = 0;
|
2022-05-07 23:42:22 +00:00
|
|
|
endLine = lines.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
const whichLines = entireFile
|
|
|
|
? ""
|
|
|
|
: startLine == endLine
|
|
|
|
? "Line " + startLine
|
|
|
|
: "Lines " + startLine + "-" + endLine;
|
|
|
|
|
2023-08-04 02:24:01 +00:00
|
|
|
if (entireFile && lines.length > 20) return "";
|
|
|
|
|
2022-05-07 23:42:22 +00:00
|
|
|
const targetLines = (
|
2023-08-04 02:24:01 +00:00
|
|
|
entireFile ? lines : lines.slice(startLine - 1, endLine)
|
2022-05-07 23:42:22 +00:00
|
|
|
).join("\n");
|
|
|
|
|
2023-03-28 18:03:21 +00:00
|
|
|
return `**${fileName}: **${whichLines}\n\`\`\`${fileType}\n${unindent(
|
|
|
|
targetLines
|
2023-08-04 02:24:01 +00:00
|
|
|
)}\n\`\`\``;
|
2022-05-07 23:42:22 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 22:49:07 +00:00
|
|
|
events.add("messageCreate", "codePreviews", async function (msg) {
|
2022-12-17 00:41:47 +00:00
|
|
|
if (msg.author.id == hf.bot.user.id) return;
|
2022-05-07 22:49:07 +00:00
|
|
|
if (!msg.guildID) return;
|
2022-05-07 23:59:02 +00:00
|
|
|
if (!(await hasFlag(msg.guildID, "codePreviews"))) return;
|
2022-05-07 22:49:07 +00:00
|
|
|
|
2023-03-28 18:03:21 +00:00
|
|
|
const files = [];
|
2022-05-07 22:49:07 +00:00
|
|
|
|
|
|
|
const githubLinks = msg.content.match(REGEX_GITHUB);
|
|
|
|
const gitlabLinks = msg.content.match(REGEX_GITLAB);
|
|
|
|
const giteaLinks = msg.content.match(REGEX_GITEA);
|
|
|
|
|
2022-05-07 22:51:27 +00:00
|
|
|
if (githubLinks?.length) {
|
|
|
|
for (const link of githubLinks) {
|
2023-03-28 18:03:21 +00:00
|
|
|
files.push(await processFile(link.replace("/blob/", "/raw/")));
|
2022-05-07 23:42:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gitlabLinks?.length) {
|
|
|
|
for (const link of gitlabLinks) {
|
2023-03-28 18:03:21 +00:00
|
|
|
files.push(await processFile(link.replace("/blob/", "/raw/")));
|
2022-05-07 23:42:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (giteaLinks?.length) {
|
|
|
|
for (const link of giteaLinks) {
|
2023-03-28 18:03:21 +00:00
|
|
|
files.push(await processFile(link.replace("/src/", "/raw/")));
|
2022-05-07 22:51:27 +00:00
|
|
|
}
|
2022-05-07 22:49:07 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 18:03:21 +00:00
|
|
|
let out = "";
|
|
|
|
const allFiles = files.join("\n").trim();
|
|
|
|
if (allFiles !== "" && allFiles.length <= 2000) {
|
2023-01-22 04:45:57 +00:00
|
|
|
await msg.edit({flags: MessageFlags.SUPPRESS_EMBEDS}).catch(() => {});
|
2023-03-28 18:03:21 +00:00
|
|
|
}
|
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
|
|
const file = files[i];
|
|
|
|
if (file === "") continue;
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2022-12-06 03:35:48 +00:00
|
|
|
|
2023-03-28 18:03:21 +00:00
|
|
|
if (i == files.length - 1 && out.length <= 2000) {
|
|
|
|
await msg.channel.createMessage({
|
|
|
|
content: out,
|
|
|
|
allowedMentions: {
|
|
|
|
repliedUser: false,
|
|
|
|
},
|
|
|
|
messageReference: {
|
|
|
|
messageID: msg.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2022-05-07 23:10:31 +00:00
|
|
|
}
|
2022-05-07 22:49:07 +00:00
|
|
|
});
|
2023-05-12 19:55:24 +00:00
|
|
|
|
|
|
|
// TODO: maybe all command outputs should have this ability
|
|
|
|
events.add(
|
|
|
|
"messageReactionAdd",
|
|
|
|
"codePreviews",
|
|
|
|
async function (msg, reaction, reactor) {
|
|
|
|
if (!msg.guildID) return;
|
|
|
|
if (!(await hasFlag(msg.guildID, "codePreviews"))) return;
|
|
|
|
if (reaction.name != "\u274c") return;
|
|
|
|
|
|
|
|
let channel = msg.channel;
|
|
|
|
if (!channel.name) {
|
|
|
|
channel = hf.bot.getChannel(channel.id);
|
|
|
|
}
|
|
|
|
if (!msg.messageReference) {
|
|
|
|
msg = await channel.getMessage(msg.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!msg.messageReference) return;
|
|
|
|
|
|
|
|
const ref = await channel.getMessage(msg.messageReference.messageID);
|
|
|
|
if (!ref) return;
|
|
|
|
if (
|
2023-08-04 02:24:01 +00:00
|
|
|
ref.author.id != reactor.id &&
|
2023-05-12 19:55:24 +00:00
|
|
|
!channel.permissionsOf(reactor.id).has("manageMessages")
|
|
|
|
)
|
|
|
|
return;
|
|
|
|
if (
|
|
|
|
!REGEX_GITHUB.test(ref.content) &&
|
|
|
|
!REGEX_GITLAB.test(ref.content) &&
|
|
|
|
!REGEX_GITEA.test(ref.content)
|
|
|
|
)
|
|
|
|
return;
|
|
|
|
|
|
|
|
await msg.delete("Author requested code preview deletion");
|
|
|
|
}
|
|
|
|
);
|