maple/messageresponder/mangadex.py
2024-04-06 18:32:49 +02:00

110 lines
5.3 KiB
Python

from datetime import datetime, timedelta
from interactions.models.discord import Embed
from interactions.api.events import MessageCreate
from interactions import EmbedAttachment, EmbedFooter, EmbedField
from api.mangadex import search_manga
# We don't want to spam the API too much
timeouts = {
"mangadex": datetime.now()
}
async def send_manga_response(event: MessageCreate, match: str) -> None:
"""
Searches for a manga on mangadex.org and sends the result
as an embed into the channel that the event originated from
:param event:The MessageCreate event
:param match:The manga title matched from the event's message
"""
if datetime.now() < timeouts["mangadex"]:
message = await event.message.channel.send("Globální timeout je 5 sekund")
await message.delete(delay=5)
return
timeouts["mangadex"] = datetime.now() + timedelta(seconds=5)
content_rating = ["safe", "suggestive"]
if event.message.channel.nsfw:
content_rating.append("erotica")
content_rating.append("pornography")
m = search_manga(match, content_rating)
if m == {}:
message = await event.message.channel.send(
embed=Embed(title="Chyba!", color="#e0351f", description="Nic nenalezeno :("))
await message.delete(5)
return
manga_em = Embed(title=f'{m["attributes"]["title"]["en"]} ',
url=f"https://mangadex.org/title/{m['id']}",
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="Cílová skupina",
value="Neznámá" if m["attributes"]["publicationDemographic"] is None else m["attributes"][
"publicationDemographic"].capitalize(),
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
),
],
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 m["cover"] is None else f"https://uploads.mangadex"
f".org/covers/{m['id']}/{m['cover']}")])
await event.message.channel.send(embed=manga_em)