From d86eb4a2f95198315e0fa2ff4677cefb378a184f Mon Sep 17 00:00:00 2001 From: Nat1an Date: Thu, 25 Jul 2024 17:04:30 +0800 Subject: [PATCH] feat: lazy load for lyrics (#156) * feat(lava): add async method fetch_and_update_lyrics for fetching and updating lyrics, improve handling of lyrics loading in generate_lyrics_embed, and update language files with new loading messages * fix(lava/classes/player.py): fix the line length of doc string * feat(lava/classes/player.py): add docstring to the fetch_and_update_lyrics method --- lava/classes/player.py | 37 ++++++++++++++++++++++++++----------- lava/cogs/events.py | 1 - locale/en_US.json | 1 + locale/zh_TW.json | 1 + 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/lava/classes/player.py b/lava/classes/player.py index 2d77906c..9a9502ea 100644 --- a/lava/classes/player.py +++ b/lava/classes/player.py @@ -40,7 +40,16 @@ def __init__(self, bot: "Bot", guild_id: int, node: Node): self._lyrics: Union[Lyrics[LyricLine], None] = None @property - def lyrics(self) -> Union[Lyrics[LyricLine], None]: + def guild(self) -> Optional[Guild]: + if not self._guild: + self._guild = self.bot.get_guild(self.guild_id) + + return self._guild + + async def fetch_and_update_lyrics(self) -> Union[Lyrics[LyricLine], None]: + """ + Fetch and update the lyrics to the cache for the current playing track. + """ if self._lyrics == MISSING: return MISSING @@ -60,13 +69,6 @@ def lyrics(self) -> Union[Lyrics[LyricLine], None]: return self._lyrics - @property - def guild(self) -> Optional[Guild]: - if not self._guild: - self._guild = self.bot.get_guild(self.guild_id) - - return self._guild - async def check_autoplay(self) -> bool: """ Check the autoplay status and add recommended tracks if enabled. @@ -260,6 +262,9 @@ async def update_display(self, embeds = [await self.__generate_display_embed()] if self.is_playing and self.show_lyrics: + if self._lyrics is None: + _ = self.bot.loop.create_task(self.fetch_and_update_lyrics()) + embeds.append(await self.__generate_lyrics_embed()) if interaction: @@ -281,15 +286,25 @@ async def update_display(self, ) async def __generate_lyrics_embed(self) -> Embed: - """Generate the lyrics embed for the player.""" - if self.lyrics is MISSING: + """ + Generate the lyrics embed for the player based on the cached lyrics. + Use fetch_and_update_lyrics to update. + """ + if self._lyrics is None: + return Embed( + title=self.bot.get_text('display.lyrics.title', self.locale, '🎤 | 歌詞'), + description=self.bot.get_text('displa .lyrics.loading', self.locale, '正在載入歌詞...'), + color=Colour.blurple() + ) + + if self.fetch_and_update_lyrics is MISSING: return Embed( title=self.bot.get_text('display.lyrics.title', self.locale, '🎤 | 歌詞'), description=self.bot.get_text('display.lyrics.not_found', self.locale, '*你得自己唱出這首歌的歌詞*'), color=Colour.red() ) - lyrics_in_range = find_lyrics_within_range(self.lyrics, (self.position / 1000), 5.0) + lyrics_in_range = find_lyrics_within_range(self._lyrics, (self.position / 1000), 5.0) lyrics_text = '\n'.join( [ diff --git a/lava/cogs/events.py b/lava/cogs/events.py index 2fd863d4..1dba548e 100644 --- a/lava/cogs/events.py +++ b/lava/cogs/events.py @@ -48,7 +48,6 @@ async def on_track_start(self, event: TrackStartEvent): self.bot.logger.info("Received track start event for guild %s", player.guild) player.reset_lyrics() - _ = player.lyrics # Fetch the lyrics try: await player.update_display() diff --git a/locale/en_US.json b/locale/en_US.json index f1c35367..a5b05b0d 100644 --- a/locale/en_US.json +++ b/locale/en_US.json @@ -127,6 +127,7 @@ "display.footer": "If you felt the quality of the music is kinda awful, maybe checking filters or switching region in voice channel setting will help", "display.lyrics.title": "🎤 | Lyrics", "display.lyrics.not_found": "*You need to sing the lyrics of this song yourself*", + "display.lyrics.loading": "Loading lyrics...", "display.control.pause": "Pause", "display.control.resume": "Resume", "display.control.previous": "Restart", diff --git a/locale/zh_TW.json b/locale/zh_TW.json index 6748387b..6bc65b7c 100644 --- a/locale/zh_TW.json +++ b/locale/zh_TW.json @@ -127,6 +127,7 @@ "display.footer": "如果你覺得音樂怪怪的,可以試著檢查看看效果器設定或是切換語音頻道地區", "display.lyrics.title": "🎤 | 歌詞", "display.lyrics.not_found": "*你得自己唱出這首歌的歌詞*", + "display.lyrics.loading": "正在載入歌詞...", "display.control.pause": "暫停", "display.control.resume": "繼續", "display.control.previous": "重新開始",