summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-09-07 14:00:39 -0700
committerFuwn <[email protected]>2025-09-07 14:00:39 -0700
commit34d7302a7d1db91bd9cdb7c698b999d65ea99cbb (patch)
treeb2df57f4cbe0a53c7f6aa9858b48a1c9af67ed7d
parentstyle(reddit): Fix lints (diff)
downloadumabotdiscord-34d7302a7d1db91bd9cdb7c698b999d65ea99cbb.tar.xz
umabotdiscord-34d7302a7d1db91bd9cdb7c698b999d65ea99cbb.zip
fix: Properly handle videos
-rw-r--r--src/reddit.ts16
-rw-r--r--src/server.ts46
2 files changed, 58 insertions, 4 deletions
diff --git a/src/reddit.ts b/src/reddit.ts
index aaadcd2..6fe576e 100644
--- a/src/reddit.ts
+++ b/src/reddit.ts
@@ -13,6 +13,22 @@ export interface RedditPost {
is_gallery?: boolean;
over_18: boolean;
link_flair_text?: string;
+ thumbnail?: string;
+ preview?: {
+ images: Array<{
+ source: {
+ url: string;
+ width: number;
+ height: number;
+ };
+ resolutions: Array<{
+ url: string;
+ width: number;
+ height: number;
+ }>;
+ }>;
+ enabled: boolean;
+ };
media?: {
reddit_video?: {
fallback_url: string;
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(/&amp;/g, '&')
+ .replace(/&lt;/g, '<')
+ .replace(/&gt;/g, '>')
+ .replace(/&quot;/g, '"')
+ .replace(/&#x27;/g, "'")
+ .replace(/&#x2F;/g, '/')
+ .replace(/&#x60;/g, '`')
+ .replace(/&#x3D;/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;
}