aboutsummaryrefslogtreecommitdiffstats
path: root/bot/exts/evergreen/reddit.py
diff options
context:
space:
mode:
Diffstat (limited to 'bot/exts/evergreen/reddit.py')
-rw-r--r--bot/exts/evergreen/reddit.py37
1 files changed, 20 insertions, 17 deletions
diff --git a/bot/exts/evergreen/reddit.py b/bot/exts/evergreen/reddit.py
index 2be511c8..bda155c3 100644
--- a/bot/exts/evergreen/reddit.py
+++ b/bot/exts/evergreen/reddit.py
@@ -3,8 +3,8 @@ import random
import discord
from discord.ext import commands
-from discord.ext.commands.cooldowns import BucketType
+from bot.bot import Bot
from bot.utils.pagination import ImagePaginator
log = logging.getLogger(__name__)
@@ -13,25 +13,25 @@ log = logging.getLogger(__name__)
class Reddit(commands.Cog):
"""Fetches reddit posts."""
- def __init__(self, bot: commands.Bot):
+ def __init__(self, bot: Bot):
self.bot = bot
async def fetch(self, url: str) -> dict:
"""Send a get request to the reddit API and get json response."""
session = self.bot.http_session
params = {
- 'limit': 50
+ "limit": 50
}
headers = {
- 'User-Agent': 'Iceman'
+ "User-Agent": "Iceman"
}
async with session.get(url=url, params=params, headers=headers) as response:
return await response.json()
- @commands.command(name='reddit')
- @commands.cooldown(1, 10, BucketType.user)
- async def get_reddit(self, ctx: commands.Context, subreddit: str = 'python', sort: str = "hot") -> None:
+ @commands.command(name="reddit")
+ @commands.cooldown(1, 10, commands.BucketType.user)
+ async def get_reddit(self, ctx: commands.Context, subreddit: str = "python", sort: str = "hot") -> None:
"""
Fetch reddit posts by using this command.
@@ -45,20 +45,23 @@ class Reddit(commands.Cog):
await ctx.send(f"Invalid sorting: {sort}\nUsing default sorting: `Hot`")
sort = "hot"
- data = await self.fetch(f'https://www.reddit.com/r/{subreddit}/{sort}/.json')
+ data = await self.fetch(f"https://www.reddit.com/r/{subreddit}/{sort}/.json")
try:
posts = data["data"]["children"]
except KeyError:
- return await ctx.send('Subreddit not found!')
+ await ctx.send("Subreddit not found!")
+ return
if not posts:
- return await ctx.send('No posts available!')
+ await ctx.send("No posts available!")
+ return
if posts[0]["data"]["over_18"] is True:
- return await ctx.send(
+ await ctx.send(
"You cannot access this Subreddit as it is meant for those who "
"are 18 years or older."
)
+ return
embed_titles = ""
@@ -80,7 +83,7 @@ class Reddit(commands.Cog):
for i, post in enumerate(random_posts, start=1):
post_title = post["data"]["title"][0:50]
- post_url = post['data']['url']
+ post_url = post["data"]["url"]
if post_title == "":
post_title = "No Title."
elif post_title == post_url:
@@ -102,12 +105,12 @@ class Reddit(commands.Cog):
post_stats = f"{image_emoji} "
image_url = post_url
- votes = f'{upvote_emoji}{post["data"]["ups"]}'
- comments = f'{comment_emoji}\u2002{ post["data"]["num_comments"]}'
+ votes = f"{upvote_emoji}{post['data']['ups']}"
+ comments = f"{comment_emoji}\u2002{ post['data']['num_comments']}"
post_stats += (
f"\u2002{votes}\u2003"
f"{comments}"
- f'\u2003{user_emoji}\u2002{post["data"]["author"]}\n'
+ f"\u2003{user_emoji}\u2002{post['data']['author']}\n"
)
embed_titles += f"{post_stats}\n"
page_text = f"**[{post_title}]({post_url})**\n{post_stats}\n{post['data']['selftext'][0:200]}"
@@ -123,6 +126,6 @@ class Reddit(commands.Cog):
await ImagePaginator.paginate(pages, ctx, embed)
-def setup(bot: commands.Bot) -> None:
- """Load the Cog."""
+def setup(bot: Bot) -> None:
+ """Load the Reddit cog."""
bot.add_cog(Reddit(bot))