fix(discord-gateway): handle forwarded message content via messageSnapshots

Two-layer fix for forwarded messages showing as empty/clean:

Layer 1 (messageMetadata.ts): getReferencedMessageContent() now falls
back to message.messageSnapshots Collection when channel.messages.cache
lookup fails. Discord stores forward content in message_snapshots API
field, not in message.content.

Layer 2 (moderationBuilders.ts): buildReferenceXml() now parses msg.
metadata JSON to extract reference.content when DB getMessageById()
fails (cross-server forwards not in local DB).

Previously: forward messages captured with empty parentContent →
LLM saw no reference text → '99% confidence, pesan kosong'.
Now: forward content flows through capture → metadata → analysis.
This commit is contained in:
MythEclipse
2026-06-23 23:27:23 +07:00
parent ed4a506ca7
commit d5c22a3959
2 changed files with 40 additions and 4 deletions
@@ -48,6 +48,7 @@ export async function buildReferenceXml(msg: MessageRecord): Promise<string> {
let parentContent = "";
if (msg.reference_message_id) {
// 1. Try DB first — works for messages captured in the same server
try {
const parent = await getMessageById(msg.reference_message_id);
if (parent) {
@@ -55,7 +56,22 @@ export async function buildReferenceXml(msg: MessageRecord): Promise<string> {
parentContent = parentText.slice(0, 500);
}
} catch {
// Parent fetch failed — still inject reference with available info
// Parent fetch failed — fall through to metadata
}
// 2. Fall back to metadata — works for forwards from other servers/channels
// where the original message was never captured in this DB.
// The capture phase stores the snapshot content in metadata.reference.content.
if (!parentContent && msg.metadata) {
try {
const meta = JSON.parse(msg.metadata);
const refContent = meta?.reference?.content;
if (refContent && typeof refContent === "string") {
parentContent = refContent.slice(0, 500);
}
} catch {
// Metadata parse failed — no fallback available
}
}
}
@@ -211,17 +211,20 @@ export function getEmbedMetadata(
}
/**
* Try to get referenced message content from the channel cache.
* Try to get referenced message content from the channel cache or message snapshots.
* For replies, Discord sends `referenced_message` in the API, which
* discord.js-selfbot-v13 caches in the channel's message manager.
* Returns null if the message isn't cached (e.g. forwards without
* referenced_message payload).
* For forwards, Discord sends `message_snapshots` which discord.js-selfbot-v13
* stores in `message.messageSnapshots` as a Collection of partial Message objects.
* Returns null if the message can't be resolved from either source.
*/
function getReferencedMessageContent(
message: Message,
): { content: string; username: string; userId: string } | null {
const ref = message.reference;
if (!ref?.messageId) return null;
// 1. Channel cache — works for same-channel replies/forwards
try {
const cached = (message.channel as any)?.messages?.cache?.get(
ref.messageId,
@@ -236,6 +239,23 @@ function getReferencedMessageContent(
} catch {
// Cache may not be available or message not in it
}
// 2. messageSnapshots — works for cross-channel/cross-server forwards
// Discord API sends message_snapshots for FORWARD type messages,
// and discord.js-selfbot-v13 stores them in message.messageSnapshots.
try {
const snapshot = message.messageSnapshots?.get(ref.messageId);
if (snapshot?.content) {
return {
content: snapshot.content,
username: (snapshot as any).author?.username ?? "Unknown",
userId: (snapshot as any).author?.id ?? "",
};
}
} catch {
// Snapshots may not be available
}
return null;
}