diff --git a/jarvis/cogs/reddit.py b/jarvis/cogs/reddit.py index 1456a09..4d6a137 100644 --- a/jarvis/cogs/reddit.py +++ b/jarvis/cogs/reddit.py @@ -15,6 +15,7 @@ from dis_snek.models.discord.embed import Embed from dis_snek.models.snek.application_commands import ( OptionTypes, SlashCommand, + SlashCommandChoice, slash_option, ) from dis_snek.models.snek.command import check @@ -255,6 +256,46 @@ 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") + @slash_option( + name="name", description="Subreddit name", opt_type=OptionTypes.STRING, required=True + ) + @slash_option( + name="time", + description="Top time", + opt_type=OptionTypes.STRING, + required=False, + choices=[ + SlashCommandChoice(name="All", value="all"), + SlashCommandChoice(name="Day", value="day"), + SlashCommandChoice(name="Hour", value="hour"), + SlashCommandChoice(name="Month", value="month"), + SlashCommandChoice(name="Week", value="week"), + SlashCommandChoice(name="Year", value="year"), + ], + ) + async def _subreddit_top(self, ctx: InteractionContext, name: str, time: str = "all") -> 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 hot") + await ctx.send("Subreddit may be private, quarantined, or nonexistent.", ephemeral=True) + return + try: + hot = [x async for x in subreddit.top(time_filter=time, limit=1)][0] + 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"""