diff options
| author | Fuwn <[email protected]> | 2025-09-07 14:00:39 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-09-07 14:00:39 -0700 |
| commit | 34d7302a7d1db91bd9cdb7c698b999d65ea99cbb (patch) | |
| tree | b2df57f4cbe0a53c7f6aa9858b48a1c9af67ed7d /src/server.ts | |
| parent | style(reddit): Fix lints (diff) | |
| download | umabotdiscord-34d7302a7d1db91bd9cdb7c698b999d65ea99cbb.tar.xz umabotdiscord-34d7302a7d1db91bd9cdb7c698b999d65ea99cbb.zip | |
fix: Properly handle videos
Diffstat (limited to 'src/server.ts')
| -rw-r--r-- | src/server.ts | 46 |
1 files changed, 42 insertions, 4 deletions
diff --git a/src/server.ts b/src/server.ts index bb0c458..baeb607 100644 --- a/src/server.ts +++ b/src/server.ts @@ -58,7 +58,6 @@ interface DiscordEmbed { footer: { text: string; }; - video?: { url: string }; image?: { url: string }; } @@ -87,6 +86,18 @@ class JSONResponse extends Response { const router = AutoRouter(); +function decodeHtmlEntities(str: string): string { + return str + .replace(/&/g, '&') + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/"/g, '"') + .replace(/'/g, "'") + .replace(///g, '/') + .replace(/`/g, '`') + .replace(/=/g, '='); +} + function createPostEmbed(post: RedditPost): DiscordEmbed { const mediaUrl = post.media?.reddit_video?.fallback_url || @@ -125,13 +136,40 @@ function createPostEmbed(post: RedditPost): DiscordEmbed { }, }; - if (mediaUrl) { + if (mediaUrl) if (post.media?.reddit_video || post.secure_media?.reddit_video) { - embed.video = { url: mediaUrl }; + if (!description) description = ''; + + description += + '\n\n📹 **This post contains a video** - [Click here to view](' + + mediaUrl + + ')'; + embed.description = description; + + if (post.preview?.images?.[0]?.source?.url) { + const decodedURL = decodeHtmlEntities( + post.preview.images[0].source.url, + ); + + console.log('Using preview image:', decodedURL); + + embed.image = { url: decodedURL }; + } else if ( + post.thumbnail && + post.thumbnail !== 'self' && + post.thumbnail !== 'default' + ) { + const decodedThumbnail = decodeHtmlEntities(post.thumbnail); + + console.log('Using thumbnail:', decodedThumbnail); + + embed.image = { url: decodedThumbnail }; + } else { + console.log('No suitable thumbnail found for video post'); + } } else { embed.image = { url: mediaUrl }; } - } return embed; } |