length check & embed colour

This commit is contained in:
Corban-Lee Jones 2024-06-26 15:59:53 +01:00
parent f7ef7f1b64
commit bcb0f2470a
2 changed files with 18 additions and 10 deletions

View File

@ -11,7 +11,7 @@ from os import getenv
from time import process_time
import aiohttp
from discord import TextChannel, Embed
from discord import TextChannel, Embed, Colour
from discord import app_commands
from discord.ext import commands, tasks
from discord.errors import Forbidden
@ -146,7 +146,7 @@ class TaskCog(commands.Cog):
if not articles:
log.debug("No articles found")
embeds = await self.get_articles_as_embeds(api, session, sub.id, sub.mutators, filters, articles)
embeds = await self.get_articles_as_embeds(api, session, sub.id, sub.mutators, filters, articles, Colour.from_str("#" + sub.embed_colour))
await self.send_embeds_in_chunks(embeds, channels)
async def get_articles_as_embeds(
@ -156,7 +156,8 @@ class TaskCog(commands.Cog):
sub_id: int,
mutators: dict[str, list[dict]],
filters: list[dict],
articles: list[Article]
articles: list[Article],
embed_colour: str
) -> list[Embed]:
"""
Process articles and return their respective embeds.
@ -164,7 +165,7 @@ class TaskCog(commands.Cog):
embeds = []
for article in articles:
embed = await self.process_article(api, session, sub_id, mutators, filters, article)
embed = await self.process_article(api, session, sub_id, mutators, filters, article, embed_colour)
if embed:
embeds.append(embed)
@ -209,7 +210,8 @@ class TaskCog(commands.Cog):
sub_id: int,
mutators: dict[str, list[dict]],
filters: list[dict],
article: Article
article: Article,
embed_colour: str
) -> Embed | None:
"""
Process a given Article.
@ -242,7 +244,7 @@ class TaskCog(commands.Cog):
return
if not blocked:
return await article.to_embed(session)
return await article.to_embed(session, embed_colour)
def mutate_article(self, article: Article, mutators: list[dict]):

View File

@ -79,8 +79,7 @@ class Article:
mutator_value = mutator["value"]
if mutator_value in mutator_map:
func = mutator_map[mutator_value]
setattr(self, attr, func(getattr(self, attr)))
setattr(self, attr, mutator_map[mutator_value](getattr(self, attr)))
log.debug("mutated %s, to: %s", attr, getattr(self, attr))
else:
log.warn("Unknown mutator value '%s', skipping", mutator["value"])
@ -116,7 +115,7 @@ class Article:
image_content = image_element.get("content")
return image_content if validators.url(image_content) else None
async def to_embed(self, session: aiohttp.ClientSession) -> Embed:
async def to_embed(self, session: aiohttp.ClientSession, colour: Colour) -> Embed:
"""Creates and returns a Discord Embed object from the article.
Parameters
@ -143,12 +142,18 @@ class Article:
icon_url = self.source.icon_url if validators.url(self.source.icon_url) else None
thumb_url = await self.get_thumbnail_url(session) # validation done inside func
# Combined length validation
# Can't exceed combined 6000 characters, [400 Bad Request] if failed.
combined_length = len(title) + len(desc) + (len(author) * 2)
cutoff = combined_length - 6000
desc = shorten(desc, cutoff) if cutoff > 0 else desc
embed = Embed(
title=title,
description=desc,
timestamp=self.published,
url=embed_url,
colour=Colour.blue()
colour=colour
)
embed.set_thumbnail(url=icon_url)
@ -284,6 +289,7 @@ class Subscription(DjangoDataModel):
extra_notes: str
filters: list[int]
mutators: dict[str, list[dict]]
embed_colour: str
active: bool
channels_count: int