Add reddit post, support crossposting

This commit is contained in:
Zeva Rose 2022-05-05 00:14:29 -06:00
parent 6a040a18d4
commit 828ea92188

View file

@ -13,7 +13,7 @@ from naff import Client, Cog, InteractionContext, Permissions
from naff.client.utils.misc_utils import get from naff.client.utils.misc_utils import get
from naff.models.discord.channel import ChannelTypes, GuildText from naff.models.discord.channel import ChannelTypes, GuildText
from naff.models.discord.components import ActionRow, Select, SelectOption 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 ( from naff.models.naff.application_commands import (
OptionTypes, OptionTypes,
SlashCommand, SlashCommand,
@ -51,7 +51,15 @@ class RedditCog(Cog):
await post.author.load() await post.author.load()
author_url = f"https://reddit.com/u/{post.author.name}" author_url = f"https://reddit.com/u/{post.author.name}"
images = [] 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 "url" in vars(post):
if any(post.url.endswith(x) for x in ["jpeg", "jpg", "png", "gif"]): if any(post.url.endswith(x) for x in ["jpeg", "jpg", "png", "gif"]):
images = [post.url] images = [post.url]
@ -66,9 +74,9 @@ class RedditCog(Cog):
break break
if "selftext" in vars(post) and post.selftext: if "selftext" in vars(post) and post.selftext:
content += "\n\n" + post.selftext content += post.selftext
if len(content) > 600: if len(content) > 900:
content = content[:600] + "..." content = content[:900] + "..."
content += f"\n\n[View this post]({url})" content += f"\n\n[View this post]({url})"
if not images and not content: if not images and not content:
@ -79,7 +87,7 @@ class RedditCog(Cog):
if "primary_color" in vars(sub): if "primary_color" in vars(sub):
color = sub.primary_color color = sub.primary_color
base_embed = build_embed( base_embed = build_embed(
title="", title=title,
description=content, description=content,
fields=[], fields=[],
timestamp=post.created_utc, timestamp=post.created_utc,
@ -384,6 +392,30 @@ class RedditCog(Cog):
else: else:
await ctx.send(embeds=embeds) 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: def setup(bot: Client) -> None:
"""Add RedditCog to JARVIS""" """Add RedditCog to JARVIS"""