codePreviews: fix edge case and logging for future issues

This commit is contained in:
Cynthia Foxwell 2024-08-30 18:58:33 -06:00
parent 83c0b226eb
commit f54ec82c99

View file

@ -2,6 +2,7 @@ const {ApplicationCommandOptionTypes, MessageFlags} = require("#util/dconstants.
const InteractionCommand = require("#lib/interactionCommand.js"); const InteractionCommand = require("#lib/interactionCommand.js");
const events = require("#lib/events.js"); const events = require("#lib/events.js");
const logger = require("#lib/logger.js");
const {hasFlag} = require("#lib/guildSettings.js"); const {hasFlag} = require("#lib/guildSettings.js");
const REGEX_GITHUB = const REGEX_GITHUB =
/(?:\s|^)(\|\|\s*)?https?:\/\/(www\.)?github\.com\/[a-z0-9-]+\/[a-z0-9-._]+\/blob\/([a-z0-9-_.?&=#root/%]*)(\s*\|\|)?/gi; /(?:\s|^)(\|\|\s*)?https?:\/\/(www\.)?github\.com\/[a-z0-9-]+\/[a-z0-9-._]+\/blob\/([a-z0-9-_.?&=#root/%]*)(\s*\|\|)?/gi;
@ -120,6 +121,7 @@ events.add("messageCreate", "codePreviews", async function (msg) {
for (const link of githubLinks) { for (const link of githubLinks) {
const spoiler = REGEX_SPOILER.test(link); const spoiler = REGEX_SPOILER.test(link);
files.push(await processFile(link.replace("/blob/", "/raw/"), link, spoiler)); files.push(await processFile(link.replace("/blob/", "/raw/"), link, spoiler));
logger.verbose("hf:codePreviews", `Processed GitHub file: ${link}`);
} }
} }
@ -127,6 +129,7 @@ events.add("messageCreate", "codePreviews", async function (msg) {
for (const link of gitlabLinks) { for (const link of gitlabLinks) {
const spoiler = REGEX_SPOILER.test(link); const spoiler = REGEX_SPOILER.test(link);
files.push(await processFile(link.replace("/blob/", "/raw/"), link, spoiler)); files.push(await processFile(link.replace("/blob/", "/raw/"), link, spoiler));
logger.verbose("hf:codePreviews", `Processed GitLab file: ${link}`);
} }
} }
@ -134,6 +137,7 @@ events.add("messageCreate", "codePreviews", async function (msg) {
for (const link of giteaLinks) { for (const link of giteaLinks) {
const spoiler = REGEX_SPOILER.test(link); const spoiler = REGEX_SPOILER.test(link);
files.push(await processFile(link.replace("/src/", "/raw/"), link, spoiler)); files.push(await processFile(link.replace("/src/", "/raw/"), link, spoiler));
logger.verbose("hf:codePreviews", `Processed Gitea file: ${link}`);
} }
} }
@ -147,15 +151,16 @@ events.add("messageCreate", "codePreviews", async function (msg) {
if (file == "") continue; if (file == "") continue;
if (out.length + file.length > 2000) { if (out.length + file.length > 2000) {
await msg.channel.createMessage({ if (out.length > 0)
content: out, await msg.channel.createMessage({
allowedMentions: { content: out,
repliedUser: false, allowedMentions: {
}, repliedUser: false,
messageReference: { },
messageID: msg.id, messageReference: {
}, messageID: msg.id,
}); },
});
out = file; out = file;
} else { } else {