Add embeds, forwards, and voice flags to the message shape
This commit is contained in:
@@ -34,6 +34,9 @@ const SENDABLE_CHANNEL_TYPES = new Set([0, 1, 3, 5, 10, 11, 12]);
|
||||
/** Channel types that are voice channels. */
|
||||
const VOICE_CHANNEL_TYPES = new Set([2, 13]);
|
||||
|
||||
/** Discord message flag for a voice message. */
|
||||
const VOICE_MESSAGE_FLAG = 1 << 13;
|
||||
|
||||
/** Discord's message fetch action, located lazily at runtime. */
|
||||
const MessageActions = findByPropsLazy("fetchMessages");
|
||||
|
||||
@@ -188,10 +191,64 @@ function getSendableChannel(channelId: string) {
|
||||
return channel;
|
||||
}
|
||||
|
||||
/** Pull a URL from an embed part. Discord gives some parts as strings and some as objects. */
|
||||
function embedUrl(value: any): string | null {
|
||||
if (typeof value === "string") return value;
|
||||
return value?.url ?? value?.proxyURL ?? value?.proxy_url ?? null;
|
||||
}
|
||||
|
||||
function mapAttachments(message: any) {
|
||||
return (message?.attachments ?? []).map((attachment: any) => ({
|
||||
id: attachment.id ?? null,
|
||||
filename: attachment.filename,
|
||||
url: attachment.url ?? null,
|
||||
contentType: attachment.contentType ?? attachment.content_type ?? null,
|
||||
size: attachment.size ?? null,
|
||||
width: attachment.width ?? null,
|
||||
height: attachment.height ?? null,
|
||||
durationSecs: attachment.durationSecs ?? attachment.duration_secs ?? null
|
||||
}));
|
||||
}
|
||||
|
||||
function mapEmbeds(message: any) {
|
||||
return (message?.embeds ?? []).map((embed: any) => ({
|
||||
type: embed?.type ?? null,
|
||||
url: embed?.url ?? null,
|
||||
title: embed?.title ?? null,
|
||||
provider: embed?.provider?.name ?? null,
|
||||
videoUrl: embedUrl(embed?.video),
|
||||
imageUrl: embedUrl(embed?.image),
|
||||
thumbnailUrl: embedUrl(embed?.thumbnail)
|
||||
}));
|
||||
}
|
||||
|
||||
function mapStickers(message: any) {
|
||||
return (message?.stickerItems ?? message?.sticker_items ?? []).map((sticker: any) => ({
|
||||
id: sticker.id ?? null,
|
||||
name: sticker.name ?? null,
|
||||
formatType: sticker.formatType ?? sticker.format_type ?? null
|
||||
}));
|
||||
}
|
||||
|
||||
/** Shape a forwarded snapshot. Snapshots carry no channel fields and are one level deep. */
|
||||
function mapSnapshot(snapshot: any) {
|
||||
const message = snapshot?.message ?? snapshot;
|
||||
|
||||
return {
|
||||
content: message?.content ?? "",
|
||||
timestamp: message?.timestamp ?? null,
|
||||
attachments: mapAttachments(message),
|
||||
embeds: mapEmbeds(message),
|
||||
stickerItems: mapStickers(message),
|
||||
voiceMessage: Boolean((message?.flags ?? 0) & VOICE_MESSAGE_FLAG)
|
||||
};
|
||||
}
|
||||
|
||||
/** Shape a Discord message for the daemon. Attachments carry CDN links only. */
|
||||
function mapMessage(message: any) {
|
||||
const reference = message.messageReference;
|
||||
const referenced = message.referencedMessage ?? message.referenced_message;
|
||||
const snapshots = message.messageSnapshots ?? message.message_snapshots ?? [];
|
||||
|
||||
return {
|
||||
id: message.id,
|
||||
@@ -210,13 +267,11 @@ function mapMessage(message: any) {
|
||||
authorName: referenced?.author?.username ?? null
|
||||
}
|
||||
: null,
|
||||
attachments: (message.attachments ?? []).map((attachment: any) => ({
|
||||
filename: attachment.filename,
|
||||
url: attachment.url ?? null,
|
||||
contentType: attachment.contentType ?? null,
|
||||
width: attachment.width ?? null,
|
||||
height: attachment.height ?? null
|
||||
}))
|
||||
attachments: mapAttachments(message),
|
||||
embeds: mapEmbeds(message),
|
||||
stickerItems: mapStickers(message),
|
||||
forwarded: snapshots.map(mapSnapshot),
|
||||
voiceMessage: Boolean((message.flags ?? 0) & VOICE_MESSAGE_FLAG)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user