Skip to content

Commit

Permalink
feat: lazy load for lyrics (#156)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Nat1anWasTaken authored Jul 25, 2024
1 parent 4f0de7a commit d86eb4a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
37 changes: 26 additions & 11 deletions lava/classes/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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:
Expand All @@ -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(
[
Expand Down
1 change: 0 additions & 1 deletion lava/cogs/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions locale/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions locale/zh_TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "重新開始",
Expand Down

0 comments on commit d86eb4a

Please sign in to comment.