|
| 1 | +import { clampText } from '@/util/text.js'; |
| 2 | +import { |
| 3 | + ActionRowBuilder, |
| 4 | + type APIEmbedField, |
| 5 | + ButtonBuilder, |
| 6 | + ButtonStyle, |
| 7 | + ComponentType, |
| 8 | + EmbedBuilder, |
| 9 | + type Message, |
| 10 | + type MessageActionRowComponentBuilder, |
| 11 | + type MessageCreateOptions, |
| 12 | + MessageFlags, |
| 13 | + TextDisplayBuilder, |
| 14 | + type User, |
| 15 | +} from 'discord.js'; |
| 16 | + |
| 17 | +const EMBED_DESC_LIMIT = 4096; |
| 18 | +const FIELD_VALUE_LIMIT = 1024; |
| 19 | +const JUMP_BUTTON_LABEL = 'Jump to message'; |
| 20 | + |
| 21 | +type OriginalQuoteInfo = { |
| 22 | + authorMention: string; |
| 23 | + channelName: string; |
| 24 | + jumpLink: string; |
| 25 | +}; |
| 26 | + |
| 27 | +// Captures the pieces of a line we previously generated: |
| 28 | +// "<@quotedBy> quoted <@author> from **#channel** [link ↗](<url>)" (V2, has link) |
| 29 | +// "<@quotedBy> quoted <@author> from **#channel**" (V1, no link) |
| 30 | +const QUOTE_LINE_CAPTURE_REGEX = |
| 31 | + /^(?:-#\s)?<@!?\d+>\squoted\s(<@!?\d+>)\sfrom\s\*\*#(.+?)\*\*(?:\s\[link ↗\]\(<(.+?)>\))?$/; |
| 32 | + |
| 33 | +type ParsedQuoteLine = { |
| 34 | + authorMention: string; |
| 35 | + channelName: string; |
| 36 | + jumpLink?: string; |
| 37 | +}; |
| 38 | + |
| 39 | +const parseQuoteLine = (text: string): ParsedQuoteLine | null => { |
| 40 | + const match = QUOTE_LINE_CAPTURE_REGEX.exec(text); |
| 41 | + if (!match) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + const [, authorMention, channelName, jumpLink] = match; |
| 45 | + return { authorMention, channelName, jumpLink }; |
| 46 | +}; |
| 47 | + |
| 48 | +const buildQuoteLine = ( |
| 49 | + quotedBy: User, |
| 50 | + info: OriginalQuoteInfo, |
| 51 | + includeLink: boolean |
| 52 | +): string => |
| 53 | + clampText( |
| 54 | + includeLink |
| 55 | + ? `${quotedBy.toString()} quoted ${info.authorMention} from **#${info.channelName}** [link ↗](<${info.jumpLink}>)` |
| 56 | + : `${quotedBy.toString()} quoted ${info.authorMention} from **#${info.channelName}**`, |
| 57 | + FIELD_VALUE_LIMIT |
| 58 | + ); |
| 59 | + |
| 60 | +const findExistingJumpButtonUrl = (message: Message): string | null => { |
| 61 | + for (const row of message.components) { |
| 62 | + if (row.type !== ComponentType.ActionRow) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + for (const component of row.components) { |
| 66 | + if ( |
| 67 | + component.type === ComponentType.Button && |
| 68 | + component.style === ButtonStyle.Link && |
| 69 | + component.label === JUMP_BUTTON_LABEL |
| 70 | + ) { |
| 71 | + return component.url ?? null; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + return null; |
| 76 | +}; |
| 77 | + |
| 78 | +export const createQuoteEmbed = ({ |
| 79 | + quotedMessage, |
| 80 | + quotedBy, |
| 81 | +}: { |
| 82 | + quotedMessage: Message; |
| 83 | + quotedBy: User; |
| 84 | +}): MessageCreateOptions | null => { |
| 85 | + const channelName = !quotedMessage.channel.isDMBased() |
| 86 | + ? quotedMessage.channel.name |
| 87 | + : 'Direct Message'; |
| 88 | + |
| 89 | + // Default: quotedMessage is an original, non-quote message, so it *is* |
| 90 | + // the source of truth for author/channel/link. |
| 91 | + const freshInfo: OriginalQuoteInfo = { |
| 92 | + authorMention: `${quotedMessage.author.toString()}`, |
| 93 | + channelName, |
| 94 | + jumpLink: quotedMessage.url, |
| 95 | + }; |
| 96 | + |
| 97 | + const isV2 = quotedMessage.flags.has(MessageFlags.IsComponentsV2); |
| 98 | + |
| 99 | + if (isV2) { |
| 100 | + const components = quotedMessage.components.map((component) => |
| 101 | + component.toJSON() |
| 102 | + ); |
| 103 | + |
| 104 | + const existingLineIndex = components.findIndex( |
| 105 | + (component) => component.type === ComponentType.TextDisplay |
| 106 | + ); |
| 107 | + const existingContent = |
| 108 | + existingLineIndex !== -1 |
| 109 | + ? (components[existingLineIndex] as { content: string }).content |
| 110 | + : null; |
| 111 | + |
| 112 | + const parsed = |
| 113 | + existingContent !== null ? parseQuoteLine(existingContent) : null; |
| 114 | + |
| 115 | + const originalInfo: OriginalQuoteInfo = parsed |
| 116 | + ? { |
| 117 | + authorMention: parsed.authorMention, |
| 118 | + channelName: parsed.channelName, |
| 119 | + jumpLink: parsed.jumpLink ?? freshInfo.jumpLink, |
| 120 | + } |
| 121 | + : freshInfo; |
| 122 | + |
| 123 | + const attributionLine = new TextDisplayBuilder() |
| 124 | + .setContent(`-# ${buildQuoteLine(quotedBy, originalInfo, true)}`) |
| 125 | + .toJSON(); |
| 126 | + |
| 127 | + if (existingLineIndex !== -1) { |
| 128 | + components[existingLineIndex] = attributionLine; |
| 129 | + } else { |
| 130 | + components.push(attributionLine); |
| 131 | + } |
| 132 | + |
| 133 | + return { |
| 134 | + allowedMentions: { parse: [] }, |
| 135 | + components, |
| 136 | + flags: MessageFlags.IsComponentsV2, |
| 137 | + }; |
| 138 | + } |
| 139 | + |
| 140 | + // Legacy (non-V2 components) |
| 141 | + const attachmentUrls = quotedMessage.attachments.map( |
| 142 | + (attachment) => attachment.url |
| 143 | + ); |
| 144 | + const firstImage = quotedMessage.attachments.find((attachment) => |
| 145 | + attachment.contentType?.startsWith('image/') |
| 146 | + ); |
| 147 | + |
| 148 | + let embeds = quotedMessage.embeds |
| 149 | + .filter((embed) => embed.data.type === 'rich') |
| 150 | + .slice(0, 9) // leave room for our wrapper, max 10 embeds/message |
| 151 | + .map((embed) => EmbedBuilder.from(embed)); |
| 152 | + |
| 153 | + // Find an existing "Quoted by" field, if quotedMessage is itself a quote. |
| 154 | + let existingField: APIEmbedField | null = null; |
| 155 | + for (const embed of embeds) { |
| 156 | + const found = embed.data.fields?.find( |
| 157 | + (field) => /^quoted by$/i.test(field.name) && parseQuoteLine(field.value) |
| 158 | + ); |
| 159 | + if (found) { |
| 160 | + existingField = found; |
| 161 | + break; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + const parsedField = existingField |
| 166 | + ? parseQuoteLine(existingField.value) |
| 167 | + : null; |
| 168 | + |
| 169 | + // Recover link from the existing jump button, if present, otherwise fall back to the parsed field or fresh info. |
| 170 | + const originalInfo: OriginalQuoteInfo = parsedField |
| 171 | + ? { |
| 172 | + authorMention: parsedField.authorMention, |
| 173 | + channelName: parsedField.channelName, |
| 174 | + jumpLink: |
| 175 | + findExistingJumpButtonUrl(quotedMessage) ?? |
| 176 | + parsedField.jumpLink ?? |
| 177 | + freshInfo.jumpLink, |
| 178 | + } |
| 179 | + : freshInfo; |
| 180 | + |
| 181 | + const quotedByField: APIEmbedField = { |
| 182 | + name: 'Quoted by', |
| 183 | + value: buildQuoteLine(quotedBy, originalInfo, false), |
| 184 | + inline: false, |
| 185 | + }; |
| 186 | + |
| 187 | + if (existingField) { |
| 188 | + // Already a quote: swap the field's value in place, keep everything |
| 189 | + // else (original author/description/image/timestamp) untouched. |
| 190 | + existingField.value = quotedByField.value; |
| 191 | + } else { |
| 192 | + // First-time quote: build the wrapper/annotation. |
| 193 | + const authorOptions = { |
| 194 | + name: quotedMessage.author.username, |
| 195 | + iconURL: quotedMessage.author.displayAvatarURL({ size: 64 }), |
| 196 | + }; |
| 197 | + |
| 198 | + const stampAsQuote = (embed: EmbedBuilder) => |
| 199 | + embed.setAuthor(authorOptions).addFields(quotedByField).setTimestamp(); |
| 200 | + |
| 201 | + const hasContent = quotedMessage.content.length > 0; |
| 202 | + const hasEmbeds = embeds.length > 0; |
| 203 | + const hasAttachments = attachmentUrls.length > 0; |
| 204 | + const hasStickers = quotedMessage.stickers.size > 0; |
| 205 | + |
| 206 | + if (!hasContent && !hasStickers && !hasEmbeds && !hasAttachments) { |
| 207 | + return null; |
| 208 | + } |
| 209 | + |
| 210 | + if (hasContent || hasStickers || (!hasEmbeds && !hasAttachments)) { |
| 211 | + const wrapper = stampAsQuote(new EmbedBuilder()).setDescription( |
| 212 | + hasContent |
| 213 | + ? clampText(quotedMessage.content, EMBED_DESC_LIMIT) |
| 214 | + : hasStickers |
| 215 | + ? '*sent a sticker*' |
| 216 | + : null |
| 217 | + ); |
| 218 | + if (firstImage) { |
| 219 | + wrapper.setImage(firstImage.url); |
| 220 | + } |
| 221 | + embeds = [wrapper, ...embeds]; |
| 222 | + } else if (hasEmbeds) { |
| 223 | + embeds[0] = stampAsQuote(embeds[0]); |
| 224 | + } else { |
| 225 | + embeds = [ |
| 226 | + stampAsQuote(new EmbedBuilder()).setImage(firstImage?.url ?? null), |
| 227 | + ]; |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + // Don't re-send the image we already used as the embed's setImage, |
| 232 | + // otherwise it shows up twice. |
| 233 | + const filesToSend = firstImage |
| 234 | + ? attachmentUrls.filter((url) => url !== firstImage.url) |
| 235 | + : attachmentUrls; |
| 236 | + |
| 237 | + return { |
| 238 | + allowedMentions: { parse: [] }, |
| 239 | + embeds: embeds.length > 0 ? embeds : undefined, |
| 240 | + components: [ |
| 241 | + new ActionRowBuilder<MessageActionRowComponentBuilder>().addComponents( |
| 242 | + new ButtonBuilder() |
| 243 | + .setURL(originalInfo.jumpLink) |
| 244 | + .setLabel(JUMP_BUTTON_LABEL) |
| 245 | + .setStyle(ButtonStyle.Link) |
| 246 | + ), |
| 247 | + ], |
| 248 | + files: filesToSend.length > 0 ? filesToSend : undefined, |
| 249 | + }; |
| 250 | +}; |
0 commit comments