Skip to content

Commit

Permalink
randommeme add galleryLinks parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Supinic committed Oct 1, 2024
1 parent 24189d4 commit 979b08f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
49 changes: 27 additions & 22 deletions commands/randommeme/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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");
Expand Down
22 changes: 22 additions & 0 deletions commands/randommeme/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = class RedditPost {
#title;
#url;
#commentsUrl;
#galleryLinks;

#flairs = [];
#crosspostOrigin = null;
Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit 979b08f

Please sign in to comment.