Skip to content

Commit

Permalink
[MAL] Add update command (#5)
Browse files Browse the repository at this point in the history
* initial addition

* remove manga whose status changed

* fix message edit after update completed
  • Loading branch information
Ableytner authored Nov 15, 2024
1 parent 446c10b commit ced4c50
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/nikobot/modules/mal/mal_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def fetch_manga_list(self) -> None:

manga_list = mal_helper.get_manga_list_from_username(self.username)
for entry in manga_list:
mal_id = entry["mal_id"]
mal_id = int(entry["mal_id"])
if mal_id in self.manga:
self.manga[mal_id].set_chapters_read(entry["read_chapters"])
else:
Expand All @@ -77,6 +77,13 @@ def fetch_manga_list(self) -> None:
self.manga[mal_id] = manga
except error.MediaTypeError:
pass

# remove manga that no longer have the 'Reading' status on MAL
correct_mal_ids = [int(entry["mal_id"]) for entry in manga_list]
for mal_id in list(self.manga.keys()):
if mal_id not in correct_mal_ids:
self.manga.pop(mal_id)

self.save_to_storage()

def save_to_storage(self) -> None:
Expand Down
32 changes: 32 additions & 0 deletions src/nikobot/modules/mal/malnotifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,38 @@ async def deregister(self, ctx: commands.context.Context | discord.interactions.
embed=discord.Embed(title="Successfully deregistered from release notifications",
color=discord.Color.blue()))

@util.discord.grouped_hybrid_command(
name="update",
description="Check for new manga chapters for the MyAnimeList account connected to your discord account",
command_group=command_group
)
async def update(self, ctx: commands.context.Context | discord.interactions.Interaction):
"""The discord command 'niko.mal.update'"""

if util.discord.is_slash_command(ctx):
user_id = ctx.user.id
else:
user_id = ctx.author.id

if not util.VolatileStorage.contains("mal.user", str(user_id)):
await util.discord.reply(ctx,
embed=discord.Embed(title="You are not yet registered",
color=discord.Color.orange()))
return

embed = discord.Embed(title="Checking for new chapters...",
color=discord.Color.blue())
embed.add_field(name="\u200b",
value="Please wait a few minutes",
inline=True)
message = await util.discord.reply(ctx, embed=embed)

maluser = util.VolatileStorage[f"mal.user.{user_id}"]
await self.notify_user(int(user_id), maluser)

await message.edit(embed=discord.Embed(title="Finished checking!",
color=discord.Color.blue()))

@tasks.loop(hours=1, reconnect=True, name="notify-users-task")
async def notify_users(self):
"""A method responsible for notifying users if a new manga chapter was released"""
Expand Down
3 changes: 3 additions & 0 deletions src/nikobot/modules/mal/manga.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ def from_export(export: dict[str, str]) -> Manga:
def from_mal_id(mal_id: int) -> Manga:
"""Factory method creating a new Manga object using the MyAnimeList id"""

if not isinstance(mal_id, int):
raise TypeError()

data = mal_helper.get_manga_from_id(mal_id)
manga = Manga(mal_id,
data["title"],
Expand Down

0 comments on commit ced4c50

Please sign in to comment.