impr: Async database and code optimization.

This commit is contained in:
Lemon4ksan
2025-02-03 22:31:06 +03:00
parent 09b28de205
commit 1ab823569e
11 changed files with 600 additions and 534 deletions

View File

@@ -16,18 +16,18 @@ class PlayButton(Button, VoiceExtension):
self.item = item
async def callback(self, interaction: Interaction) -> None:
logging.debug(f"Callback triggered for type: '{type(self.item).__name__}'")
logging.debug(f"[FIND] Callback triggered for type: '{type(self.item).__name__}'")
if not interaction.guild:
logging.warning("No guild found in PlayButton callback")
logging.warning("[FIND] No guild found in PlayButton callback")
return
if not await self.voice_check(interaction):
logging.debug("Voice check failed in PlayButton callback")
logging.debug("[FIND] Voice check failed in PlayButton callback")
return
gid = interaction.guild.id
guild = self.db.get_guild(gid)
guild = await self.db.get_guild(gid, projection={'current_track': 1, 'current_menu': 1, 'vote_add_track': 1, 'vote_add_album': 1, 'vote_add_artist': 1, 'vote_add_playlist': 1})
channel = cast(discord.VoiceChannel, interaction.channel)
member = cast(discord.Member, interaction.user)
action: Literal['add_track', 'add_album', 'add_artist', 'add_playlist']
@@ -41,7 +41,7 @@ class PlayButton(Button, VoiceExtension):
elif isinstance(self.item, Album):
album = await self.item.with_tracks_async()
if not album or not album.volumes:
logging.debug("Failed to fetch album tracks in PlayButton callback")
logging.debug("[FIND] Failed to fetch album tracks in PlayButton callback")
await interaction.respond("Не удалось получить треки альбома.", ephemeral=True)
return
@@ -53,7 +53,7 @@ class PlayButton(Button, VoiceExtension):
elif isinstance(self.item, Artist):
artist_tracks = await self.item.get_tracks_async()
if not artist_tracks:
logging.debug("Failed to fetch artist tracks in PlayButton callback")
logging.debug("[FIND] Failed to fetch artist tracks in PlayButton callback")
await interaction.respond("Не удалось получить треки артиста.", ephemeral=True)
return
@@ -65,7 +65,7 @@ class PlayButton(Button, VoiceExtension):
elif isinstance(self.item, Playlist):
short_tracks = await self.item.fetch_tracks_async()
if not short_tracks:
logging.debug("Failed to fetch playlist tracks in PlayButton callback")
logging.debug("[FIND] Failed to fetch playlist tracks in PlayButton callback")
await interaction.respond("Не удалось получить треки из плейлиста.", delete_after=15)
return
@@ -77,12 +77,12 @@ class PlayButton(Button, VoiceExtension):
elif isinstance(self.item, list):
tracks = self.item.copy()
if not tracks:
logging.debug("Empty tracks list in PlayButton callback")
logging.debug("[FIND] Empty tracks list in PlayButton callback")
await interaction.respond("Не удалось получить треки.", delete_after=15)
return
action = 'add_playlist'
vote_message = f"{member.mention} хочет добавить плейлист **** в очередь.\n\n Голосуйте за добавление."
vote_message = f"{member.mention} хочет добавить плейлист **Мне Нравится** в очередь.\n\n Голосуйте за добавление."
response_message = f"Плейлист **«Мне нравится»** был добавлен в очередь."
else:
@@ -97,7 +97,7 @@ class PlayButton(Button, VoiceExtension):
await response.add_reaction('')
await response.add_reaction('')
self.db.update_vote(
await self.db.update_vote(
gid,
response.id,
{
@@ -109,22 +109,22 @@ class PlayButton(Button, VoiceExtension):
}
)
else:
logging.debug(f"Skipping vote for '{action}' (from PlayButton callback)")
logging.debug(f"[FIND] Skipping vote for '{action}' (from PlayButton callback)")
current_menu = await self.get_menu_message(interaction, guild['current_menu']) if guild['current_menu'] else None
if guild['current_track'] is not None:
self.db.modify_track(gid, tracks, 'next', 'extend')
logging.debug(f"[FIND] Adding tracks to queue (from PlayButton callback)")
await self.db.modify_track(gid, tracks, 'next', 'extend')
else:
logging.debug(f"[FIND] Playing track (from PlayButton callback)")
track = tracks.pop(0)
self.db.modify_track(gid, tracks, 'next', 'extend')
await self.db.modify_track(gid, tracks, 'next', 'extend')
await self.play_track(interaction, track)
response_message = f"Сейчас играет: **{track.title}**!"
current_menu = None
if guild['current_menu']:
current_menu = await self.get_menu_message(interaction, guild['current_menu'])
if current_menu and interaction.message:
logging.debug(f"Deleting interaction message {interaction.message.id}: current player {current_menu.id} found")
logging.debug(f"[FIND] Deleting interaction message {interaction.message.id}: current player {current_menu.id} found")
await interaction.message.delete()
else:
await interaction.respond(response_message, delete_after=15)
@@ -145,7 +145,7 @@ class MyVibeButton(Button, VoiceExtension):
logging.warning(f"[VIBE] Guild ID is None in button callback")
return
guild = self.db.get_guild(gid)
guild = await self.db.get_guild(gid)
channel = cast(discord.VoiceChannel, interaction.channel)
if len(channel.members) > 2 and not guild['always_allow_menu']:
@@ -167,7 +167,7 @@ class MyVibeButton(Button, VoiceExtension):
class ListenView(View):
def __init__(self, item: Track | Album | Artist | Playlist | list[Track], *items: Item, timeout: float | None = 360, disable_on_timeout: bool = True):
super().__init__(*items, timeout=timeout, disable_on_timeout=disable_on_timeout)
logging.debug(f"Creating view for type: '{type(item).__name__}'")
logging.debug(f"[FIND] Creating view for type: '{type(item).__name__}'")
if isinstance(item, Track):
link_app = f"yandexmusic://album/{item.albums[0].id}/track/{item.id}"
@@ -195,3 +195,9 @@ class ListenView(View):
self.add_item(self.button2)
self.add_item(self.button3)
self.add_item(self.button4)
async def on_timeout(self) -> None:
try:
return await super().on_timeout()
except discord.NotFound:
pass