fix file not open error with discord files, UX changes, allow bot for user install
This commit is contained in:
parent
4338aebac2
commit
9408d4b8d6
|
|
@ -15,7 +15,7 @@ class ListManga(discord.ui.View):
|
||||||
self.ret = []
|
self.ret = []
|
||||||
self.index = 0
|
self.index = 0
|
||||||
self.manga_list = manga_list
|
self.manga_list = manga_list
|
||||||
self.manga_files = gen_manga_files(self.manga_list)
|
self.thumbnail_files: list[str] = gen_manga_files(self.manga_list)
|
||||||
self.comp = self.children
|
self.comp = self.children
|
||||||
self.prev: discord.Button = None
|
self.prev: discord.Button = None
|
||||||
self.next: discord.Button = None
|
self.next: discord.Button = None
|
||||||
|
|
@ -33,8 +33,8 @@ class ListManga(discord.ui.View):
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
|
|
||||||
async def update_buttons(self):
|
async def update_buttons(self):
|
||||||
self.prev.disabled = self.index != 0
|
self.prev.disabled = self.index == 0
|
||||||
self.next.disabled = self.index != len(self.manga_list) - 1
|
self.next.disabled = self.index == len(self.manga_list) - 1
|
||||||
|
|
||||||
print(f"index: {self.index}, prev: {self.prev.disabled}, next {self.next.disabled}")
|
print(f"index: {self.index}, prev: {self.prev.disabled}, next {self.next.disabled}")
|
||||||
|
|
||||||
|
|
@ -44,7 +44,9 @@ class ListManga(discord.ui.View):
|
||||||
|
|
||||||
self.msg = await self.msg.edit(
|
self.msg = await self.msg.edit(
|
||||||
embed=manga_embed(self.manga_list[self.index]),
|
embed=manga_embed(self.manga_list[self.index]),
|
||||||
attachments=[self.manga_files[self.index]],
|
attachments=[
|
||||||
|
parallel_downloads.discord_file_from_filename(self.thumbnail_files[self.index])
|
||||||
|
],
|
||||||
view=self
|
view=self
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -96,6 +98,14 @@ def gen_manga_files(manga_list: list[Manga]):
|
||||||
return parallel_downloads.parallel_download(manga_list)
|
return parallel_downloads.parallel_download(manga_list)
|
||||||
|
|
||||||
|
|
||||||
|
def manga_list_embed(manga_list: list[Manga], index: int):
|
||||||
|
manga = manga_list[index]
|
||||||
|
e = discord.Embed(title=f"({index+1}\\{len(manga_list)}) {manga.get_title()}", description=manga.get_description(), url=manga.get_url())
|
||||||
|
extension = manga.get_cover_art_extension()
|
||||||
|
e.set_thumbnail(url=f"attachment://{manga.id}.{extension}")
|
||||||
|
|
||||||
|
return e
|
||||||
|
|
||||||
def manga_embed(manga: Manga):
|
def manga_embed(manga: Manga):
|
||||||
e = discord.Embed(title=manga.get_title(), description=manga.get_description(), url=manga.get_url())
|
e = discord.Embed(title=manga.get_title(), description=manga.get_description(), url=manga.get_url())
|
||||||
extension = manga.get_cover_art_extension()
|
extension = manga.get_cover_art_extension()
|
||||||
|
|
@ -104,10 +114,11 @@ def manga_embed(manga: Manga):
|
||||||
return e
|
return e
|
||||||
|
|
||||||
|
|
||||||
def get_chapter_files(manga: Manga):
|
def get_chapter_filenames(manga: Manga):
|
||||||
return parallel_downloads.parallel_download([manga])
|
return parallel_downloads.parallel_download([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 ""
|
volume_info = f"Volume {chapter.get_volume()}" if chapter.get_volume() else ""
|
||||||
chapter_title = f"{chapter.get_title()}" if chapter.get_title() else ""
|
chapter_title = f"{chapter.get_title()}" if chapter.get_title() else ""
|
||||||
|
|
|
||||||
3
main.py
3
main.py
|
|
@ -65,7 +65,6 @@ async def render_manga_list_in_dm(interaction: discord.Interaction, manga_list:
|
||||||
@tree.command(
|
@tree.command(
|
||||||
name="search",
|
name="search",
|
||||||
description="Search for manga to follow",
|
description="Search for manga to follow",
|
||||||
guild=discord.Object(id=1042133536926347324)
|
|
||||||
)
|
)
|
||||||
@app_commands.describe(title='Title of the manga to search for')
|
@app_commands.describe(title='Title of the manga to search for')
|
||||||
async def search_command(
|
async def search_command(
|
||||||
|
|
@ -80,7 +79,6 @@ async def search_command(
|
||||||
@tree.command(
|
@tree.command(
|
||||||
name="list",
|
name="list",
|
||||||
description="List the manga you follow",
|
description="List the manga you follow",
|
||||||
guild=discord.Object(id=1042133536926347324)
|
|
||||||
)
|
)
|
||||||
async def list_command(interaction: discord.Interaction):
|
async def list_command(interaction: discord.Interaction):
|
||||||
await interaction.response.defer()
|
await interaction.response.defer()
|
||||||
|
|
@ -98,6 +96,7 @@ async def update_manga():
|
||||||
@client.event
|
@client.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
await tree.sync(guild=discord.Object(id=1042133536926347324))
|
await tree.sync(guild=discord.Object(id=1042133536926347324))
|
||||||
|
await tree.sync()
|
||||||
print("Ready!")
|
print("Ready!")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
10
manager.py
10
manager.py
|
|
@ -5,6 +5,7 @@ import embed_util
|
||||||
import manga_api
|
import manga_api
|
||||||
|
|
||||||
import os.path
|
import os.path
|
||||||
|
from util import parallel_downloads
|
||||||
|
|
||||||
|
|
||||||
class Manager:
|
class Manager:
|
||||||
|
|
@ -33,7 +34,8 @@ class Manager:
|
||||||
|
|
||||||
def remove_user_from_manga(self, user: discord.User, manga: manga_api.Manga) -> None:
|
def remove_user_from_manga(self, user: discord.User, manga: manga_api.Manga) -> None:
|
||||||
if manga.id in self.manga.keys():
|
if manga.id in self.manga.keys():
|
||||||
self.manga[manga.id].remove(user.id)
|
if user.id in self.manga[manga.id]:
|
||||||
|
self.manga[manga.id].remove(user.id)
|
||||||
else:
|
else:
|
||||||
self.manga[manga.id] = []
|
self.manga[manga.id] = []
|
||||||
|
|
||||||
|
|
@ -71,7 +73,11 @@ class Manager:
|
||||||
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,
|
||||||
chapter: manga_api.Chapter) -> None:
|
chapter: manga_api.Chapter) -> None:
|
||||||
dm_channel = await user.create_dm()
|
dm_channel = await user.create_dm()
|
||||||
await dm_channel.send(embed=embed_util.chapter_embed(manga, chapter), files=embed_util.get_chapter_files(manga))
|
await dm_channel.send(
|
||||||
|
embed=embed_util.chapter_embed(manga, chapter),
|
||||||
|
files=[parallel_downloads.discord_file_from_filename(filename) for filename in
|
||||||
|
embed_util.get_chapter_filenames(manga)]
|
||||||
|
)
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
with open(self.savefile, "w") as f:
|
with open(self.savefile, "w") as f:
|
||||||
|
|
|
||||||
|
|
@ -6,16 +6,20 @@ import os.path
|
||||||
import discord
|
import discord
|
||||||
from manga_api import Manga
|
from manga_api import Manga
|
||||||
|
|
||||||
|
DOWNLOAD_DIRECTORY = 'tmp/'
|
||||||
|
|
||||||
def download_file(manga: Manga):
|
|
||||||
|
def discord_file_from_filename(filename: str) -> discord.File:
|
||||||
|
return discord.File(f"{DOWNLOAD_DIRECTORY}{filename}",filename)
|
||||||
|
def download_file(manga: Manga) -> str:
|
||||||
extension = manga.get_cover_art_extension()
|
extension = manga.get_cover_art_extension()
|
||||||
if not os.path.isfile(f'tmp/{manga.id}.{extension}'):
|
if not os.path.isfile(f'{DOWNLOAD_DIRECTORY}/{manga.id}.{extension}'):
|
||||||
img_data = requests.get(manga.get_cover_art_url()).content
|
img_data = requests.get(manga.get_cover_art_url()).content
|
||||||
with open(f'tmp/{manga.id}.{extension}', 'wb') as handler:
|
with open(f'{DOWNLOAD_DIRECTORY}/{manga.id}.{extension}', 'wb') as handler:
|
||||||
handler.write(img_data)
|
handler.write(img_data)
|
||||||
return discord.File(f"tmp/{manga.id}.{extension}", f"{manga.id}.{extension}")
|
return f"{manga.id}.{extension}"
|
||||||
|
|
||||||
def parallel_download(manga_list: list[Manga]) -> list[discord.File]:
|
def parallel_download(manga_list: list[Manga]) -> list[str]:
|
||||||
print("Downloading Images...")
|
print("Downloading Images...")
|
||||||
with concurrent.futures.ThreadPoolExecutor() as exector:
|
with concurrent.futures.ThreadPoolExecutor() as exector:
|
||||||
result = exector.map(download_file, manga_list)
|
result = exector.map(download_file, manga_list)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue