diff --git a/commands/randommeme/index.js b/commands/randommeme/index.js index ada75f20..7ca0c3fb 100644 --- a/commands/randommeme/index.js +++ b/commands/randommeme/index.js @@ -21,6 +21,7 @@ module.exports = { Params: [ { name: "comments", type: "boolean" }, { name: "flair", type: "string" }, + { name: "galleryLinks", type: "boolean" }, { name: "ignoreFlair", type: "string" }, { name: "linkOnly", type: "boolean" }, { name: "showFlairs", type: "boolean" }, @@ -54,7 +55,8 @@ module.exports = { /** @type {Subreddit} */ let forum = this.data.subreddits[subreddit]; if (!forum) { - const { body, statusCode } = await redditGot(`${subreddit}/about.json`); + const response = await redditGot(`${subreddit}/about.json`); + const { body, statusCode } = response; if (statusCode === 403) { return { @@ -104,7 +106,8 @@ module.exports = { } if (forum.posts.length === 0 || sb.Date.now() > forum.expiration) { - const { statusCode, body } = await redditGot(`${subreddit}/hot.json`); + const response = await redditGot(`${subreddit}/hot.json`); + const { statusCode, body } = response; if (statusCode === 403) { return { @@ -238,31 +241,33 @@ module.exports = { reply: `No eligible post found! This should not happen though, please contact @Supinic` }; } - else { - // Add the currently used post ID at the beginning of the array - repeatedPosts.unshift(post.id); - // And then splice off everything over the length of 3. - repeatedPosts.splice(config.repeats); - if (context.params.linkOnly) { - return { - reply: post.url - }; - } - - const commentsUrl = (context.params.comments) - ? `Thread: https://reddit.com/${post.commentsUrl}` - : ""; - - const symbol = (forum.quarantine) ? "⚠" : ""; - const postString = (context.platform.Name === "discord" && post.isVideoPost) - ? `https://reddit.com/${post.commentsUrl}` - : post.toString(); + // Add the currently used post ID at the beginning of the array + repeatedPosts.unshift(post.id); + // And then splice off everything over the length of 3. + repeatedPosts.splice(config.repeats); + if (context.params.linkOnly) { return { - reply: sb.Utils.fixHTML(`${symbol} r/${forum.name}: ${postString} ${commentsUrl}`) + reply: post.url }; } + + const galleryLinksString = (context.params.galleryLinks && post.hasGalleryLinks()) + ? post.getGalleryLinks().join(" ") + : ""; + const commentsUrl = (context.params.comments) + ? `Thread: https://reddit.com/${post.commentsUrl}` + : ""; + + const symbol = (forum.quarantine) ? "⚠" : ""; + const postString = (context.platform.Name === "discord" && post.isVideoPost) + ? `https://reddit.com/${post.commentsUrl}` + : post.toString(); + + return { + reply: sb.Utils.fixHTML(`${symbol} r/${forum.name}: ${postString} ${commentsUrl} ${galleryLinksString}`) + }; }), Dynamic_Description: (async function (prefix) { const { defaultMemeSubreddits } = require("./config.json"); diff --git a/commands/randommeme/post.js b/commands/randommeme/post.js index fe214d4d..14c07279 100644 --- a/commands/randommeme/post.js +++ b/commands/randommeme/post.js @@ -5,6 +5,7 @@ module.exports = class RedditPost { #title; #url; #commentsUrl; + #galleryLinks; #flairs = []; #crosspostOrigin = null; @@ -40,6 +41,19 @@ module.exports = class RedditPost { this.#stickied = Boolean(data.stickied); this.#score = data.ups ?? 0; + + this.#galleryLinks = []; + if (data.is_gallery) { + const meta = data.media_metadata; + for (const item of data.gallery_data.items) { + const mime = meta[item.media_id].m; + const ext = mime.split("/")[1]; + const link = `https://i.redd.it/${item.media_id}.${ext}`; + + this.#galleryLinks.push(link); + } + } + } get id () { return this.#id; } @@ -78,6 +92,14 @@ module.exports = class RedditPost { return this.#url.includes("v.reddit") || this.#url.includes("youtu"); } + hasGalleryLinks () { + return (this.#galleryLinks.length > 0); + } + + getGalleryLinks () { + return this.#galleryLinks; + } + toString () { let fixedUrl = `https://redd.it/${this.#id}`; if (this.hasGallery() || this.hasVideo()) {