diff options
author | 2019-10-20 21:15:06 +1100 | |
---|---|---|
committer | 2019-10-20 21:15:06 +1100 | |
commit | e84e760af56e9149d7c8f19279ef17a933de8f19 (patch) | |
tree | 594bbbfa8499a21a614bb60dae6754695ab5e2d4 | |
parent | Utilise `tasks.loop` and a webhook for reddit postings (diff) |
Apply suggestions from review, and add correct webhook ID.
- More informative docstrings for `Reddit.channel` and `get_top_posts`
- Add the `amount` parameter, defaulting to 5.
- Pin a max of 12 weeks worth of top posts.
-rw-r--r-- | bot/cogs/reddit.py | 24 | ||||
-rw-r--r-- | config-default.yml | 2 |
2 files changed, 15 insertions, 11 deletions
diff --git a/bot/cogs/reddit.py b/bot/cogs/reddit.py index 58a6d3c7e..7749d237f 100644 --- a/bot/cogs/reddit.py +++ b/bot/cogs/reddit.py @@ -44,7 +44,7 @@ class Reddit(Cog): @property def channel(self) -> TextChannel: - """Returns the #reddit channel object from the bot's cache.""" + """Get the #reddit channel object from the bot's cache.""" return self.bot.get_channel(Channels.reddit) async def fetch_posts(self, route: str, *, amount: int = 25, params: dict = None) -> List[dict]: @@ -74,17 +74,21 @@ class Reddit(Cog): log.debug(f"Invalid response from: {url} - status code {response.status}, mimetype {response.content_type}") return list() # Failed to get appropriate response within allowed number of retries. - async def get_top_posts(self, subreddit: Subreddit, time: str = "all") -> Embed: - """Returns an embed for the top posts of the given subreddit.""" - # Create the new spicy embed. + async def get_top_posts(self, subreddit: Subreddit, time: str = "all", amount: int = 5) -> Embed: + """ + Get the top amount of posts for a given subreddit within a specified timeframe. + + A time of "all" will get posts from all time, "day" will get top daily posts and "week" will get the top + weekly posts. + + The amount should be between 0 and 25 as Reddit's JSON requests only provide 25 posts at most. + """ embed = Embed(description="") posts = await self.fetch_posts( route=f"{subreddit}/top", - amount=5, - params={ - "t": time - } + amount=amount, + params={"t": time} ) if not posts: @@ -156,10 +160,10 @@ class Reddit(Cog): log.warning("Failed to get #reddit channel to remove pins in the weekly loop.") return - # Remove the oldest pins so that only 5 remain at most. + # Remove the oldest pins so that only 12 remain at most. pins = await self.channel.pins() - while len(pins) >= 5: + while len(pins) >= 12: await pins[-1].unpin() del pins[-1] diff --git a/config-default.yml b/config-default.yml index 52e0b880c..c97072d57 100644 --- a/config-default.yml +++ b/config-default.yml @@ -141,7 +141,7 @@ guild: webhooks: talent_pool: 569145364800602132 big_brother: 569133704568373283 - reddit: 123456789 + reddit: 635408384794951680 filter: |