diff --git a/ubot/modules/reddit.py b/ubot/modules/reddit.py index a169dd2..5bf3fd7 100644 --- a/ubot/modules/reddit.py +++ b/ubot/modules/reddit.py @@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-or-later import io -from random import choice +from random import choice, shuffle import praw @@ -37,6 +37,18 @@ async def titlefetcherfallback(sub): return choice(hot_list).title +async def bodyfetcherfallback(sub): + hot = REDDIT.subreddit(sub).hot() + hot_list = list(hot.__iter__()) + shuffle(hot_list) + + for i in hot_list: + if i.selftext: + return i.selftext, i.title + + return None, None + + async def imagefetcher(event, sub): image_url = False @@ -74,6 +86,27 @@ async def titlefetcher(event, sub): await event.reply(title) +async def bodyfetcher(event, sub): + await event.edit(f"`Fetching from `**r/{sub}**`…`") + + for _ in range(10): + post = REDDIT.subreddit(sub).random() + + if not post: + body, title = await titlefetcherfallback(sub) + elif post.selftext: + body = post.selftext + title = post.title + + if not body: + continue + + await event.edit(f"**{title}**\n\n{body}") + return + + await event.edit(f"`Failed to find any valid content on `**r/{sub}**`!`") + + @ldr.add(pattern="redi") async def redimg(event): sub = event.pattern_match.group(1) @@ -94,6 +127,16 @@ async def redtit(event): await event.reply("Syntax: .redt ") +@ldr.add(pattern="redb") +async def redbod(event): + sub = event.pattern_match.group(1) + + if sub: + await bodyfetcher(event, sub) + else: + await event.edit("Syntax: .redb ") + + @ldr.add(pattern="suffer") async def makemesuffer(event): await imagefetcher(event, "MakeMeSuffer")