aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar RohanJnr <[email protected]>2020-10-01 15:27:50 +0530
committerGravatar RohanJnr <[email protected]>2020-10-01 15:27:50 +0530
commitd8fbeedb7ec42b387c7f32d15e45675f987f427b (patch)
tree24264a61cf4e3e5c116696193338a76f9f0fbaaa
parentFilter out reddit posts which are meant for users 18 years of older and send ... (diff)
handling empty list error in get_top_posts() method and filter posts using list comprehension.
-rw-r--r--bot/exts/info/reddit.py32
1 files changed, 9 insertions, 23 deletions
diff --git a/bot/exts/info/reddit.py b/bot/exts/info/reddit.py
index f2aecc498..c6aecaa20 100644
--- a/bot/exts/info/reddit.py
+++ b/bot/exts/info/reddit.py
@@ -141,31 +141,14 @@ class Reddit(Cog):
content = await response.json()
posts = content["data"]["children"]
- for post in posts:
- if post["data"]["over_18"]:
- posts.remove(post)
-
- if not posts:
- resp_not_allowed = [
- {
- "error": "Oops ! Looks like this subreddit, doesn't fit in the scope of the server."
- }
- ]
- return resp_not_allowed
- return posts[:amount]
+ filtered_posts = [post for post in posts if not post["data"]["over_18"]]
+
+ return filtered_posts[:amount]
await asyncio.sleep(3)
log.debug(f"Invalid response from: {url} - status code {response.status}, mimetype {response.content_type}")
- resp_failed = [
- {
- "error": (
- "Sorry! We couldn't find any posts from that subreddit. "
- "If this problem persists, please let us know."
- )
- }
- ]
- return resp_failed # Failed to get appropriate response within allowed number of retries.
+ return list()
async def get_top_posts(self, subreddit: Subreddit, time: str = "all", amount: int = 5) -> Embed:
"""
@@ -183,10 +166,13 @@ class Reddit(Cog):
amount=amount,
params={"t": time}
)
- if "error" in posts[0]:
+ if not posts:
embed.title = random.choice(ERROR_REPLIES)
embed.colour = Colour.red()
- embed.description = posts[0]["error"]
+ embed.description = (
+ "Sorry! We couldn't find any SFW posts from that subreddit. "
+ "If this problem persists, please let us know."
+ )
return embed