MessageLinkEmbeds: fix erroring on some invalid message links

This commit is contained in:
Vendicated 2024-02-15 10:12:33 +01:00
parent 89367e3b2a
commit bc0a55053d
No known key found for this signature in database
GPG key ID: D66986BAF75ECF18

View file

@ -31,13 +31,14 @@ import {
GuildStore, GuildStore,
MessageStore, MessageStore,
Parser, Parser,
PermissionsBits,
PermissionStore, PermissionStore,
RestAPI, RestAPI,
Text, Text,
TextAndImagesSettingsStores, TextAndImagesSettingsStores,
UserStore UserStore
} from "@webpack/common"; } from "@webpack/common";
import { Channel, Guild, Message } from "discord-types/general"; import { Channel, Message } from "discord-types/general";
const messageCache = new Map<string, { const messageCache = new Map<string, {
message?: Message; message?: Message;
@ -50,7 +51,7 @@ const ChannelMessage = findComponentByCodeLazy("renderSimpleAccessories)");
const SearchResultClasses = findByPropsLazy("message", "searchResult"); const SearchResultClasses = findByPropsLazy("message", "searchResult");
const messageLinkRegex = /(?<!<)https?:\/\/(?:\w+\.)?discord(?:app)?\.com\/channels\/(\d{17,20}|@me)\/(\d{17,20})\/(\d{17,20})/g; const messageLinkRegex = /(?<!<)https?:\/\/(?:\w+\.)?discord(?:app)?\.com\/channels\/(?:\d{17,20}|@me)\/(\d{17,20})\/(\d{17,20})/g;
const tenorRegex = /^https:\/\/(?:www\.)?tenor\.com\//; const tenorRegex = /^https:\/\/(?:www\.)?tenor\.com\//;
interface Attachment { interface Attachment {
@ -63,7 +64,6 @@ interface Attachment {
interface MessageEmbedProps { interface MessageEmbedProps {
message: Message; message: Message;
channel: Channel; channel: Channel;
guildID: string;
} }
const messageFetchQueue = new Queue(); const messageFetchQueue = new Queue();
@ -226,19 +226,19 @@ function MessageEmbedAccessory({ message }: { message: Message; }) {
let match = null as RegExpMatchArray | null; let match = null as RegExpMatchArray | null;
while ((match = messageLinkRegex.exec(message.content!)) !== null) { while ((match = messageLinkRegex.exec(message.content!)) !== null) {
const [_, guildID, channelID, messageID] = match; const [_, channelID, messageID] = match;
if (embeddedBy.includes(messageID)) { if (embeddedBy.includes(messageID)) {
continue; continue;
} }
const linkedChannel = ChannelStore.getChannel(channelID); const linkedChannel = ChannelStore.getChannel(channelID);
if (!linkedChannel || (guildID !== "@me" && !PermissionStore.can(1024n /* view channel */, linkedChannel))) { if (!linkedChannel || (!linkedChannel.isDM() && !PermissionStore.can(PermissionsBits.VIEW_CHANNEL, linkedChannel))) {
continue; continue;
} }
const { listMode, idList } = settings.store; const { listMode, idList } = settings.store;
const isListed = [guildID, channelID, message.author.id].some(id => id && idList.includes(id)); const isListed = [linkedChannel.guild_id, channelID, message.author.id].some(id => id && idList.includes(id));
if (listMode === "blacklist" && isListed) continue; if (listMode === "blacklist" && isListed) continue;
if (listMode === "whitelist" && !isListed) continue; if (listMode === "whitelist" && !isListed) continue;
@ -265,8 +265,7 @@ function MessageEmbedAccessory({ message }: { message: Message; }) {
const messageProps: MessageEmbedProps = { const messageProps: MessageEmbedProps = {
message: withEmbeddedBy(linkedMessage, [...embeddedBy, message.id]), message: withEmbeddedBy(linkedMessage, [...embeddedBy, message.id]),
channel: linkedChannel, channel: linkedChannel
guildID
}; };
const type = settings.store.automodEmbeds; const type = settings.store.automodEmbeds;
@ -280,10 +279,8 @@ function MessageEmbedAccessory({ message }: { message: Message; }) {
return accessories.length ? <>{accessories}</> : null; return accessories.length ? <>{accessories}</> : null;
} }
function ChannelMessageEmbedAccessory({ message, channel, guildID }: MessageEmbedProps): JSX.Element | null { function ChannelMessageEmbedAccessory({ message, channel }: MessageEmbedProps): JSX.Element | null {
const isDM = guildID === "@me"; const guild = !channel.isDM() && GuildStore.getGuild(channel.guild_id);
const guild = !isDM && GuildStore.getGuild(channel.guild_id);
const dmReceiver = UserStore.getUser(ChannelStore.getChannel(channel.id).recipients?.[0]); const dmReceiver = UserStore.getUser(ChannelStore.getChannel(channel.id).recipients?.[0]);
@ -293,11 +290,8 @@ function ChannelMessageEmbedAccessory({ message, channel, guildID }: MessageEmbe
color: "var(--background-secondary)", color: "var(--background-secondary)",
author: { author: {
name: <Text variant="text-xs/medium" tag="span"> name: <Text variant="text-xs/medium" tag="span">
<span>{isDM ? "Direct Message - " : (guild as Guild).name + " - "}</span> {channel.isDM() && <span>Direct Message - </span>}
{isDM {Parser.parse(channel.isDM() ? `<@${dmReceiver.id}>` : `<#${channel.id}>`)}
? Parser.parse(`<@${dmReceiver.id}>`)
: Parser.parse(`<#${channel.id}>`)
}
</Text>, </Text>,
iconProxyURL: guild iconProxyURL: guild
? `https://${window.GLOBAL_ENV.CDN_HOST}/icons/${guild.id}/${guild.icon}.png` ? `https://${window.GLOBAL_ENV.CDN_HOST}/icons/${guild.id}/${guild.icon}.png`
@ -318,9 +312,8 @@ function ChannelMessageEmbedAccessory({ message, channel, guildID }: MessageEmbe
} }
function AutomodEmbedAccessory(props: MessageEmbedProps): JSX.Element | null { function AutomodEmbedAccessory(props: MessageEmbedProps): JSX.Element | null {
const { message, channel, guildID } = props; const { message, channel } = props;
const compact = TextAndImagesSettingsStores.MessageDisplayCompact.useSetting(); const compact = TextAndImagesSettingsStores.MessageDisplayCompact.useSetting();
const isDM = guildID === "@me";
const images = getImages(message); const images = getImages(message);
const { parse } = Parser; const { parse } = Parser;
@ -328,11 +321,11 @@ function AutomodEmbedAccessory(props: MessageEmbedProps): JSX.Element | null {
channel={channel} channel={channel}
childrenAccessories={ childrenAccessories={
<Text color="text-muted" variant="text-xs/medium" tag="span"> <Text color="text-muted" variant="text-xs/medium" tag="span">
{isDM {channel.isDM()
? parse(`<@${ChannelStore.getChannel(channel.id).recipients[0]}>`) ? parse(`<@${ChannelStore.getChannel(channel.id).recipients[0]}>`)
: parse(`<#${channel.id}>`) : parse(`<#${channel.id}>`)
} }
<span>{isDM ? " - Direct Message" : " - " + GuildStore.getGuild(channel.guild_id)?.name}</span> {channel.isDM() && <span> - Direct Message</span>}
</Text> </Text>
} }
compact={compact} compact={compact}