mirror of
https://github.com/deadcxap/YandexMusicDiscordBot.git
synced 2026-01-10 09:41:46 +03:00
impr: Add BaseBot class for code reduction.
This commit is contained in:
@@ -8,20 +8,16 @@ import yandex_music.exceptions
|
||||
from yandex_music import Track, TrackShort, ClientAsync as YMClient
|
||||
|
||||
import discord
|
||||
from discord.ui import View
|
||||
from discord import Interaction, ApplicationContext, RawReactionActionEvent, VoiceChannel
|
||||
|
||||
from MusicBot.cogs.utils.base_bot import BaseBot
|
||||
from MusicBot.cogs.utils import generate_item_embed
|
||||
from MusicBot.database import VoiceGuildsDatabase, BaseUsersDatabase, ExplicitGuild, MessageVotes
|
||||
from MusicBot.database import ExplicitGuild, MessageVotes
|
||||
|
||||
menu_views: dict[int, View] = {} # Store menu views and delete them when needed to prevent memory leaks for after callbacks.
|
||||
|
||||
class VoiceExtension:
|
||||
class VoiceExtension(BaseBot):
|
||||
|
||||
def __init__(self, bot: discord.Bot | None) -> None:
|
||||
self.bot = bot
|
||||
self.db = VoiceGuildsDatabase()
|
||||
self.users_db = BaseUsersDatabase()
|
||||
super().__init__(bot)
|
||||
|
||||
async def send_menu_message(self, ctx: ApplicationContext | Interaction | RawReactionActionEvent, *, disable: bool = False) -> bool:
|
||||
"""Send menu message to the channel and delete old one if exists. Return True if sent.
|
||||
@@ -30,16 +26,16 @@ class VoiceExtension:
|
||||
ctx (ApplicationContext | Interaction | RawReactionActionEvent): Context.
|
||||
disable (bool, optional): Disable menu message buttons. Defaults to False.
|
||||
|
||||
Raises:
|
||||
ValueError: If bot instance is not set and ctx is RawReactionActionEvent.
|
||||
|
||||
Returns:
|
||||
bool: True if sent, False if not.
|
||||
|
||||
Raises:
|
||||
ValueError: If bot instance is not set and ctx is RawReactionActionEvent.
|
||||
"""
|
||||
logging.info(f"[VC_EXT] Sending menu message to channel {ctx.channel_id} in guild {ctx.guild_id}")
|
||||
|
||||
if not ctx.guild_id:
|
||||
logging.warning("[VC_EXT] Guild id not found in context inside 'create_menu'")
|
||||
logging.warning("[VC_EXT] Guild id not found in context")
|
||||
return False
|
||||
|
||||
guild = await self.db.get_guild(ctx.guild_id, projection={'current_track': 1, 'current_menu': 1, 'vibing': 1})
|
||||
@@ -65,28 +61,17 @@ class VoiceExtension:
|
||||
if (message := await self.get_menu_message(ctx, guild['current_menu'])):
|
||||
await message.delete()
|
||||
|
||||
await self._update_menu_views_dict(ctx, disable=disable)
|
||||
|
||||
if isinstance(ctx, (ApplicationContext, Interaction)):
|
||||
interaction = await ctx.respond(view=menu_views[ctx.guild_id], embed=embed)
|
||||
elif not self.bot:
|
||||
raise ValueError("Bot instance is not set.")
|
||||
elif not (channel := self.bot.get_channel(ctx.channel_id)):
|
||||
logging.warning(f"[VC_EXT] Channel {ctx.channel_id} not found in guild {ctx.guild_id}")
|
||||
return False
|
||||
elif isinstance(channel, discord.VoiceChannel):
|
||||
interaction = await channel.send(
|
||||
view=menu_views[ctx.guild_id],
|
||||
embed=embed # type: ignore # Wrong typehints.
|
||||
)
|
||||
else:
|
||||
logging.warning(f"[VC_EXT] Channel {ctx.channel_id} is not a voice channel in guild {ctx.guild_id}")
|
||||
return False
|
||||
await self.update_menu_views_dict(ctx, disable=disable)
|
||||
|
||||
interaction = await self.send_response_message(ctx, embed=embed, view=self.menu_views[ctx.guild_id])
|
||||
response = await interaction.original_response() if isinstance(interaction, discord.Interaction) else interaction
|
||||
await self.db.update(ctx.guild_id, {'current_menu': response.id})
|
||||
|
||||
logging.info(f"[VC_EXT] New menu message {response.id} created in guild {ctx.guild_id}")
|
||||
if response:
|
||||
await self.db.update(ctx.guild_id, {'current_menu': response.id})
|
||||
logging.info(f"[VC_EXT] New menu message {response.id} created in guild {ctx.guild_id}")
|
||||
else:
|
||||
logging.warning(f"[VC_EXT] Failed to save menu message id. Invalid response.")
|
||||
|
||||
return True
|
||||
|
||||
async def get_menu_message(self, ctx: ApplicationContext | Interaction | RawReactionActionEvent, menu_mid: int) -> discord.Message | None:
|
||||
@@ -107,18 +92,9 @@ class VoiceExtension:
|
||||
return None
|
||||
|
||||
try:
|
||||
if isinstance(ctx, ApplicationContext):
|
||||
menu = await ctx.fetch_message(menu_mid)
|
||||
elif isinstance(ctx, Interaction):
|
||||
menu = ctx.client.get_message(menu_mid)
|
||||
elif not self.bot:
|
||||
raise ValueError("Bot instance is not set.")
|
||||
else:
|
||||
menu = self.bot.get_message(menu_mid)
|
||||
except discord.DiscordException as e:
|
||||
logging.debug(f"[VC_EXT] Failed to get menu message: {e}")
|
||||
await self.db.update(ctx.guild_id, {'current_menu': None})
|
||||
return None
|
||||
menu = await self.get_message_by_id(ctx, menu_mid)
|
||||
except discord.DiscordException:
|
||||
menu = None
|
||||
|
||||
if not menu:
|
||||
logging.debug(f"[VC_EXT] Menu message {menu_mid} not found in guild {ctx.guild_id}")
|
||||
@@ -128,7 +104,7 @@ class VoiceExtension:
|
||||
logging.debug(f"[VC_EXT] Menu message {menu_mid} successfully fetched")
|
||||
return menu
|
||||
|
||||
async def update_menu_full(
|
||||
async def update_menu_embed_and_view(
|
||||
self,
|
||||
ctx: ApplicationContext | Interaction | RawReactionActionEvent,
|
||||
*,
|
||||
@@ -151,17 +127,15 @@ class VoiceExtension:
|
||||
"interaction context" if isinstance(ctx, Interaction) else
|
||||
"application context" if isinstance(ctx, ApplicationContext) else
|
||||
"raw reaction context"
|
||||
)
|
||||
)
|
||||
))
|
||||
|
||||
gid = ctx.guild_id
|
||||
uid = ctx.user_id if isinstance(ctx, discord.RawReactionActionEvent) else ctx.user.id if ctx.user else None
|
||||
|
||||
if not gid or not uid:
|
||||
if not ctx.guild_id or not uid:
|
||||
logging.warning("[VC_EXT] Guild ID or User ID not found in context inside 'update_menu_embed'")
|
||||
return False
|
||||
|
||||
guild = await self.db.get_guild(gid, projection={'vibing': 1, 'current_menu': 1, 'current_track': 1})
|
||||
guild = await self.db.get_guild(ctx.guild_id, projection={'vibing': 1, 'current_menu': 1, 'current_track': 1})
|
||||
if not guild['current_menu']:
|
||||
logging.debug("[VC_EXT] No current menu found")
|
||||
return False
|
||||
@@ -180,8 +154,7 @@ class VoiceExtension:
|
||||
))
|
||||
embed = await generate_item_embed(track, guild['vibing'])
|
||||
|
||||
vc = await self.get_voice_client(ctx)
|
||||
if not vc:
|
||||
if not (vc := await self.get_voice_client(ctx)):
|
||||
logging.warning("[VC_EXT] Voice client not found")
|
||||
return False
|
||||
|
||||
@@ -190,16 +163,16 @@ class VoiceExtension:
|
||||
else:
|
||||
embed.remove_footer()
|
||||
|
||||
await self._update_menu_views_dict(ctx)
|
||||
await self.update_menu_views_dict(ctx)
|
||||
try:
|
||||
if isinstance(ctx, Interaction) and button_callback:
|
||||
# If interaction from menu buttons
|
||||
await ctx.edit(embed=embed, view=menu_views[gid])
|
||||
await ctx.edit(embed=embed, view=self.menu_views[ctx.guild_id])
|
||||
else:
|
||||
# If interaction from other buttons or commands. They should have their own response.
|
||||
await menu_message.edit(embed=embed, view=menu_views[gid])
|
||||
except discord.NotFound:
|
||||
logging.warning("[VC_EXT] Menu message not found")
|
||||
await menu_message.edit(embed=embed, view=self.menu_views[ctx.guild_id])
|
||||
except discord.DiscordException as e:
|
||||
logging.warning(f"[VC_EXT] Error while updating menu message: {e}")
|
||||
return False
|
||||
|
||||
logging.debug("[VC_EXT] Menu embed updated successfully")
|
||||
@@ -231,24 +204,26 @@ class VoiceExtension:
|
||||
logging.warning("[VC_EXT] Guild ID not found in context inside 'update_menu_view'")
|
||||
return False
|
||||
|
||||
guild = await self.db.get_guild(ctx.guild_id, projection={'current_menu': 1})
|
||||
if not guild['current_menu']:
|
||||
return False
|
||||
if not menu_message:
|
||||
guild = await self.db.get_guild(ctx.guild_id, projection={'current_menu': 1})
|
||||
if not guild['current_menu']:
|
||||
return False
|
||||
|
||||
menu_message = await self.get_menu_message(ctx, guild['current_menu']) if not menu_message else menu_message
|
||||
|
||||
menu_message = await self.get_menu_message(ctx, guild['current_menu']) if not menu_message else menu_message
|
||||
if not menu_message:
|
||||
return False
|
||||
|
||||
await self._update_menu_views_dict(ctx, disable=disable)
|
||||
await self.update_menu_views_dict(ctx, disable=disable)
|
||||
try:
|
||||
if isinstance(ctx, Interaction) and button_callback:
|
||||
# If interaction from menu buttons
|
||||
await ctx.edit(view=menu_views[ctx.guild_id])
|
||||
await ctx.edit(view=self.menu_views[ctx.guild_id])
|
||||
else:
|
||||
# If interaction from other buttons or commands. They should have their own response.
|
||||
await menu_message.edit(view=menu_views[ctx.guild_id])
|
||||
except discord.NotFound:
|
||||
logging.warning("[VC_EXT] Menu message not found")
|
||||
await menu_message.edit(view=self.menu_views[ctx.guild_id])
|
||||
except discord.DiscordException as e:
|
||||
logging.warning(f"[VC_EXT] Error while updating menu view: {e}")
|
||||
return False
|
||||
|
||||
logging.debug("[VC_EXT] Menu view updated successfully")
|
||||
@@ -257,8 +232,8 @@ class VoiceExtension:
|
||||
async def update_vibe(
|
||||
self,
|
||||
ctx: ApplicationContext | Interaction | RawReactionActionEvent,
|
||||
type: str,
|
||||
id: str | int,
|
||||
vibe_type: str,
|
||||
item_id: str | int,
|
||||
*,
|
||||
viber_id: int | None = None,
|
||||
update_settings: bool = False
|
||||
@@ -268,28 +243,26 @@ class VoiceExtension:
|
||||
|
||||
Args:
|
||||
ctx (ApplicationContext | Interaction | RawReactionActionEvent): Context.
|
||||
type (str): Type of the item.
|
||||
id (str | int): ID of the item.
|
||||
vibe_type (str): Type of the item.
|
||||
item_id (str | int): ID of the item.
|
||||
viber_id (int | None, optional): ID of the user who started vibe. If None, uses user id in context. Defaults to None.
|
||||
update_settings (bool, optional): Update vibe settings by sending feedack usind data from database. Defaults to False.
|
||||
|
||||
Returns:
|
||||
bool: True if vibe was updated successfully. False otherwise.
|
||||
"""
|
||||
logging.info(f"[VC_EXT] Updating vibe for guild {ctx.guild_id} with type '{type}' and id '{id}'")
|
||||
logging.info(f"[VC_EXT] Updating vibe for guild {ctx.guild_id} with type '{vibe_type}' and id '{item_id}'")
|
||||
|
||||
gid = ctx.guild_id
|
||||
uid = viber_id if viber_id else ctx.user_id if isinstance(ctx, discord.RawReactionActionEvent) else ctx.user.id if ctx.user else None
|
||||
|
||||
if not uid or not gid:
|
||||
if not uid or not ctx.guild_id:
|
||||
logging.warning("[VC_EXT] Guild ID or User ID not found in context")
|
||||
return False
|
||||
|
||||
user = await self.users_db.get_user(uid, projection={'ym_token': 1, 'vibe_settings': 1})
|
||||
guild = await self.db.get_guild(gid, projection={'vibing': 1, 'current_track': 1})
|
||||
client = await self.init_ym_client(ctx, user['ym_token'])
|
||||
guild = await self.db.get_guild(ctx.guild_id, projection={'vibing': 1, 'current_track': 1})
|
||||
|
||||
if not client:
|
||||
if not (client := await self.init_ym_client(ctx, user['ym_token'])):
|
||||
return False
|
||||
|
||||
if update_settings:
|
||||
@@ -297,7 +270,7 @@ class VoiceExtension:
|
||||
|
||||
settings = user['vibe_settings']
|
||||
await client.rotor_station_settings2(
|
||||
f"{type}:{id}",
|
||||
f"{vibe_type}:{item_id}",
|
||||
mood_energy=settings['mood'],
|
||||
diversity=settings['diversity'],
|
||||
language=settings['lang']
|
||||
@@ -306,7 +279,7 @@ class VoiceExtension:
|
||||
if not guild['vibing']:
|
||||
try:
|
||||
feedback = await client.rotor_station_feedback_radio_started(
|
||||
f"{type}:{id}",
|
||||
f"{vibe_type}:{item_id}",
|
||||
f"desktop-user-{client.me.account.uid}", # type: ignore # That's made up, but it doesn't do much anyway.
|
||||
)
|
||||
except yandex_music.exceptions.BadRequestError as e:
|
||||
@@ -314,11 +287,11 @@ class VoiceExtension:
|
||||
return False
|
||||
|
||||
if not feedback:
|
||||
logging.warning(f"[VIBE] Failed to start radio '{type}:{id}'")
|
||||
logging.warning(f"[VIBE] Failed to start radio '{vibe_type}:{item_id}'")
|
||||
return False
|
||||
|
||||
tracks = await client.rotor_station_tracks(
|
||||
f"{type}:{id}",
|
||||
f"{vibe_type}:{item_id}",
|
||||
queue=guild['current_track']['id'] if guild['current_track'] else None # type: ignore
|
||||
)
|
||||
|
||||
@@ -330,11 +303,11 @@ class VoiceExtension:
|
||||
logging.debug(f"[VIBE] Got next vibe tracks: {[track.title for track in next_tracks]}")
|
||||
|
||||
await self.users_db.update(uid, {
|
||||
'vibe_type': type,
|
||||
'vibe_id': id,
|
||||
'vibe_type': vibe_type,
|
||||
'vibe_id': item_id,
|
||||
'vibe_batch_id': tracks.batch_id
|
||||
})
|
||||
await self.db.update(gid, {
|
||||
await self.db.update(ctx.guild_id, {
|
||||
'next_tracks': [track.to_dict() for track in next_tracks],
|
||||
'current_viber_id': uid,
|
||||
'vibing': True
|
||||
@@ -352,9 +325,14 @@ class VoiceExtension:
|
||||
Returns:
|
||||
bool: Check result.
|
||||
"""
|
||||
if not ctx.user or not ctx.guild_id:
|
||||
logging.warning("[VC_EXT] User or guild id not found in context inside 'voice_check'")
|
||||
await ctx.respond("❌ Что-то пошло не так. Попробуйте еще раз.", delete_after=15, ephemeral=True)
|
||||
if not ctx.user:
|
||||
logging.info("[VC_EXT] User not found in context inside 'voice_check'")
|
||||
await ctx.respond("❌ Пользователь не найден.", delete_after=15, ephemeral=True)
|
||||
return False
|
||||
|
||||
if not ctx.guild_id:
|
||||
logging.info("[VC_EXT] Guild id not found in context inside 'voice_check'")
|
||||
await ctx.respond("❌ Эта команда может быть использована только на сервере.", delete_after=15, ephemeral=True)
|
||||
return False
|
||||
|
||||
if not await self.users_db.get_ym_token(ctx.user.id):
|
||||
@@ -400,20 +378,16 @@ class VoiceExtension:
|
||||
if isinstance(ctx, (Interaction, ApplicationContext)):
|
||||
voice_clients = ctx.client.voice_clients if isinstance(ctx, Interaction) else ctx.bot.voice_clients
|
||||
guild = ctx.guild
|
||||
elif isinstance(ctx, RawReactionActionEvent):
|
||||
if not self.bot:
|
||||
raise ValueError("Bot instance is not set.")
|
||||
if not ctx.guild_id:
|
||||
logging.warning("[VC_EXT] Guild ID not found in context inside 'get_voice_client'")
|
||||
return None
|
||||
elif not self.bot:
|
||||
raise ValueError("Bot instance is not set.")
|
||||
elif not ctx.guild_id:
|
||||
logging.warning("[VC_EXT] Guild ID not found in context")
|
||||
return None
|
||||
else:
|
||||
voice_clients = self.bot.voice_clients
|
||||
guild = await self.bot.fetch_guild(ctx.guild_id)
|
||||
else:
|
||||
raise ValueError(f"Invalid context type: '{type(ctx).__name__}'.")
|
||||
|
||||
voice_client = discord.utils.get(voice_clients, guild=guild)
|
||||
|
||||
if voice_client:
|
||||
if (voice_client := discord.utils.get(voice_clients, guild=guild)):
|
||||
logging.debug("[VC_EXT] Voice client found")
|
||||
else:
|
||||
logging.debug("[VC_EXT] Voice client not found")
|
||||
@@ -484,34 +458,32 @@ class VoiceExtension:
|
||||
"""
|
||||
logging.debug("[VC_EXT] Stopping playback")
|
||||
|
||||
gid = ctx.guild_id
|
||||
uid = ctx.user_id if isinstance(ctx, discord.RawReactionActionEvent) else ctx.user.id if ctx.user else None
|
||||
|
||||
if not gid or not uid:
|
||||
if not ctx.guild_id:
|
||||
logging.warning("[VC_EXT] Guild ID not found in context")
|
||||
return False
|
||||
|
||||
guild = await self.db.get_guild(gid, projection={'current_menu': 1, 'current_track': 1, 'vibing': 1})
|
||||
vc = await self.get_voice_client(ctx) if not vc else vc
|
||||
|
||||
if not vc:
|
||||
return False
|
||||
|
||||
await self.db.update(gid, {'current_track': None, 'is_stopped': True})
|
||||
await self.db.update(ctx.guild_id, {'current_track': None, 'is_stopped': True})
|
||||
vc.stop()
|
||||
|
||||
if full:
|
||||
guild = await self.db.get_guild(ctx.guild_id, projection={'current_menu': 1, 'current_track': 1, 'vibing': 1})
|
||||
if guild['vibing'] and guild['current_track']:
|
||||
await self.send_vibe_feedback(ctx, 'trackFinished', guild['current_track'])
|
||||
|
||||
await self.db.update(ctx.guild_id, {
|
||||
'current_menu': None, 'repeat': False, 'shuffle': False, 'previous_tracks': [], 'next_tracks': [], 'votes': {}, 'vibing': False
|
||||
})
|
||||
|
||||
if not guild['current_menu']:
|
||||
return True
|
||||
|
||||
return await self._full_stop(ctx, guild['current_menu'], gid)
|
||||
if guild['current_menu']:
|
||||
return await self._delete_menu_message(ctx, guild['current_menu'], ctx.guild_id)
|
||||
|
||||
return True
|
||||
|
||||
async def next_track(
|
||||
async def play_next_track(
|
||||
self,
|
||||
ctx: ApplicationContext | Interaction | RawReactionActionEvent,
|
||||
vc: discord.VoiceClient | None = None,
|
||||
@@ -537,44 +509,38 @@ class VoiceExtension:
|
||||
"""
|
||||
logging.debug("[VC_EXT] Switching to next track")
|
||||
|
||||
gid = ctx.guild_id
|
||||
uid = ctx.user_id if isinstance(ctx, discord.RawReactionActionEvent) else ctx.user.id if ctx.user else None
|
||||
|
||||
if not gid or not uid:
|
||||
if not ctx.guild_id or not uid:
|
||||
logging.warning("[VC_EXT] Guild ID or User ID not found in context inside 'next_track'")
|
||||
return None
|
||||
|
||||
guild = await self.db.get_guild(gid, projection={'shuffle': 1, 'repeat': 1, 'is_stopped': 1, 'current_menu': 1, 'vibing': 1, 'current_track': 1})
|
||||
guild = await self.db.get_guild(ctx.guild_id, projection={'shuffle': 1, 'repeat': 1, 'is_stopped': 1, 'current_menu': 1, 'vibing': 1, 'current_track': 1})
|
||||
user = await self.users_db.get_user(uid)
|
||||
|
||||
if guild['is_stopped'] and after:
|
||||
logging.debug("[VC_EXT] Playback is stopped, skipping after callback.")
|
||||
return None
|
||||
|
||||
if guild['current_track'] and guild['current_menu'] and not guild['repeat']:
|
||||
if guild['current_track'] and not guild['repeat']:
|
||||
logging.debug("[VC_EXT] Adding current track to history")
|
||||
await self.db.modify_track(gid, guild['current_track'], 'previous', 'insert')
|
||||
await self.db.modify_track(ctx.guild_id, guild['current_track'], 'previous', 'insert')
|
||||
|
||||
if after and guild['current_menu']:
|
||||
await self.update_menu_view(ctx, menu_message=menu_message, disable=True)
|
||||
if after and not await self.update_menu_view(ctx, menu_message=menu_message, disable=True):
|
||||
await self.send_response_message(ctx, "❌ Не удалось обновить меню.", ephemeral=True, delete_after=15)
|
||||
|
||||
if guild['vibing'] and guild['current_track']:
|
||||
if not await self.send_vibe_feedback(ctx, 'trackFinished' if after else 'skip', guild['current_track']):
|
||||
if not isinstance(ctx, RawReactionActionEvent):
|
||||
await ctx.respond("❌ Не удалось отправить отчёт об оконачнии Моей Волны.", ephemeral=True, delete_after=15)
|
||||
elif self.bot:
|
||||
channel = cast(discord.VoiceChannel, self.bot.get_channel(ctx.channel_id))
|
||||
await channel.send("❌ Не удалось отправить отчёт об оконачнии Моей Волны.", delete_after=15)
|
||||
await self.send_vibe_feedback(ctx, 'trackFinished' if after else 'skip', guild['current_track'])
|
||||
|
||||
if guild['repeat'] and after:
|
||||
logging.debug("[VC_EXT] Repeating current track")
|
||||
next_track = guild['current_track']
|
||||
elif guild['shuffle']:
|
||||
logging.debug("[VC_EXT] Getting random track from queue")
|
||||
next_track = await self.db.pop_random_track(gid, 'next')
|
||||
next_track = await self.db.pop_random_track(ctx.guild_id, 'next')
|
||||
else:
|
||||
logging.debug("[VC_EXT] Getting next track from queue")
|
||||
next_track = await self.db.get_track(gid, 'next')
|
||||
next_track = await self.db.get_track(ctx.guild_id, 'next')
|
||||
|
||||
if not next_track and guild['vibing']:
|
||||
logging.debug("[VC_EXT] No next track found, generating new vibe")
|
||||
@@ -583,7 +549,7 @@ class VoiceExtension:
|
||||
return None
|
||||
|
||||
await self.update_vibe(ctx, user['vibe_type'], user['vibe_id'])
|
||||
next_track = await self.db.get_track(gid, 'next')
|
||||
next_track = await self.db.get_track(ctx.guild_id, 'next')
|
||||
|
||||
if next_track:
|
||||
title = await self.play_track(ctx, next_track, client=client, vc=vc, button_callback=button_callback)
|
||||
@@ -602,11 +568,11 @@ class VoiceExtension:
|
||||
|
||||
logging.info("[VC_EXT] No next track found")
|
||||
if after:
|
||||
await self.db.update(gid, {'is_stopped': True, 'current_track': None})
|
||||
await self.db.update(ctx.guild_id, {'is_stopped': True, 'current_track': None})
|
||||
|
||||
return None
|
||||
|
||||
async def previous_track(self, ctx: ApplicationContext | Interaction | RawReactionActionEvent, button_callback: bool = False) -> str | None:
|
||||
async def play_previous_track(self, ctx: ApplicationContext | Interaction | RawReactionActionEvent, button_callback: bool = False) -> str | None:
|
||||
"""Switch to the previous track in the queue. Repeat current track if no previous one found.
|
||||
Return track title on success. Should be called only if there's already track playing.
|
||||
|
||||
@@ -619,15 +585,14 @@ class VoiceExtension:
|
||||
"""
|
||||
logging.debug("[VC_EXT] Switching to previous track")
|
||||
|
||||
gid = ctx.guild_id
|
||||
uid = ctx.user_id if isinstance(ctx, discord.RawReactionActionEvent) else ctx.user.id if ctx.user else None
|
||||
|
||||
if not gid or not uid:
|
||||
if not ctx.guild_id or not uid:
|
||||
logging.warning("[VC_EXT] Guild ID or User ID not found in context inside 'next_track'")
|
||||
return None
|
||||
|
||||
current_track = await self.db.get_track(gid, 'current')
|
||||
prev_track = await self.db.get_track(gid, 'previous')
|
||||
current_track = await self.db.get_track(ctx.guild_id, 'current')
|
||||
prev_track = await self.db.get_track(ctx.guild_id, 'previous')
|
||||
|
||||
if prev_track:
|
||||
logging.debug("[VC_EXT] Previous track found")
|
||||
@@ -644,34 +609,32 @@ class VoiceExtension:
|
||||
|
||||
return None
|
||||
|
||||
async def get_likes(self, ctx: ApplicationContext | Interaction | RawReactionActionEvent) -> list[TrackShort] | None:
|
||||
"""Get liked tracks. Return list of tracks on success. Return None if no token found.
|
||||
async def get_liked_tracks(self, ctx: ApplicationContext | Interaction | RawReactionActionEvent) -> list[TrackShort]:
|
||||
"""Get liked tracks from Yandex Music. Return list of tracks on success.
|
||||
Return empty list if no likes found or error occurred.
|
||||
|
||||
Args:
|
||||
ctx (ApplicationContext | Interaction | RawReactionActionEvent): Context.
|
||||
|
||||
Returns:
|
||||
(list[Track] | None): List of tracks or None.
|
||||
list[Track]: List of tracks.
|
||||
"""
|
||||
logging.info("[VC_EXT] Getting liked tracks")
|
||||
|
||||
if not ctx.guild_id:
|
||||
logging.warning("Guild ID not found in context inside 'get_likes'")
|
||||
return None
|
||||
|
||||
client = await self.init_ym_client(ctx)
|
||||
logging.warning("Guild ID not found in context")
|
||||
return []
|
||||
|
||||
if not await self.db.get_track(ctx.guild_id, 'current'):
|
||||
logging.debug("[VC_EXT] Current track not found in 'get_likes'")
|
||||
return None
|
||||
logging.debug("[VC_EXT] Current track not found. Likes can't be fetched")
|
||||
return []
|
||||
|
||||
if not client:
|
||||
return None
|
||||
if not (client := await self.init_ym_client(ctx)):
|
||||
return []
|
||||
|
||||
likes = await client.users_likes_tracks()
|
||||
if not likes:
|
||||
if not (likes := await client.users_likes_tracks()):
|
||||
logging.info("[VC_EXT] No likes found")
|
||||
return None
|
||||
return []
|
||||
|
||||
return likes.tracks
|
||||
|
||||
@@ -724,48 +687,6 @@ class VoiceExtension:
|
||||
logging.debug(f"[VC_EXT] Track found in {action}s. Removing...")
|
||||
await remove_func(current_track['id'])
|
||||
return (True, 'removed')
|
||||
|
||||
async def init_ym_client(self, ctx: ApplicationContext | Interaction | RawReactionActionEvent, token: str | None = None) -> YMClient | None:
|
||||
"""Initialize Yandex Music client. Return client on success. Return None if no token found and respond to the context.
|
||||
|
||||
Args:
|
||||
ctx (ApplicationContext | Interaction | RawReactionActionEvent): Context.
|
||||
token (str | None, optional): Token. Fetched from database if not provided. Defaults to None.
|
||||
|
||||
Returns:
|
||||
(YMClient | None): Client or None.
|
||||
"""
|
||||
logging.debug("[VC_EXT] Initializing Yandex Music client")
|
||||
|
||||
if not token:
|
||||
uid = ctx.user_id if isinstance(ctx, discord.RawReactionActionEvent) else ctx.user.id if ctx.user else None
|
||||
token = await self.users_db.get_ym_token(uid) if uid else None
|
||||
|
||||
if not token:
|
||||
logging.debug("No token found in 'init_ym_client'")
|
||||
if not isinstance(ctx, discord.RawReactionActionEvent):
|
||||
await ctx.respond("❌ Укажите токен через /account login.", delete_after=15, ephemeral=True)
|
||||
return None
|
||||
|
||||
if not hasattr(self, '_ym_clients'):
|
||||
self._ym_clients: dict[str, YMClient] = {}
|
||||
|
||||
if token in self._ym_clients:
|
||||
client = self._ym_clients[token]
|
||||
try:
|
||||
await client.account_status()
|
||||
return client
|
||||
except yandex_music.exceptions.UnauthorizedError:
|
||||
del self._ym_clients[token]
|
||||
return None
|
||||
try:
|
||||
client = await YMClient(token).init()
|
||||
except yandex_music.exceptions.UnauthorizedError:
|
||||
logging.debug("UnauthorizedError in 'init_ym_client'")
|
||||
return None
|
||||
|
||||
self._ym_clients[token] = client
|
||||
return client
|
||||
|
||||
async def proccess_vote(self, ctx: RawReactionActionEvent, guild: ExplicitGuild, channel: VoiceChannel, vote_data: MessageVotes) -> bool:
|
||||
"""Proccess vote and perform action from `vote_data` and respond. Return True on success.
|
||||
@@ -787,14 +708,16 @@ class VoiceExtension:
|
||||
|
||||
if not guild['current_menu'] and not await self.send_menu_message(ctx):
|
||||
await channel.send(content=f"❌ Не удалось отправить меню! Попробуйте ещё раз.", delete_after=15)
|
||||
return False
|
||||
|
||||
if vote_data['action'] in ('next', 'previous'):
|
||||
if not guild.get(f'{vote_data['action']}_tracks'):
|
||||
logging.info(f"[VOICE] No {vote_data['action']} tracks found for message {ctx.message_id}")
|
||||
await channel.send(content=f"❌ Очередь пуста!", delete_after=15)
|
||||
|
||||
elif not (await self.next_track(ctx) if vote_data['action'] == 'next' else await self.previous_track(ctx)):
|
||||
elif not (await self.play_next_track(ctx) if vote_data['action'] == 'next' else await self.play_previous_track(ctx)):
|
||||
await channel.send(content=f"❌ Ошибка при смене трека! Попробуйте ещё раз.", delete_after=15)
|
||||
return False
|
||||
|
||||
elif vote_data['action'] == 'add_track':
|
||||
if not vote_data['vote_content']:
|
||||
@@ -805,9 +728,9 @@ class VoiceExtension:
|
||||
|
||||
if guild['current_track']:
|
||||
await channel.send(content=f"✅ Трек был добавлен в очередь!", delete_after=15)
|
||||
else:
|
||||
if not await self.next_track(ctx):
|
||||
await channel.send(content=f"❌ Ошибка при воспроизведении! Попробуйте ещё раз.", delete_after=15)
|
||||
elif not await self.play_next_track(ctx):
|
||||
await channel.send(content=f"❌ Ошибка при воспроизведении! Попробуйте ещё раз.", delete_after=15)
|
||||
return False
|
||||
|
||||
elif vote_data['action'] in ('add_album', 'add_artist', 'add_playlist'):
|
||||
if not vote_data['vote_content']:
|
||||
@@ -819,9 +742,9 @@ class VoiceExtension:
|
||||
|
||||
if guild['current_track']:
|
||||
await channel.send(content=f"✅ Контент был добавлен в очередь!", delete_after=15)
|
||||
else:
|
||||
if not await self.next_track(ctx):
|
||||
await channel.send(content=f"❌ Ошибка при воспроизведении! Попробуйте ещё раз.", delete_after=15)
|
||||
elif not await self.play_next_track(ctx):
|
||||
await channel.send(content=f"❌ Ошибка при воспроизведении! Попробуйте ещё раз.", delete_after=15)
|
||||
return False
|
||||
|
||||
elif vote_data['action'] == 'play/pause':
|
||||
if not (vc := await self.get_voice_client(ctx)):
|
||||
@@ -833,7 +756,7 @@ class VoiceExtension:
|
||||
else:
|
||||
vc.resume()
|
||||
|
||||
await self.update_menu_full(ctx)
|
||||
await self.update_menu_embed_and_view(ctx)
|
||||
|
||||
elif vote_data['action'] in ('repeat', 'shuffle'):
|
||||
await self.db.update(guild['_id'], {vote_data['action']: not guild[vote_data['action']]})
|
||||
@@ -844,26 +767,25 @@ class VoiceExtension:
|
||||
await channel.send("✅ Очередь и история сброшены.", delete_after=15)
|
||||
|
||||
elif vote_data['action'] == 'stop':
|
||||
res = await self.stop_playing(ctx, full=True)
|
||||
if res:
|
||||
if await self.stop_playing(ctx, full=True):
|
||||
await channel.send("✅ Воспроизведение остановлено.", delete_after=15)
|
||||
else:
|
||||
await channel.send("❌ Произошла ошибка при остановке воспроизведения.", delete_after=15)
|
||||
return False
|
||||
|
||||
elif vote_data['action'] == 'vibe_station':
|
||||
_type, _id, viber_id = vote_data['vote_content'] if isinstance(vote_data['vote_content'], list) else (None, None, None)
|
||||
vibe_type, vibe_id, viber_id = vote_data['vote_content'] if isinstance(vote_data['vote_content'], list) else (None, None, None)
|
||||
|
||||
if not _type or not _id or not viber_id:
|
||||
if not vibe_type or not vibe_id or not viber_id:
|
||||
logging.warning(f"[VOICE] Recieved empty vote context for message {ctx.message_id}")
|
||||
await channel.send("❌ Произошла ошибка при обновлении станции.", delete_after=15)
|
||||
return False
|
||||
|
||||
if not await self.update_vibe(ctx, _type, _id, viber_id=viber_id):
|
||||
if not await self.update_vibe(ctx, vibe_type, vibe_id, viber_id=viber_id):
|
||||
await channel.send("❌ Операция не удалась. Возможно, у вес нет подписки на Яндекс Музыку.", delete_after=15)
|
||||
return False
|
||||
|
||||
next_track = await self.db.get_track(ctx.guild_id, 'next')
|
||||
if next_track:
|
||||
if (next_track := await self.db.get_track(ctx.guild_id, 'next')):
|
||||
await self.play_track(ctx, next_track)
|
||||
else:
|
||||
await channel.send("❌ Не удалось воспроизвести трек.", delete_after=15)
|
||||
@@ -908,14 +830,14 @@ class VoiceExtension:
|
||||
client = await self.init_ym_client(ctx, user['ym_token'])
|
||||
if not client:
|
||||
logging.info(f"[VC_EXT] Failed to init YM client for user {user['_id']}")
|
||||
if not isinstance(ctx, RawReactionActionEvent):
|
||||
await ctx.respond("❌ Что-то пошло не так. Попробуйте позже.", delete_after=15, ephemeral=True)
|
||||
elif self.bot:
|
||||
channel = cast(discord.VoiceChannel, self.bot.get_channel(ctx.channel_id))
|
||||
await channel.send("❌ Что-то пошло не так. Попробуйте позже.", delete_after=15)
|
||||
await self.send_response_message(ctx, "❌ Что-то пошло не так. Попробуйте позже.", delete_after=15, ephemeral=True)
|
||||
return False
|
||||
|
||||
total_play_seconds = track['duration_ms'] // 1000 if feedback_type not in ('radioStarted', 'trackStarted') and track['duration_ms'] else None
|
||||
|
||||
if feedback_type not in ('radioStarted', 'trackStarted') and track['duration_ms']:
|
||||
total_play_seconds = track['duration_ms'] // 1000
|
||||
else:
|
||||
total_play_seconds = None
|
||||
|
||||
try:
|
||||
feedback = await client.rotor_station_feedback(
|
||||
f'{user['vibe_type']}:{user['vibe_id']}',
|
||||
@@ -930,32 +852,6 @@ class VoiceExtension:
|
||||
|
||||
logging.info(f"[VC_EXT] Sent vibe feedback type '{feedback_type}' with result: {feedback}")
|
||||
return feedback
|
||||
|
||||
async def _update_menu_views_dict(
|
||||
self,
|
||||
ctx: ApplicationContext | Interaction | RawReactionActionEvent,
|
||||
*,
|
||||
disable: bool = False
|
||||
) -> None:
|
||||
"""Genereate a new menu view and update the `menu_views` dict. This prevents creating multiple menu views for the same guild.
|
||||
Use guild id as a key to access menu view.
|
||||
|
||||
Args:
|
||||
ctx (ApplicationContext | Interaction | RawReactionActionEvent): Context
|
||||
guild (ExplicitGuild): Guild.
|
||||
disable (bool, optional): Disable menu. Defaults to False.
|
||||
"""
|
||||
logging.debug(f"[VC_EXT] Updating menu views dict for guild {ctx.guild_id}")
|
||||
from MusicBot.ui import MenuView
|
||||
|
||||
if not ctx.guild_id:
|
||||
logging.warning("[VC_EXT] Guild not found")
|
||||
return
|
||||
|
||||
if ctx.guild_id in menu_views:
|
||||
menu_views[ctx.guild_id].stop()
|
||||
|
||||
menu_views[ctx.guild_id] = await MenuView(ctx).init(disable=disable)
|
||||
|
||||
async def _download_track(self, gid: int, track: Track) -> None:
|
||||
"""Download track to local storage. Return True on success.
|
||||
@@ -970,8 +866,8 @@ class VoiceExtension:
|
||||
logging.warning(f"[VC_EXT] Timed out while downloading track '{track.title}'")
|
||||
raise
|
||||
|
||||
async def _full_stop(self, ctx: ApplicationContext | Interaction | RawReactionActionEvent, current_menu: int, gid: int) -> Literal[True]:
|
||||
"""Stop all actions and delete menu. Return True on success.
|
||||
async def _delete_menu_message(self, ctx: ApplicationContext | Interaction | RawReactionActionEvent, current_menu: int, gid: int) -> Literal[True]:
|
||||
"""Delete current menu message and stop menu view. Return True on success.
|
||||
|
||||
Args:
|
||||
ctx (ApplicationContext | Interaction | RawReactionActionEvent): Context.
|
||||
@@ -982,16 +878,13 @@ class VoiceExtension:
|
||||
"""
|
||||
logging.debug("[VC_EXT] Performing full stop")
|
||||
|
||||
if gid in menu_views:
|
||||
menu_views[gid].stop()
|
||||
del menu_views[gid]
|
||||
if gid in self.menu_views:
|
||||
self.menu_views[gid].stop()
|
||||
del self.menu_views[gid]
|
||||
|
||||
if (menu := await self.get_menu_message(ctx, current_menu)):
|
||||
await menu.delete()
|
||||
|
||||
await self.db.update(gid, {
|
||||
'current_menu': None, 'repeat': False, 'shuffle': False, 'previous_tracks': [], 'next_tracks': [], 'votes': {}, 'vibing': False
|
||||
})
|
||||
return True
|
||||
|
||||
async def _play_track(
|
||||
@@ -1052,7 +945,7 @@ class VoiceExtension:
|
||||
|
||||
if menu_message or guild['current_menu']:
|
||||
# Updating menu message before playing to prevent delay and avoid FFMPEG lags.
|
||||
await self.update_menu_full(ctx, menu_message=menu_message, button_callback=button_callback)
|
||||
await self.update_menu_embed_and_view(ctx, menu_message=menu_message, button_callback=button_callback)
|
||||
|
||||
if not guild['vibing']:
|
||||
# Giving FFMPEG enough time to process the audio file
|
||||
@@ -1060,22 +953,14 @@ class VoiceExtension:
|
||||
|
||||
loop = self._get_current_event_loop(ctx)
|
||||
try:
|
||||
vc.play(song, after=lambda exc: asyncio.run_coroutine_threadsafe(self.next_track(ctx, after=True), loop))
|
||||
vc.play(song, after=lambda exc: asyncio.run_coroutine_threadsafe(self.play_next_track(ctx, after=True), loop))
|
||||
except discord.errors.ClientException as e:
|
||||
logging.error(f"[VC_EXT] Error while playing track '{track.title}': {e}")
|
||||
if not isinstance(ctx, RawReactionActionEvent):
|
||||
await ctx.respond(f"❌ Не удалось проиграть трек. Попробуйте сбросить меню.", delete_after=15, ephemeral=True)
|
||||
elif self.bot:
|
||||
channel = cast(discord.VoiceChannel, self.bot.get_channel(ctx.channel_id))
|
||||
await channel.send(f"❌ Не удалось проиграть трек. Попробуйте сбросить меню.", delete_after=15)
|
||||
await self.send_response_message(ctx, f"❌ Не удалось проиграть трек. Попробуйте сбросить меню.", delete_after=15, ephemeral=True)
|
||||
return None
|
||||
except yandex_music.exceptions.InvalidBitrateError:
|
||||
logging.error(f"[VC_EXT] Invalid bitrate while playing track '{track.title}'")
|
||||
if not isinstance(ctx, RawReactionActionEvent):
|
||||
await ctx.respond(f"❌ У трека отсутствует необходимый битрейт. Его проигрывание невозможно.", delete_after=15, ephemeral=True)
|
||||
elif self.bot:
|
||||
channel = cast(discord.VoiceChannel, self.bot.get_channel(ctx.channel_id))
|
||||
await channel.send(f"❌ У трека отсутствует необходимый битрейт. Его проигрывание невозможно.", delete_after=15)
|
||||
await self.send_response_message(ctx, f"❌ У трека отсутствует необходимый битрейт. Его проигрывание невозможно.", delete_after=15, ephemeral=True)
|
||||
return None
|
||||
|
||||
logging.info(f"[VC_EXT] Playing track '{track.title}'")
|
||||
@@ -1085,27 +970,3 @@ class VoiceExtension:
|
||||
await self.send_vibe_feedback(ctx, 'trackStarted', track)
|
||||
|
||||
return track.title
|
||||
|
||||
def _get_current_event_loop(self, ctx: ApplicationContext | Interaction | RawReactionActionEvent) -> asyncio.AbstractEventLoop:
|
||||
"""Get the current event loop. If the context is a RawReactionActionEvent, get the loop from the self.bot instance.
|
||||
|
||||
Args:
|
||||
ctx (ApplicationContext | Interaction | RawReactionActionEvent): Context.
|
||||
|
||||
Raises:
|
||||
TypeError: If the context is not a RawReactionActionEvent, ApplicationContext or Interaction.
|
||||
ValueError: If the context is a RawReactionActionEvent and the bot is not set.
|
||||
|
||||
Returns:
|
||||
asyncio.AbstractEventLoop: Current event loop.
|
||||
"""
|
||||
if isinstance(ctx, Interaction):
|
||||
return ctx.client.loop
|
||||
elif isinstance(ctx, ApplicationContext):
|
||||
return ctx.bot.loop
|
||||
elif isinstance(ctx, RawReactionActionEvent):
|
||||
if not self.bot:
|
||||
raise ValueError("Bot is not set.")
|
||||
return self.bot.loop
|
||||
else:
|
||||
raise TypeError(f"Invalid context type: '{type(ctx).__name__}'.")
|
||||
|
||||
Reference in New Issue
Block a user