diff --git a/config/.platforms.template b/config/.platforms.template index 97db691..c109197 100644 --- a/config/.platforms.template +++ b/config/.platforms.template @@ -2,4 +2,5 @@ [REDDIT] CLIENT_ID = CLIENT_SECRET = -USER_AGENT = Small discord bot to embed posts (given by url) into an standardized format \ No newline at end of file +USER_AGENT = Small discord bot to embed posts (given by url) into an standardized format +EMBED_COLOR = 0xff4500 \ No newline at end of file diff --git a/src/cogs/post.py b/src/cogs/post.py new file mode 100644 index 0000000..b63d4ca --- /dev/null +++ b/src/cogs/post.py @@ -0,0 +1,73 @@ +import discord +from discord import app_commands +from discord.ext import commands +from cogs.base_cog import Base_Cog +from cogs.maintenance import Maintenance_Command + +import logging +from urllib.parse import urlparse +from praw.models import Submission, Subreddit +from utils.portal import Portal + +class Post_Command(Base_Cog): + def __init__(self, bot:commands.Bot): + self.__bot = bot + super().__init__(logging.getLogger("cmds.post")) + + @app_commands.command(name = "post", description = "Post an embed in the Current Channel with a link to the content") + async def post(self, ctx:discord.Interaction, url:str): + domain_info = urlparse(url) + portal = Portal.instance() + toplevel_domain = '.'.join(domain_info.netloc.split('.')[-2:]) + match toplevel_domain: + case "reddit.com": + subm:Submission = portal.reddit_adapter.fetch(url) + embeds = [] + + image_urls = [] + # Check if submission has a gallery + if hasattr(subm, "media_metadata"): + for media_id, media in subm.media_metadata.items(): + file_extension = media["m"].split("/")[1] + image_urls.append(f"https://i.redd.it/{media_id}.{file_extension}") + else: + image_urls.append(subm.url) + self._logger.debug(f"Found {len(image_urls)} image urls for the post") + + embed = discord.Embed(url = "https://discord.com/humans.txt", color = int(portal.platforms_config["REDDIT"]["embed_color"], 16)) + embed.set_author(name = subm.title) + embeds.append(embed) + + embed.add_field( + name = "Author", + value = f"[u/{subm.author}](https://www.reddit.com/user/{subm.author})", + inline = True) + embed.add_field( + name = "Subreddit", + value = f"[r/{subm.subreddit.display_name}]({url})", + inline = True) + if subm.selftext: + embed.add_field( + name = "Discription", + value = subm.selftext, + inline = False) + + for image_url in image_urls: + embed = discord.Embed(url = "https://discord.com/humans.txt") + embed.set_image(url = image_url) + embeds.append(embed) + + await ctx.response.send_message(embeds = embeds) + + # No domain for seperation found + case _: + embed = discord.Embed( + title = "Domain not found", + description = f"The requested domain `{toplevel_domain}` is currently not supported\nOpen [an issue](https://github.com/official-Cromatin/Post-It/issues/new?assignees=&labels=feature-request&projects=&template=feature_request.yml) to request support for it.\n\nSee an list of supported platforms with the `/platforms` command", + color = 0xED4337) + + await ctx.response.send_message(embed = embed) + + +async def setup(bot:commands.Bot): + await bot.add_cog(Post_Command(bot)) \ No newline at end of file diff --git a/src/utils/portal.py b/src/utils/portal.py index 52e2f26..e67701f 100644 --- a/src/utils/portal.py +++ b/src/utils/portal.py @@ -4,7 +4,7 @@ from platforms.reddit import Reddit_Adapter @Singleton class Portal: - PROGRAM_VERSION = "0.2" + PROGRAM_VERSION = "0.3" bot_config:Advanced_ConfigParser = None platforms_config:Advanced_ConfigParser = None STARTUP_TIMESTAMP:float = None