From 828ea921880e6fadeacab6acc5fc409f0267d514 Mon Sep 17 00:00:00 2001 From: zevaryx Date: Thu, 5 May 2022 00:14:29 -0600 Subject: [PATCH] Add reddit post, support crossposting --- jarvis/cogs/reddit.py | 44 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/jarvis/cogs/reddit.py b/jarvis/cogs/reddit.py index 1da3474..2f6a900 100644 --- a/jarvis/cogs/reddit.py +++ b/jarvis/cogs/reddit.py @@ -13,7 +13,7 @@ from naff import Client, Cog, InteractionContext, Permissions from naff.client.utils.misc_utils import get from naff.models.discord.channel import ChannelTypes, GuildText from naff.models.discord.components import ActionRow, Select, SelectOption -from naff.models.discord.embed import Embed +from naff.models.discord.embed import Embed, EmbedField from naff.models.naff.application_commands import ( OptionTypes, SlashCommand, @@ -51,7 +51,15 @@ class RedditCog(Cog): await post.author.load() author_url = f"https://reddit.com/u/{post.author.name}" images = [] - content = f"**{post.title}**" + title = f"{post.title}" + fields = [] + content = "" + og_post = None + if not post.is_self: + og_post = post # noqa: F841 + post = await self.api.submission(post.crosspost_parents_list[0]["id"]) + fields.append(EmbedField(name="Crossposted From", value=post.subreddit_name_prefixed)) + content = f"> **{post.title}**\n\n" if "url" in vars(post): if any(post.url.endswith(x) for x in ["jpeg", "jpg", "png", "gif"]): images = [post.url] @@ -66,9 +74,9 @@ class RedditCog(Cog): break if "selftext" in vars(post) and post.selftext: - content += "\n\n" + post.selftext - if len(content) > 600: - content = content[:600] + "..." + content += post.selftext + if len(content) > 900: + content = content[:900] + "..." content += f"\n\n[View this post]({url})" if not images and not content: @@ -79,7 +87,7 @@ class RedditCog(Cog): if "primary_color" in vars(sub): color = sub.primary_color base_embed = build_embed( - title="", + title=title, description=content, fields=[], timestamp=post.created_utc, @@ -384,6 +392,30 @@ class RedditCog(Cog): else: await ctx.send(embeds=embeds) + @reddit.subcommand(sub_cmd_name="post", sub_cmd_description="Get a specific submission") + @slash_option( + name="sid", description="Submission ID", opt_type=OptionTypes.STRING, required=True + ) + async def _reddit_post(self, ctx: InteractionContext, sid: str) -> None: + await ctx.defer() + try: + post = await self.api.submission(sid)() + await post.load() + except (NotFound, Forbidden, Redirect) as e: + self.logger.debug(f"Submission {sid} raised {e.__class__.__name__} in post") + await ctx.send("Subreddit may be private, quarantined, or nonexistent.", ephemeral=True) + return + + embeds = await self.post_embeds(post.subreddit, post) + if post.over_18 and not ctx.channel.nsfw: + try: + await ctx.author.send(embeds=embeds) + await ctx.send("Hey! Due to content, I had to DM the result to you") + except Exception: + await ctx.send("Hey! Due to content, I cannot share the result") + else: + await ctx.send(embeds=embeds) + def setup(bot: Client) -> None: """Add RedditCog to JARVIS"""