Skip to content

Commit

Permalink
Add Platform Identifier updater
Browse files Browse the repository at this point in the history
  • Loading branch information
nattadasu committed Jan 28, 2025
1 parent 855571a commit f7192b9
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 4 deletions.
1 change: 1 addition & 0 deletions extensions/tasker.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ async def delete_cache(self) -> None:
"mangadex": a_day,
"pronoundb": a_week,
"rawg": a_day,
"refresh": a_day,
"shikimori": {"base": a_day, "user": half_day},
"simkl": a_day,
"spotify": a_week * 2,
Expand Down
131 changes: 127 additions & 4 deletions extensions/usersettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

import interactions as ipy

from classes import cache
from classes.anilist import AniList
from classes.database import UserDatabase
from classes.jikan import JikanApi
from classes.shikimori import Shikimori

Cache = cache.Caching(
cache_directory="cache/refresh", cache_expiration_time=60 * 60 * 24
)


class UserSettings(ipy.Extension):
"""User Settings commands"""
Expand All @@ -17,6 +27,110 @@ class UserSettings(ipy.Extension):
),
)

@usersettings_head.subcommand(
sub_cmd_name="refresh_accounts",
sub_cmd_description="Refresh your account informations",
)
async def usersettings_refresh_accounts(self, ctx: ipy.SlashContext):
"""Refresh account informations"""
await ctx.defer(ephemeral=True)
cfp = Cache.get_cache_path(f"{ctx.author.id}.json")
cached: cache.CacheModel | None = Cache.read_cache(cfp, as_raw=True)
if cached:
timestamp = cached.timestamp + Cache.cache_expiration_time
await ctx.send(
(
"You've invoked refresh command recently. "
f"You can reinvoke the command <t:{int(timestamp)}:R>"
),
ephemeral=True,
)
return

udb = UserDatabase()
if not await udb.check_if_registered(ctx.author.id):
await ctx.send("You haven't register yet.", ephemeral=True)
return

usr_ = await udb.get_user_data(ctx.author.id)

success: list[str] = []
fails: list[str] = []
unsupported: list[str] = []
not_modified: list[str] = []
try:
async with JikanApi() as jikan:
jusr_ = await jikan.get_user_by_id(usr_.mal_id)
if jusr_.username == usr_.mal_username:
not_modified.append("MyAnimeList")
else:
await udb.update_user(
ctx.author.id, row="malUsername", modified_input=jusr_.username
)
success.append("MyAnimeList")
except Exception as _:
await ctx.send(
(
"Failed to refresh MyAnimeList account by looking up through user ID. "
"Please unregister and register again."
),
ephemeral=True,
)
return

if usr_.anilist_id:
try:
async with AniList() as al_:
ausr_ = await al_.user_by_id(usr_.anilist_id, True)
if ausr_.name == usr_.anilist_username:
not_modified.append("AniList")
else:
await udb.update_user(
ctx.author.id, row="anilistUsername", modified_input=ausr_.name
)
success.append("AniList")
except Exception as err:
fails.append(f"AniList (`{err}`)")

if usr_.lastfm_username:
unsupported.append("Last.FM")

if usr_.shikimori_id:
try:
async with Shikimori() as shiki:
susr_ = await shiki.get_user(usr_.shikimori_id, is_nickname=False)
if susr_.nickname == usr_.shikimori_username:
not_modified.append("Shikimori")
else:
await udb.update_user(
ctx.author.id,
row="shikimoriUsername",
modified_input=susr_.nickname,
)
success.append("Shikimori")
except Exception as err:
fails.append(f"Shikimori (`{err}`)")

final = "We've refreshed your account informations.\n"
remarks: list[str] = []
if success:
final += f"* Successfully refreshed: {', '.join(success)}\n"
if fails:
final += f"* Failed to refresh: {', '.join(fails)}\n"
remarks.append("failed")
if unsupported:
final += f"* Unsupported\\*: {', '.join(unsupported)}\n"
remarks.append("unsupported")
if not_modified:
final += f"* Not modified: {', '.join(not_modified)}\n"
plurals = [*fails, *unsupported]
f_p = "s" if len(plurals) > 1 else ""
final += f"For {' or '.join(remarks)} account{f_p}, please relink them by `/platform unlink` then `/platform link`"
if unsupported:
final += "\n-# \\* Database only stores modifiable identifier, not permanent one like ID number."
Cache.write_cache(cfp, final)
await ctx.send(final, ephemeral=True)

@usersettings_head.subcommand(
sub_cmd_name="autoembed",
sub_cmd_description="Configure autoembed function",
Expand Down Expand Up @@ -45,16 +159,23 @@ async def usersettings_autoembed(self, ctx: ipy.SlashContext, state: str):
path_exist = os.path.exists(file_path)
state_ = "true" == state
if path_exist and state_ is True:
await ctx.send("It seems you've enabled the feature already. If you wanted to disable, set `state` to `disable`", ephemeral=True)
await ctx.send(
"It seems you've enabled the feature already. If you wanted to disable, set `state` to `disable`",
ephemeral=True,
)
return
if not path_exist and state_ is False:
await ctx.send("It seems you've disabled the feature already. If you wanted to enable, set `state` to `enable`", ephemeral=True)
await ctx.send(
"It seems you've disabled the feature already. If you wanted to enable, set `state` to `enable`",
ephemeral=True,
)
return

if state_:
with open(file_path, "w", encoding="utf-8") as file:
file.write("")
await ctx.send("""Feature enabled, now Ryuusei will automatically respond to your message with supported sites.
await ctx.send(
"""Feature enabled, now Ryuusei will automatically respond to your message with supported sites.
We currently support following sites
## Anime
Expand All @@ -70,7 +191,9 @@ async def usersettings_autoembed(self, ctx: ipy.SlashContext, state: str):
Upcoming: IMDb, TMDB, Trakt
## Games
*Uses RAWG*
RAWG""", ephemeral=True)
RAWG""",
ephemeral=True,
)
return

os.remove(file_path)
Expand Down

0 comments on commit f7192b9

Please sign in to comment.