HiddenPhox/src/modules/codePreviews.js

181 lines
4.7 KiB
JavaScript
Raw Normal View History

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 =
/(?:\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"), "");
}
async function processFile(link) {
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 "";
const file = await res.text();
const lines = file.replace(/\r/g, "").split("\n");
const fileName = link.substring(
link.lastIndexOf("/") + 1,
link.indexOf("#") == -1 ? link.length : link.indexOf("#")
);
const fileType =
fileName.lastIndexOf(".") == -1
? ""
: fileName.substring(fileName.lastIndexOf(".") + 1);
if (fileType == "md") return "";
const lineStr = link.match(/#L\d+(-L?\d+)?/)?.[0];
let startLine, endLine;
let entireFile = false;
if (lineStr) {
const [start, end] = lineStr.match(/\d+/g);
if (!end) {
startLine = endLine = start;
} else {
startLine = start;
endLine = end;
}
} else {
entireFile = true;
startLine = 0;
endLine = lines.length;
}
const whichLines = entireFile
? ""
: startLine == endLine
? "Line " + startLine
: "Lines " + startLine + "-" + endLine;
if (entireFile && lines.length > 20) return "";
const targetLines = (
entireFile ? lines : lines.slice(startLine - 1, endLine)
).join("\n");
return `**${fileName}: **${whichLines}\n\`\`\`${fileType}\n${unindent(
targetLines
)}\n\`\`\``;
}
2022-05-07 22:49:07 +00:00
events.add("messageCreate", "codePreviews", async function (msg) {
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
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) {
files.push(await processFile(link.replace("/blob/", "/raw/")));
}
}
if (gitlabLinks?.length) {
for (const link of gitlabLinks) {
files.push(await processFile(link.replace("/blob/", "/raw/")));
}
}
if (giteaLinks?.length) {
for (const link of giteaLinks) {
files.push(await processFile(link.replace("/src/", "/raw/")));
2022-05-07 22:51:27 +00:00
}
2022-05-07 22:49:07 +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(() => {});
}
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();
}
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 (
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");
}
);