diff --git a/jarvis/cogs/reddit.py b/jarvis/cogs/reddit.py index 4d6a137..e632d33 100644 --- a/jarvis/cogs/reddit.py +++ b/jarvis/cogs/reddit.py @@ -256,7 +256,7 @@ class RedditCog(Scale): embeds = await self.post_embeds(subreddit, hot) await ctx.send(embeds=embeds) - @reddit.subcommand(sub_cmd_name="top", sub_cmd_description="Get the hot post of a subreddit") + @reddit.subcommand(sub_cmd_name="top", sub_cmd_description="Get the top post of a subreddit") @slash_option( name="name", description="Subreddit name", opt_type=OptionTypes.STRING, required=True ) @@ -283,7 +283,7 @@ class RedditCog(Scale): subreddit = await self.api.subreddit(name) await subreddit.load() except (NotFound, Forbidden, Redirect) as e: - self.logger.debug(f"Subreddit {name} raised {e.__class__.__name__} in hot") + self.logger.debug(f"Subreddit {name} raised {e.__class__.__name__} in top") await ctx.send("Subreddit may be private, quarantined, or nonexistent.", ephemeral=True) return try: @@ -296,6 +296,34 @@ class RedditCog(Scale): embeds = await self.post_embeds(subreddit, hot) await ctx.send(embeds=embeds) + @reddit.subcommand( + sub_cmd_name="random", sub_cmd_description="Get a random post of a subreddit" + ) + @slash_option( + name="name", description="Subreddit name", opt_type=OptionTypes.STRING, required=True + ) + async def _subreddit_random(self, ctx: InteractionContext, name: str) -> None: + name = name.replace("r/", "") + if len(name) > 20 or len(name) < 3: + await ctx.send("Invalid Subreddit name", ephemeral=True) + return + try: + subreddit = await self.api.subreddit(name) + await subreddit.load() + except (NotFound, Forbidden, Redirect) as e: + self.logger.debug(f"Subreddit {name} raised {e.__class__.__name__} in random") + await ctx.send("Subreddit may be private, quarantined, or nonexistent.", ephemeral=True) + return + try: + hot = await subreddit.random() + except Exception as e: + self.logger.error(f"Failed to get hot from {name}", exc_info=e) + await ctx.send("Well, this is awkward. Something went wrong", ephemeral=True) + return + + embeds = await self.post_embeds(subreddit, hot) + await ctx.send(embeds=embeds) + def setup(bot: Snake) -> None: """Add RedditCog to JARVIS"""