MangaBot/main.py

101 lines
2.8 KiB
Python
Raw Normal View History

2024-07-22 23:55:29 +01:00
import os
import discord
from discord import app_commands
2024-07-24 02:38:17 +01:00
from discord.ext import tasks
2024-07-22 23:55:29 +01:00
from dotenv import load_dotenv
import embed_util
2024-07-23 17:31:23 +01:00
import manager
import manga_api
2024-07-24 23:07:10 +01:00
from util import parallel_downloads
2024-07-23 17:31:23 +01:00
mh = manga_api.MangaHandler()
2024-07-22 23:55:29 +01:00
load_dotenv()
intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
2024-07-23 17:42:36 +01:00
man = manager.Manager(client)
2024-07-23 02:42:08 +01:00
2024-07-24 02:38:17 +01:00
async def render_manga_list_in_dm(interaction: discord.Interaction, manga_list: list[manga_api.Manga]):
2024-07-24 03:24:11 +01:00
chanel = await interaction.user.create_dm()
2024-07-24 23:07:10 +01:00
await interaction.followup.send("Check your DM's")
2024-07-24 03:24:11 +01:00
if len(manga_list) == 0:
await chanel.send("No Manga in Here")
return
2024-07-24 23:07:10 +01:00
list_msg = await chanel.send(embed=embed_util.manga_list_embed(manga_list))
2024-07-24 22:19:38 +01:00
view = embed_util.ListManga(manga_list, await man.get_user_mangas(interaction.user))
2024-07-24 23:07:10 +01:00
msg = await chanel.send(view=view, embed=await embed_util.manga_embed(manga_list, 0),
files=[parallel_downloads.discord_file_from_filename(filename) for filename in
embed_util.get_chapter_filenames(manga_list[0])]
)
2024-07-24 14:16:51 +01:00
2024-07-23 17:31:23 +01:00
await view.wait()
print("Done.. Checking Returns")
2024-07-23 17:31:23 +01:00
ret: list[dict] = view.ret
for action in ret:
if action["action"] == 1:
manga_id = action["manga"]
print(f"Userid {interaction.user.id} added mangaid {manga_id}")
2024-07-23 17:31:23 +01:00
man.add_user_to_manga(interaction.user, manga_api.Manga(manga_id))
if action["action"] == -1:
manga_id = action["manga"]
print(f"Userid {interaction.user.id} removed mangaid {manga_id}")
2024-07-23 17:31:23 +01:00
man.remove_user_from_manga(interaction.user, manga_api.Manga(manga_id))
2024-07-24 13:46:12 +01:00
await msg.delete()
2024-07-24 22:19:38 +01:00
await list_msg.delete()
2024-07-23 17:31:23 +01:00
await man.update()
2024-07-24 03:24:11 +01:00
2024-07-24 02:38:17 +01:00
@tree.command(
name="search",
description="Search for manga to follow",
)
@app_commands.describe(title='Title of the manga to search for')
2024-07-25 00:35:18 +01:00
@app_commands.user_install()
@discord.app_commands.guild_install()
2024-07-24 02:38:17 +01:00
async def search_command(
interaction: discord.Interaction,
title: str
):
2024-07-24 03:24:11 +01:00
await interaction.response.defer()
2024-07-24 02:38:17 +01:00
2024-07-24 22:19:38 +01:00
await render_manga_list_in_dm(interaction, await mh.search(title))
2024-07-24 02:38:17 +01:00
@tree.command(
name="list",
description="List the manga you follow",
)
2024-07-25 00:35:18 +01:00
@app_commands.user_install()
@discord.app_commands.guild_install()
2024-07-24 02:38:17 +01:00
async def list_command(interaction: discord.Interaction):
2024-07-24 03:24:11 +01:00
await interaction.response.defer()
2024-07-24 02:38:17 +01:00
2024-07-24 22:19:38 +01:00
await render_manga_list_in_dm(interaction, await man.get_user_mangas(interaction.user))
2024-07-24 02:38:17 +01:00
@tasks.loop(minutes=5)
async def update_manga():
2024-07-24 03:24:11 +01:00
print("Updating... ")
2024-07-24 02:38:17 +01:00
await man.update()
2024-07-24 03:24:11 +01:00
print("Update Finished!")
2024-07-24 02:38:17 +01:00
2024-07-22 23:55:29 +01:00
@client.event
async def on_ready():
await tree.sync(guild=discord.Object(id=1042133536926347324))
await tree.sync()
2024-07-22 23:55:29 +01:00
print("Ready!")
client.run(os.environ.get("BOT_TOKEN"))