Add reddit random command

This commit is contained in:
Zeva Rose 2022-05-01 22:41:36 -06:00
parent 9af93435e7
commit a6a0a5364c

View file

@ -256,7 +256,7 @@ class RedditCog(Scale):
embeds = await self.post_embeds(subreddit, hot) embeds = await self.post_embeds(subreddit, hot)
await ctx.send(embeds=embeds) 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( @slash_option(
name="name", description="Subreddit name", opt_type=OptionTypes.STRING, required=True name="name", description="Subreddit name", opt_type=OptionTypes.STRING, required=True
) )
@ -283,7 +283,7 @@ class RedditCog(Scale):
subreddit = await self.api.subreddit(name) subreddit = await self.api.subreddit(name)
await subreddit.load() await subreddit.load()
except (NotFound, Forbidden, Redirect) as e: 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) await ctx.send("Subreddit may be private, quarantined, or nonexistent.", ephemeral=True)
return return
try: try:
@ -296,6 +296,34 @@ class RedditCog(Scale):
embeds = await self.post_embeds(subreddit, hot) embeds = await self.post_embeds(subreddit, hot)
await ctx.send(embeds=embeds) 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: def setup(bot: Snake) -> None:
"""Add RedditCog to JARVIS""" """Add RedditCog to JARVIS"""