from interactions.models.discord import Embed from interactions.api.events import MessageCreate from interactions import EmbedAttachment, EmbedFooter, EmbedField import re from datetime import datetime from api.mangadex import get_manga, search_manga, get_chapter async def update_manga_thread(event: MessageCreate): chapter_discussion = [x.name for x in event.message.channel.applied_tags] in ["Diskuze o kapitole"] # ! Cannot get tags correctly for some reason find_link = re.findall(r"https://mangadex\.org/(chapter|title)/([0-9a-fA-F\-]+)", event.message.channel.initial_post.content) if len(find_link) == 0: # If no link is found, try to manually find the manga if not chapter_discussion: # Discusses a manga series m = search_manga(event.message.channel.name, content_rating=["safe", "suggestive"]) else: # TODO return else: # Get info for the specifically linked manga/chapter if find_link[0][0] == "chapter": m = get_chapter(find_link[0][1]) else: m = get_manga(find_link[0][1]) if m == {}: return chapter_title = None cover = None if m["type"] == "chapter" and chapter_discussion: # This runs if the forum thread is a chapter discussion: sets the chapter title if it exists and sends the # chapter-specific embed url = f"https://mangadex.org/chapter/{m['id']}" manga = [x for x in m["relationships"] if x["type"] == "manga"][0] chapter_title = m["attributes"]["title"] published_at = datetime.strptime(m["attributes"]["publishAt"], "%y-%m-%dT%H:%M:%S+%:z") if len([x for x in manga["attributes"]["tags"] if x["attributes"]["group"] == "format"]) == 0: oneshot = False else: oneshot = True groups = ', '.join([x["attributes"]["name"] for x in m["relationships"] if x["type"] == "scanlation_group"]) title = manga["attributes"]["title"]["en"] fields = [ EmbedField( name="Manga", value=manga["attributes"]["title"]["en"], inline=True ), EmbedField( name="Datum vydání", value=published_at.strftime("%A, %-d. %b %Y"), inline=True ), EmbedField( name="Vydala skupina", value=groups, inline=True ), EmbedField( name="Počet stran", value=m["attributes"]["pages"], inline=True ), ] await event.message.channel.edit( name=f'{manga["attributes"]["title"]["en"]}' if oneshot else f'{manga["attributes"]["title"]["en"]}, kapitola {m["attributes"]["chapter"]}') else: # otherwise sends the standard manga embed if m["type"] == "chapter": m = get_manga([x for x in m["relationships"] if x["type"] == "manga"][0]["id"]) url = f"https://mangadex.org/title/{m['id']}" await event.message.channel.edit(name=f'{m["attributes"]["title"]["en"]}') cover = m["cover"] title = m["attributes"]["title"]["en"] fields = [ EmbedField( name="Autor", value="Neznámý" if len(m["author"]) == 0 else m["author"][0], inline=True ), EmbedField( name="Kresby", value="Neznámý" if len(m["artist"]) == 0 else m["artist"][0], inline=True ), EmbedField( name="Značky", value=', '.join(m["tags"]), inline=True ), EmbedField( name="Stav", value="Vychází" if m["attributes"]["status"] == "ongoing" else "Kompletní" if m["attributes"]["status"] == "completed" else "Zrušeno" if m["attributes"]["status"] == "cancelled" else "Vydávání pozastaveno", inline=True ), EmbedField( name="Hodnocení obsahu", value="Nezávadné" if m["attributes"]["contentRating"] == "safe" else "Lechtivé" if m["attributes"]["contentRating"] == "suggestive" else "Erotika" if m["attributes"]["contentRating"] == "erotica" else "Pornografie", inline=True ), EmbedField( name="Rok vydání", value="Neznámý" if m["attributes"]["year"] is None else str(m["attributes"]["year"]), inline=True ), EmbedField( name="Poslední kapitola", value="Není" if m["attributes"]["lastChapter"] == "" else f'Kapitola {m["attributes"]["lastChapter"]}' if m["attributes"]["lastVolume"] == "" else f'Svazek {m["attributes"]["lastVolume"]}; kapitola {m["attributes"]["lastChapter"]}', inline=True ), EmbedField( name="Originální jazyk", value=m["originalLang"], inline=True ), EmbedField( name="Průměrné hodnocení", value=str(round(m["rating"]["bayesian"], 2)), inline=True ), EmbedField( name="Sledujících", value=str(m["follows"]), inline=True ), ] manga_em = Embed(title=f'{chapter_title if chapter_discussion and chapter_title is not None else title} ', url=url, fields=fields, footer=EmbedFooter(text="Informace z mangadex.org", icon_url="https://mangadex.org/pwa/icons" "/icon-180.png"), description=m["attributes"]["description"]["en"], images=[EmbedAttachment(url=None if cover is None else f"https://uploads.mangadex" f".org/covers/{m['id']}/{cover}")]) # TODO: add X button so user can delete the embed if it's not the correct manga or smthn await event.message.channel.send(embed=manga_em)