FakeNitro: Send fake links using Discord hyperlink markdown feature (#2158)

This commit is contained in:
sappho 2024-02-09 17:31:02 -05:00 committed by Nuckyz
parent f1f0da4a9d
commit b2c047390e
No known key found for this signature in database
GPG key ID: 440BF8296E1C4AD9

View file

@ -108,6 +108,7 @@ const enum FakeNoticeType {
const fakeNitroEmojiRegex = /\/emojis\/(\d+?)\.(png|webp|gif)/; const fakeNitroEmojiRegex = /\/emojis\/(\d+?)\.(png|webp|gif)/;
const fakeNitroStickerRegex = /\/stickers\/(\d+?)\./; const fakeNitroStickerRegex = /\/stickers\/(\d+?)\./;
const fakeNitroGifStickerRegex = /\/attachments\/\d+?\/\d+?\/(\d+?)\.gif/; const fakeNitroGifStickerRegex = /\/attachments\/\d+?\/\d+?\/(\d+?)\.gif/;
const hyperLinkRegex = /\[.+?\]\((https?:\/\/.+?)\)/;
const settings = definePluginSettings({ const settings = definePluginSettings({
enableEmojiBypass: { enableEmojiBypass: {
@ -447,13 +448,23 @@ export default definePlugin({
trimContent(content: Array<any>) { trimContent(content: Array<any>) {
const firstContent = content[0]; const firstContent = content[0];
if (typeof firstContent === "string") content[0] = firstContent.trimStart(); if (typeof firstContent === "string") {
if (content[0] === "") content.shift(); content[0] = firstContent.trimStart();
content[0] || content.shift();
} else if (firstContent?.type === "span") {
firstContent.props.children = firstContent.props.children.trimStart();
firstContent.props.children || content.shift();
}
const lastIndex = content.length - 1; const lastIndex = content.length - 1;
const lastContent = content[lastIndex]; const lastContent = content[lastIndex];
if (typeof lastContent === "string") content[lastIndex] = lastContent.trimEnd(); if (typeof lastContent === "string") {
if (content[lastIndex] === "") content.pop(); content[lastIndex] = lastContent.trimEnd();
content[lastIndex] || content.pop();
} else if (lastContent?.type === "span") {
lastContent.props.children = lastContent.props.children.trimEnd();
lastContent.props.children || content.pop();
}
}, },
clearEmptyArrayItems(array: Array<any>) { clearEmptyArrayItems(array: Array<any>) {
@ -465,7 +476,7 @@ export default definePlugin({
}, },
patchFakeNitroEmojisOrRemoveStickersLinks(content: Array<any>, inline: boolean) { patchFakeNitroEmojisOrRemoveStickersLinks(content: Array<any>, inline: boolean) {
// If content has more than one child or it's a single ReactElement like a header or list // If content has more than one child or it's a single ReactElement like a header, list or span
if ((content.length > 1 || typeof content[0]?.type === "string") && !settings.store.transformCompoundSentence) return content; if ((content.length > 1 || typeof content[0]?.type === "string") && !settings.store.transformCompoundSentence) return content;
let nextIndex = content.length; let nextIndex = content.length;
@ -574,7 +585,7 @@ export default definePlugin({
itemsToMaybePush.push(...message.attachments.filter(attachment => attachment.content_type === "image/gif").map(attachment => attachment.url)); itemsToMaybePush.push(...message.attachments.filter(attachment => attachment.content_type === "image/gif").map(attachment => attachment.url));
for (const item of itemsToMaybePush) { for (const item of itemsToMaybePush) {
if (!settings.store.transformCompoundSentence && !item.startsWith("http")) continue; if (!settings.store.transformCompoundSentence && !item.startsWith("http") && !hyperLinkRegex.test(item)) continue;
const imgMatch = item.match(fakeNitroStickerRegex); const imgMatch = item.match(fakeNitroStickerRegex);
if (imgMatch) { if (imgMatch) {
@ -619,8 +630,7 @@ export default definePlugin({
case "image": { case "image": {
if ( if (
!settings.store.transformCompoundSentence !settings.store.transformCompoundSentence
&& !contentItems.includes(embed.url!) && !contentItems.some(item => item === embed.url! || item.match(hyperLinkRegex)?.[1] === embed.url!)
&& !contentItems.includes(embed.image?.proxyURL!)
) return false; ) return false;
if (settings.store.transformEmojis) { if (settings.store.transformEmojis) {
@ -795,12 +805,16 @@ export default definePlugin({
if (sticker.format_type === StickerType.GIF && link.includes(".png")) { if (sticker.format_type === StickerType.GIF && link.includes(".png")) {
link = link.replace(".png", ".gif"); link = link.replace(".png", ".gif");
} }
if (sticker.format_type === StickerType.APNG) { if (sticker.format_type === StickerType.APNG) {
this.sendAnimatedSticker(link, sticker.id, channelId); this.sendAnimatedSticker(link, sticker.id, channelId);
return { cancel: true }; return { cancel: true };
} else { } else {
const url = new URL(link);
url.searchParams.set("name", sticker.name);
messageObj.content += `${getWordBoundary(messageObj.content, messageObj.content.length - 1)}[${sticker.name}](${url})`;
extra.stickers!.length = 0; extra.stickers!.length = 0;
messageObj.content += ` ${link}&name=${encodeURIComponent(sticker.name)}`;
} }
} }
@ -813,12 +827,13 @@ export default definePlugin({
if (emoji.guildId === guildId && !emoji.animated) continue; if (emoji.guildId === guildId && !emoji.animated) continue;
const emojiString = `<${emoji.animated ? "a" : ""}:${emoji.originalName || emoji.name}:${emoji.id}>`; const emojiString = `<${emoji.animated ? "a" : ""}:${emoji.originalName || emoji.name}:${emoji.id}>`;
const url = emoji.url.replace(/\?size=\d+/, "?" + new URLSearchParams({
size: Settings.plugins.FakeNitro.emojiSize, const url = new URL(emoji.url);
name: encodeURIComponent(emoji.name) url.searchParams.set("size", settings.store.emojiSize.toString());
})); url.searchParams.set("name", emoji.name);
messageObj.content = messageObj.content.replace(emojiString, (match, offset, origStr) => { messageObj.content = messageObj.content.replace(emojiString, (match, offset, origStr) => {
return `${getWordBoundary(origStr, offset - 1)}${url}${getWordBoundary(origStr, offset + match.length)}`; return `${getWordBoundary(origStr, offset - 1)}[:${emoji.name}:](${url})${getWordBoundary(origStr, offset + match.length)}`;
}); });
} }
} }
@ -840,11 +855,11 @@ export default definePlugin({
if (emoji.available !== false && canUseEmotes) return emojiStr; if (emoji.available !== false && canUseEmotes) return emojiStr;
if (emoji.guildId === guildId && !emoji.animated) return emojiStr; if (emoji.guildId === guildId && !emoji.animated) return emojiStr;
const url = emoji.url.replace(/\?size=\d+/, "?" + new URLSearchParams({ const url = new URL(emoji.url);
size: Settings.plugins.FakeNitro.emojiSize, url.searchParams.set("size", settings.store.emojiSize.toString());
name: encodeURIComponent(emoji.name) url.searchParams.set("name", emoji.name);
}));
return `${getWordBoundary(origStr, offset - 1)}${url}${getWordBoundary(origStr, offset + emojiStr.length)}`; return `${getWordBoundary(origStr, offset - 1)}[:${emoji.name}:](${url})${getWordBoundary(origStr, offset + emojiStr.length)}`;
}); });
}); });
}, },