TODO - fix buttons

This commit is contained in:
franchioping 2024-07-24 03:24:11 +01:00
parent b0230a6d88
commit 3c1759bf51
4 changed files with 36 additions and 14 deletions

View File

@ -27,6 +27,7 @@ class ListManga(discord.ui.View):
attachments=[self.manga_files[self.index]] attachments=[self.manga_files[self.index]]
) )
@discord.ui.button(label='Prev', style=discord.ButtonStyle.grey) @discord.ui.button(label='Prev', style=discord.ButtonStyle.grey)
async def previous(self, interaction: discord.Interaction, button: discord.ui.Button): async def previous(self, interaction: discord.Interaction, button: discord.ui.Button):
if self.index > 0: if self.index > 0:
@ -36,7 +37,7 @@ class ListManga(discord.ui.View):
@discord.ui.button(label='Next', style=discord.ButtonStyle.grey) @discord.ui.button(label='Next', style=discord.ButtonStyle.grey)
async def next(self, interaction: discord.Interaction, button: discord.ui.Button): async def next(self, interaction: discord.Interaction, button: discord.ui.Button):
if self.index < len(self.manga_list): if self.index < len(self.manga_list) - 1:
self.index += 1 self.index += 1
await self.print_manga(interaction) await self.print_manga(interaction)
@ -77,9 +78,11 @@ def get_chapter_files(manga: Manga):
def chapter_embed(manga: Manga, chapter: Chapter): def chapter_embed(manga: Manga, chapter: Chapter):
volume_info = f"Volume {chapter.get_volume()}" if chapter.get_volume() else ""
chapter_title = f"{chapter.get_title()}" if chapter.get_title() else ""
e = discord.Embed( e = discord.Embed(
title=f'New "{manga.get_title()}" Chapter Released!', title=f'"{manga.get_title()}" Chapter {chapter_title} Released!',
description=f"Chapter {chapter.get_number()} of {manga.get_title()} Released." description=f"{volume_info} Chapter {chapter.get_number()} of {manga.get_title()} Released."
f"\nGo read it now!", f"\nGo read it now!",
url=chapter.get_url() url=chapter.get_url()
) )

19
main.py
View File

@ -31,10 +31,13 @@ async def first_command(interaction: discord.Interaction):
async def render_manga_list_in_dm(interaction: discord.Interaction, manga_list: list[manga_api.Manga]): async def render_manga_list_in_dm(interaction: discord.Interaction, manga_list: list[manga_api.Manga]):
await interaction.response.send_message("Check your DM's") await interaction.followup.send("Check your DM's")
chanel = await interaction.user.create_dm() chanel = await interaction.user.create_dm()
if len(manga_list) == 0:
await chanel.send("No Manga in Here")
return
view = embed_util.ListManga(manga_list) view = embed_util.ListManga(manga_list)
msg = await chanel.send(view=view, embed=embed_util.manga_embed(manga_list[0])) msg = await chanel.send(view=view, embed=embed_util.manga_embed(manga_list[0]))
await view.force_reload(msg) await view.force_reload(msg)
@ -55,6 +58,7 @@ async def render_manga_list_in_dm(interaction: discord.Interaction, manga_list:
await chanel.send("Done") await chanel.send("Done")
await man.update() await man.update()
@tree.command( @tree.command(
name="search", name="search",
description="Search for manga to follow", description="Search for manga to follow",
@ -65,8 +69,9 @@ async def search_command(
interaction: discord.Interaction, interaction: discord.Interaction,
title: str title: str
): ):
await render_manga_list_in_dm(interaction, mh.search(title)) await interaction.response.defer()
await render_manga_list_in_dm(interaction, mh.search(title))
@tree.command( @tree.command(
@ -75,14 +80,16 @@ async def search_command(
guild=discord.Object(id=1042133536926347324) guild=discord.Object(id=1042133536926347324)
) )
async def list_command(interaction: discord.Interaction): async def list_command(interaction: discord.Interaction):
await interaction.response.defer()
await render_manga_list_in_dm(interaction, man.get_user_mangas(interaction.user)) await render_manga_list_in_dm(interaction, man.get_user_mangas(interaction.user))
@tasks.loop(minutes=5) @tasks.loop(minutes=5)
async def update_manga(): async def update_manga():
print("Updating... ")
await man.update() await man.update()
print("Update Finished!")
@client.event @client.event

View File

@ -14,6 +14,8 @@ class Manager:
self.chapters = {} self.chapters = {}
self.savefile = savefile self.savefile = savefile
self.load()
def get_user_mangas(self, user: discord.User) -> list[manga_api.Manga]: def get_user_mangas(self, user: discord.User) -> list[manga_api.Manga]:
manga_ids = [] manga_ids = []
@ -48,13 +50,22 @@ class Manager:
def check_for_new_chapter(self, manga: manga_api.Manga) -> manga_api.Chapter | None: def check_for_new_chapter(self, manga: manga_api.Manga) -> manga_api.Chapter | None:
latest_chap = manga.get_latest_chap() latest_chap = manga.get_latest_chap()
if latest_chap not in self.chapters.keys(): print("Comparing Chapters...")
if manga.id not in self.chapters.keys():
self.chapters[manga.id] = latest_chap.id self.chapters[manga.id] = latest_chap.id
return latest_chap return latest_chap
if latest_chap.is_more_recent_than(manga_api.Chapter(self.chapters[manga.id])): old_chap = manga_api.Chapter(self.chapters[manga.id])
print(f"Latest Chap ID: {latest_chap.id}, Old Chap ID: {self.chapters[manga.id]}")
print(f"Latest Chap: {latest_chap.get_volume()}:{latest_chap.get_number()}")
print(f"Latest Chap: {old_chap.get_volume()}:{old_chap.get_number()}")
if latest_chap.is_more_recent_than(old_chap):
self.chapters[manga.id] = latest_chap.id self.chapters[manga.id] = latest_chap.id
print("New Latest Chap!")
return latest_chap return latest_chap
else: else:
print("No New Chapter.")
return None return None
async def send_message_to_user(self, user: discord.User, manga: manga_api.Manga, async def send_message_to_user(self, user: discord.User, manga: manga_api.Manga,

View File

@ -26,6 +26,9 @@ class Chapter:
return float(self.get_volume()) > float(chapter.get_volume()) return float(self.get_volume()) > float(chapter.get_volume())
return float(self.get_number()) > float(chapter.get_number()) return float(self.get_number()) > float(chapter.get_number())
def get_title(self) -> str | None:
return self.data["attributes"]["title"] if self.data["attributes"]["title"] else None
def get_volume(self) -> str: def get_volume(self) -> str:
return self.data["attributes"]["volume"] return self.data["attributes"]["volume"]
@ -81,8 +84,6 @@ class Manga:
f"=desc", f"=desc",
) )
print(r.request.__dict__)
print(r.json())
if r.json()["total"] == 0: if r.json()["total"] == 0:
r = requests.get( r = requests.get(
f"{self.base_url}/chapter?manga={self.id}&translatedLanguage%5B%5D=en&contentRating%5B%5D=safe" f"{self.base_url}/chapter?manga={self.id}&translatedLanguage%5B%5D=en&contentRating%5B%5D=safe"
@ -157,4 +158,4 @@ class MangaHandler:
if __name__ == "__main__": if __name__ == "__main__":
mh = MangaHandler() mh = MangaHandler()
print(mh.search("Umineko no Naku Koro ni Episode 4: Alliance of the Golden Witch")[0]) print(mh.search("Umineko no Naku Koro ni Episode 4: Alliance of the Golden Witch")[0].get_latest_chap().data)