diff --git a/src/discord/handlers.ts b/src/discord/handlers.ts index 3acc20e..29f616b 100644 --- a/src/discord/handlers.ts +++ b/src/discord/handlers.ts @@ -14,6 +14,12 @@ interface MatchResult { type: 'channel' | 'role' } +interface ReplyInfo { + author: string + content: string + messageLink: string +} + async function handleMessage(message: Message): Promise { if (!message.guild) return @@ -55,6 +61,22 @@ async function handleMessage(message: Message): Promise { const messageLink = `https://discord.com/channels/${message.guild.id}/${message.channel.id}/${message.id}` const channelName = 'name' in message.channel ? (message.channel.name ?? 'unknown') : 'DM' + // Fetch replied message if this is a reply + let replyTo: ReplyInfo | undefined + if (message.type === 'REPLY' && message.reference?.messageId) { + try { + const repliedMessage = await message.channel.messages.fetch(message.reference.messageId) + replyTo = { + author: repliedMessage.author.displayName ?? repliedMessage.author.username, + content: repliedMessage.content, + messageLink: `https://discord.com/channels/${message.reference.guildId}/${message.reference.channelId}/${message.reference.messageId}`, + } + } + catch { + // Replied message deleted or inaccessible + } + } + try { await forwardMessage({ topicId: match.topicId, @@ -64,6 +86,7 @@ async function handleMessage(message: Message): Promise { content: message.content, attachments, messageLink, + replyTo, }) console.log(`Forwarded message from ${message.author.tag} (${match.type}: ${match.label}) in ${serverConfig.name}`) } diff --git a/src/telegram/sender.ts b/src/telegram/sender.ts index 5234d13..5a8e70c 100644 --- a/src/telegram/sender.ts +++ b/src/telegram/sender.ts @@ -4,10 +4,18 @@ import { config } from '../config.js' import { telegram } from './client.js' export async function forwardMessage(opts: ForwardMessageOptions): Promise { - const { topicId, author, role, channel, content, attachments, messageLink } = opts + const { topicId, author, role, channel, content, attachments, messageLink, replyTo } = opts + + let text = '' + + // Add reply context as blockquote if present + if (replyTo) { + const replyContent = replyTo.content || '(no text)' + text += `
${escapeHtml(replyTo.author)}\n${escapeHtml(replyContent)}\nView original
\n\n` + } const roleText = role ? ` (${escapeHtml(role)})` : '' - const text = `${escapeHtml(author)}${roleText} in #${escapeHtml(channel)}\n${escapeHtml(content)}\n\nJump to message` + text += `${escapeHtml(author)}${roleText} in #${escapeHtml(channel)}\n${escapeHtml(content)}\n\nJump to message` // Enable link preview only if content has URLs (not just the discord jump link) const hasLinks = /https?:\/\/\S+/i.test(content) diff --git a/src/types.ts b/src/types.ts index 8c649ff..807bf00 100644 --- a/src/types.ts +++ b/src/types.ts @@ -32,6 +32,11 @@ export interface ForwardMessageOptions { content: string attachments: AttachmentInfo[] messageLink: string + replyTo?: { + author: string + content: string + messageLink: string + } } export interface AttachmentInfo {