Add sorting to reddit.py

This commit is contained in:
Marco Thomas
2020-10-09 11:25:12 +02:00
parent 59794f8d52
commit 7cfa665060
3 changed files with 25 additions and 13 deletions

View File

@@ -18,22 +18,33 @@ class Reddit(commands.Cog):
@commands.command(name="reddit")
async def reddit(self, ctx, sub: str):
async def reddit(self, ctx, sub: str, sorting="hot", time_filter="all"):
"""
Send a post from a subreddit to chat
Send a post from a subreddit to chat. Timefilters: all, day, hour, month, week, year
"""
reddit = praw.Reddit(client_id=REDDIT_CLIENT_ID,
client_secret=REDDIT_CLIENT_SECRET,
user_agent=REDDIT_CLIENT_USERAGENT)
posts = reddit.subreddit(sub).hot()
try:
if sorting == "hot":
posts = reddit.subreddit(sub).hot()
elif sorting == "top":
posts = reddit.subreddit(sub).top(time_filter)
elif sorting == "new":
posts = reddit.subreddit(sub).new()
except ValueError as e:
await ctx.send(f"Invalid Argument: {e}")
# WORKS
rand_post = random.randint(1, 100)
# Make sure you're not sending a pinned post
for i in range(0, rand_post):
post = next(x for x in posts if not x.stickied)
submission = next(x for x in posts if not x.stickied)
await ctx.send(f"> '{post.title}' by {post.author.name}")
await ctx.send(post.url)
await ctx.send(f"'{submission.title}' by {submission.author.name} - 🔼 {submission.score}")
await ctx.send(submission.url)
def setup(bot):