summaryrefslogtreecommitdiff
path: root/src/reddit.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/reddit.js')
-rw-r--r--src/reddit.js23
1 files changed, 20 insertions, 3 deletions
diff --git a/src/reddit.js b/src/reddit.js
index 36cb154..183faca 100644
--- a/src/reddit.js
+++ b/src/reddit.js
@@ -35,10 +35,17 @@ function filterPostsByFlair(posts, excludedFlairs = [], includedFlairs = []) {
if (!hasMedia) return false;
- if (post.over_18 || post.link_flair_text?.toLowerCase().includes('nsfw'))
- return false;
-
const postFlair = post.link_flair_text?.toLowerCase() || '';
+ const isNSFW = post.over_18 || postFlair.includes('nsfw');
+
+ if (
+ includedFlairs.length > 0 &&
+ includedFlairs.some((flair) => flair.toLowerCase() === 'nsfw')
+ )
+ if (includedFlairs.some((flair) => flair.toLowerCase() === 'nsfw'))
+ return isNSFW;
+
+ if (isNSFW) return false;
if (includedFlairs.length > 0)
return includedFlairs.some((flair) =>
@@ -55,6 +62,9 @@ function filterPostsByFlair(posts, excludedFlairs = [], includedFlairs = []) {
}
function getRandomPost(posts) {
+ if (posts.length === 0)
+ throw new Error('No posts found matching the criteria');
+
const randomIndex = Math.floor(Math.random() * posts.length);
return posts[randomIndex];
@@ -74,4 +84,11 @@ export async function getRoleplayPost() {
return getRandomPost(filteredPosts);
}
+export async function getNSFWPost() {
+ const posts = await fetchRedditPosts();
+ const filteredPosts = filterPostsByFlair(posts, [], ['nsfw']);
+
+ return getRandomPost(filteredPosts);
+}
+
export const redditURL = 'https://www.reddit.com/r/okbuddyumamusume/hot.json';