MangaBot/main.py

102 lines
2.7 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-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-22 23:55:29 +01:00
@tree.command(
name="ping",
description="My first application Command",
guild=discord.Object(id=1042133536926347324)
2024-07-22 23:55:29 +01:00
)
async def first_command(interaction: discord.Interaction):
chanel = await interaction.user.create_dm()
await chanel.send("Hi")
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
await interaction.followup.send("Check your DM's")
chanel = await interaction.user.create_dm()
2024-07-24 03:24:11 +01:00
if len(manga_list) == 0:
await chanel.send("No Manga in Here")
return
view = embed_util.ListManga(manga_list)
2024-07-23 13:35:34 +01:00
msg = await chanel.send(view=view, embed=embed_util.manga_embed(manga_list[0]))
2024-07-23 14:19:45 +01:00
await view.force_reload(msg)
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))
await chanel.send("Done")
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",
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
):
2024-07-24 03:24:11 +01:00
await interaction.response.defer()
2024-07-24 02:38:17 +01:00
2024-07-24 03:24:11 +01:00
await render_manga_list_in_dm(interaction, mh.search(title))
2024-07-24 02:38:17 +01:00
@tree.command(
name="list",
description="List the manga you follow",
guild=discord.Object(id=1042133536926347324)
)
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 03:24:11 +01:00
await render_manga_list_in_dm(interaction, 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))
print("Ready!")
client.run(os.environ.get("BOT_TOKEN"))