From 7cfa6650609d172cee3be78b2ea1e924d69470bf Mon Sep 17 00:00:00 2001 From: Marco Thomas Date: Fri, 9 Oct 2020 11:25:12 +0200 Subject: [PATCH] Add sorting to reddit.py --- cogs/admin.py | 12 ++++++------ cogs/help.py | 3 ++- cogs/reddit.py | 23 +++++++++++++++++------ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/cogs/admin.py b/cogs/admin.py index 2e2f11d..75e5a87 100644 --- a/cogs/admin.py +++ b/cogs/admin.py @@ -20,9 +20,9 @@ class Admin(commands.Cog): try: self.bot.load_extension(cog) except Exception as e: - await ctx.send(f'**`ERROR:`** {type(e).__name__} - {e}') + await ctx.send(f'**ERROR:** {type(e).__name__} - {e}') else: - await ctx.send('**`SUCCESS`**') + await ctx.send('**SUCCESS**') @commands.command(name="unload", hidden=True) @@ -34,9 +34,9 @@ class Admin(commands.Cog): try: self.bot.unload_extension(cog) except Exception as e: - await ctx.send(f'**`ERROR:`** {type(e).__name__} - {e}') + await ctx.send(f'**ERROR:** {type(e).__name__} - {e}') else: - await ctx.send('**`SUCCESS`**') + await ctx.send('**SUCCESS**') @commands.command(name="reload", hidden=True) @@ -49,9 +49,9 @@ class Admin(commands.Cog): self.bot.unload_extension(cog) self.bot.load_extension(cog) except Exception as e: - await ctx.send(f'**`ERROR:`** {type(e).__name__} - {e}') + await ctx.send(f'**ERROR:** {type(e).__name__} - {e}') else: - await ctx.send('**`SUCCESS`**') + await ctx.send('**SUCCESS**') def setup(bot): diff --git a/cogs/help.py b/cogs/help.py index c7cf2f1..23771a7 100644 --- a/cogs/help.py +++ b/cogs/help.py @@ -24,8 +24,9 @@ class Help(commands.Cog): embed.add_field(name="Owner", value=self.bot.AppInfo.owner, inline=True) embed.add_field(name="Command Prefix", value=PREFIX, inline=True) embed.add_field(name="Source Code", value="[GitHub](https://github.com/crammk/aquabot)", inline=True) + embed.add_field(name="Help", value="Use the 'help' to get a list of commands!") - footer_text = "This Bot is a project by [MarcMK](https://marcmk.de)." + footer_text = "This Bot is a project by MarcMK." embed.set_footer(text=footer_text) await ctx.send(embed=embed) diff --git a/cogs/reddit.py b/cogs/reddit.py index 5e7d294..5fa8d64 100644 --- a/cogs/reddit.py +++ b/cogs/reddit.py @@ -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):