Added the post command

This commit is contained in:
2024-10-30 12:25:01 +01:00
parent fb17b8d402
commit 5f5de1d43d
3 changed files with 76 additions and 2 deletions
+1
View File
@@ -3,3 +3,4 @@
CLIENT_ID = <PASTE YOUR CLIENT ID HERE>
CLIENT_SECRET = <PASTE YOUR CLIENT SECRET HERE>
USER_AGENT = Small discord bot to embed posts (given by url) into an standardized format
EMBED_COLOR = 0xff4500
+73
View File
@@ -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))
+1 -1
View File
@@ -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