Working Basics
This commit is contained in:
parent
494fe4c850
commit
b0230a6d88
48
main.py
48
main.py
|
|
@ -1,6 +1,7 @@
|
|||
import os
|
||||
import discord
|
||||
from discord import app_commands
|
||||
from discord.ext import tasks
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
|
|
@ -29,22 +30,11 @@ async def first_command(interaction: discord.Interaction):
|
|||
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 search_command(
|
||||
interaction: discord.Interaction,
|
||||
title: str
|
||||
):
|
||||
|
||||
|
||||
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")
|
||||
chanel = await interaction.user.create_dm()
|
||||
|
||||
manga_list = mh.search(title)
|
||||
|
||||
chanel = await interaction.user.create_dm()
|
||||
view = embed_util.ListManga(manga_list)
|
||||
msg = await chanel.send(view=view, embed=embed_util.manga_embed(manga_list[0]))
|
||||
await view.force_reload(msg)
|
||||
|
|
@ -62,10 +52,38 @@ async def search_command(
|
|||
print(f"Userid {interaction.user.id} removed mangaid {manga_id}")
|
||||
man.remove_user_from_manga(interaction.user, manga_api.Manga(manga_id))
|
||||
|
||||
|
||||
await chanel.send("Done")
|
||||
await man.update()
|
||||
|
||||
@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 search_command(
|
||||
interaction: discord.Interaction,
|
||||
title: str
|
||||
):
|
||||
await render_manga_list_in_dm(interaction, mh.search(title))
|
||||
|
||||
|
||||
|
||||
@tree.command(
|
||||
name="list",
|
||||
description="List the manga you follow",
|
||||
guild=discord.Object(id=1042133536926347324)
|
||||
)
|
||||
async def list_command(interaction: discord.Interaction):
|
||||
await render_manga_list_in_dm(interaction, man.get_user_mangas(interaction.user))
|
||||
|
||||
|
||||
|
||||
|
||||
@tasks.loop(minutes=5)
|
||||
async def update_manga():
|
||||
await man.update()
|
||||
|
||||
|
||||
@client.event
|
||||
async def on_ready():
|
||||
|
|
|
|||
27
manager.py
27
manager.py
|
|
@ -4,14 +4,24 @@ import json
|
|||
import embed_util
|
||||
import manga_api
|
||||
|
||||
_manager = None
|
||||
import os.path
|
||||
|
||||
|
||||
class Manager:
|
||||
def __init__(self, client):
|
||||
def __init__(self, client, savefile: str = "manager.json"):
|
||||
self.client: discord.Client = client
|
||||
self.manga = {}
|
||||
self.chapters = {}
|
||||
self.savefile = savefile
|
||||
|
||||
def get_user_mangas(self, user: discord.User) -> list[manga_api.Manga]:
|
||||
manga_ids = []
|
||||
|
||||
for manga_id in self.manga.keys():
|
||||
if user.id in self.manga[manga_id]:
|
||||
manga_ids.append(manga_id)
|
||||
|
||||
return [manga_api.Manga(manga_id) for manga_id in manga_ids]
|
||||
|
||||
def add_user_to_manga(self, user: discord.User, manga: manga_api.Manga) -> None:
|
||||
if manga.id in self.manga.keys():
|
||||
|
|
@ -53,8 +63,17 @@ class Manager:
|
|||
await dm_channel.send(embed=embed_util.chapter_embed(manga, chapter), files=embed_util.get_chapter_files(manga))
|
||||
|
||||
def save(self):
|
||||
with open("manager.json", "w") as f:
|
||||
json.dump(self.to_dict(), f)
|
||||
with open(self.savefile, "w") as f:
|
||||
json.dump(self.to_dict(), f, indent=4)
|
||||
|
||||
def load(self):
|
||||
if os.path.isfile(self.savefile):
|
||||
with open(self.savefile, "r") as f:
|
||||
self.from_dict(json.load(f))
|
||||
|
||||
def from_dict(self, data: dict):
|
||||
self.manga = data["manga"]
|
||||
self.chapters = data["chapters"]
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ class Chapter:
|
|||
|
||||
def __str__(self):
|
||||
return str({
|
||||
"id": self.id,
|
||||
"chapter_vol": self.get_volume(),
|
||||
"chapter_num": self.get_number(),
|
||||
"chapter_url": self.get_url()
|
||||
|
|
@ -128,7 +129,10 @@ class Manga:
|
|||
|
||||
def __str__(self):
|
||||
return str({
|
||||
"id": self.id,
|
||||
"title": self.get_title(),
|
||||
"url": self.get_url(),
|
||||
"art_url": self.get_cover_art_url(),
|
||||
"latest_chapter": str(self.get_latest_chap())
|
||||
})
|
||||
|
||||
|
|
@ -153,4 +157,4 @@ class MangaHandler:
|
|||
|
||||
if __name__ == "__main__":
|
||||
mh = MangaHandler()
|
||||
print(mh.search("Umineko no Naku Koro ni Episode 4: Alliance of the Golden Witch")[0].get_latest_chap())
|
||||
print(mh.search("Umineko no Naku Koro ni Episode 4: Alliance of the Golden Witch")[0])
|
||||
|
|
|
|||
Loading…
Reference in New Issue