added embeds, upated manga.py, TODO-implement embeds

This commit is contained in:
franchioping 2024-07-23 00:32:44 +01:00
parent 1753c20142
commit d31df614e5
3 changed files with 104 additions and 14 deletions

52
embed_util.py Normal file
View File

@ -0,0 +1,52 @@
from manga import Manga, Chapter
import discord
class ListManga(discord.ui.View):
def __init__(self, manga_list: list[Manga]):
super().__init__()
self.ret = []
self.index = 0
self.manga_list = manga_list
async def print_manga(self, interaction: discord.Interaction):
await interaction.response.defer()
await interaction.message.edit(embed=manga_embed(self.manga_list[self.index]))
@discord.ui.button(label='Prev', style=discord.ButtonStyle.grey)
async def previous(self, interaction: discord.Interaction, button: discord.ui.Button):
if self.index > 0:
self.index -= 1
await self.print_manga(interaction)
@discord.ui.button(label='Next', style=discord.ButtonStyle.grey)
async def next(self, interaction: discord.Interaction, button: discord.ui.Button):
if self.index < len(self.manga_list):
self.index += 1
await self.print_manga(interaction)
@discord.ui.button(label='Add', style=discord.ButtonStyle.green)
async def add(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
self.ret.append({"manga": self.manga_list[self.index], "action": 1})
@discord.ui.button(label='Remove', style=discord.ButtonStyle.red)
async def remove(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
self.ret.append({"manga": self.manga_list[self.index], "action": -1})
@discord.ui.button(label='Done', style=discord.ButtonStyle.blurple)
async def exit(self, interaction: discord.Interaction, button: discord.ui.Button):
if len(self.ret) > 0:
await interaction.response.send_message("Your list has been altered successfully", ephemeral=True)
else:
await interaction.response.send_message("You didn't make any changes", ephemeral=True)
self.stop()
def manga_embed(manga: Manga):
e = discord.Embed(title=manga.get_title(), description=manga.get_description(), url=manga.get_url())
e.set_thumbnail(url=manga.get_cover_art_url())
return e

15
main.py
View File

@ -14,11 +14,26 @@ tree = app_commands.CommandTree(client)
@tree.command(
name="ping",
description="My first application Command",
guild=discord.Object(id=1042133536926347324)
)
async def first_command(interaction: discord.Interaction):
chanel = await interaction.user.create_dm()
await chanel.send("Hi")
@tree.command(
name="search",
description="Search for manga to follow",
guild=discord.Object(id=1042133536926347324)
)
@app_commands.describe(title='Title of the manga to search for')
async def first_command(
interaction: discord.Interaction,
title: str
):
await interaction.response.send_message("Check your DM's")
chanel = await interaction.user.create_dm()
await chanel.send(f"Hey, you searched for {title}")
@client.event
async def on_message(message: discord.Message):

View File

@ -11,18 +11,21 @@ class Chapter:
def __init__(self, id: str, _base_url=base_url):
self.id = id
self.base_url = _base_url
self.data = None
def _get_self(self):
self.update_data()
def update_data(self):
r = requests.get(
f"{self.base_url}/chapter/{self.id}"
)
return r.json()["data"]
self.data = r.json()["data"]
def get_chapter_num(self) -> str:
return self._get_self()["attributes"]["chapter"]
return self.data["attributes"]["chapter"]
def get_chapter_url(self) -> str:
return self._get_self()["attributes"]["readableAt"]
return self.data["attributes"]["readableAt"]
def __str__(self):
return str({
@ -34,18 +37,43 @@ class Manga:
def __init__(self, id: str, _base_url=base_url):
self.id = id
self.base_url = _base_url
self.data = None
def _get_self(self):
self.update_data()
def update_data(self):
r = requests.get(
f"{self.base_url}/manga/{self.id}"
)
return r.json()["data"]
self.data = r.json()["data"]
def get_title(self) -> str:
return self._get_self()["attributes"]["title"]
return self.data["attributes"]["title"]
def get_latest_chap(self) -> Chapter:
return Chapter(self._get_self()["attributes"]["latestUploadedChapter"])
return Chapter(self.data["attributes"]["latestUploadedChapter"])
def get_cover_art_url(self):
cover_art_id = ""
for relation in self.data["relationships"]:
if relation["type"] == "cover_art":
cover_art_id = relation["id"]
r = requests.get(f"{self.base_url}/cover/{cover_art_id}")
print( r.json()["data"])
cover_id = r.json()["data"]["attributes"]["fileName"]
return f"https://mangadex.org/covers/{self.id}/{cover_id}"
def get_description(self) -> str:
if "en" in self.data["attributes"]["description"].keys():
return self.data["attributes"]["description"]["en"]
else:
return self.data["attributes"]["description"][self.data["attributes"]["description"].keys()[0]]
def get_url(self) -> str:
return f"https://mangadex.org/title/{self.id}"
def __str__(self):
return str({
@ -71,13 +99,8 @@ class MangaHandler:
return manga_list
def get_chapter(self, chapter_id: str):
r = requests.get(
f"{self.base_url}/chapter/{chapter_id}"
)
return r.json()["data"]
if __name__ == "__main__":
mh = MangaHandler()
print(mh.search("Release That Witch")[0].get_latest_chap().get_chapter_num())
print(mh.search("Release That Witch")[0].get_url())