Add reddit rising, rename variables

This commit is contained in:
Zeva Rose 2022-05-01 23:06:52 -06:00
parent 4150892245
commit af42b385ab

View file

@ -247,13 +247,13 @@ class RedditCog(Scale):
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:
hot = [x async for x in subreddit.hot(limit=1)][0] post = [x async for x in subreddit.hot(limit=1)][0]
except Exception as e: except Exception as e:
self.logger.error(f"Failed to get hot from {name}", exc_info=e) self.logger.error(f"Failed to get post from {name}", exc_info=e)
await ctx.send("Well, this is awkward. Something went wrong", ephemeral=True) await ctx.send("Well, this is awkward. Something went wrong", ephemeral=True)
return return
embeds = await self.post_embeds(subreddit, hot) embeds = await self.post_embeds(subreddit, post)
await ctx.send(embeds=embeds) await ctx.send(embeds=embeds)
@reddit.subcommand(sub_cmd_name="top", sub_cmd_description="Get the top post of a subreddit") @reddit.subcommand(sub_cmd_name="top", sub_cmd_description="Get the top post of a subreddit")
@ -287,13 +287,13 @@ class RedditCog(Scale):
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:
hot = [x async for x in subreddit.top(time_filter=time, limit=1)][0] post = [x async for x in subreddit.top(time_filter=time, limit=1)][0]
except Exception as e: except Exception as e:
self.logger.error(f"Failed to get hot from {name}", exc_info=e) self.logger.error(f"Failed to get post from {name}", exc_info=e)
await ctx.send("Well, this is awkward. Something went wrong", ephemeral=True) await ctx.send("Well, this is awkward. Something went wrong", ephemeral=True)
return return
embeds = await self.post_embeds(subreddit, hot) embeds = await self.post_embeds(subreddit, post)
await ctx.send(embeds=embeds) await ctx.send(embeds=embeds)
@reddit.subcommand( @reddit.subcommand(
@ -316,13 +316,42 @@ class RedditCog(Scale):
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:
hot = await subreddit.random() post = await subreddit.random()
except Exception as e: except Exception as e:
self.logger.error(f"Failed to get hot from {name}", exc_info=e) self.logger.error(f"Failed to get post from {name}", exc_info=e)
await ctx.send("Well, this is awkward. Something went wrong", ephemeral=True) await ctx.send("Well, this is awkward. Something went wrong", ephemeral=True)
return return
embeds = await self.post_embeds(subreddit, hot) embeds = await self.post_embeds(subreddit, post)
await ctx.send(embeds=embeds)
@reddit.subcommand(
sub_cmd_name="rising", sub_cmd_description="Get a rising post of a subreddit"
)
@slash_option(
name="name", description="Subreddit name", opt_type=OptionTypes.STRING, required=True
)
async def _subreddit_rising(self, ctx: InteractionContext, name: str) -> None:
await ctx.defer()
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 rising")
await ctx.send("Subreddit may be private, quarantined, or nonexistent.", ephemeral=True)
return
try:
post = [x async for x in subreddit.rising(limit=1)][0]
except Exception as e:
self.logger.error(f"Failed to get post from {name}", exc_info=e)
await ctx.send("Well, this is awkward. Something went wrong", ephemeral=True)
return
embeds = await self.post_embeds(subreddit, post)
await ctx.send(embeds=embeds) await ctx.send(embeds=embeds)