mirror of
https://github.com/deadcxap/YandexMusicDiscordBot.git
synced 2026-01-09 22:51:46 +03:00
feat: Add find autocomplete.
This commit is contained in:
@@ -18,9 +18,55 @@ from MusicBot.cogs.utils.embeds import generate_item_embed
|
||||
def setup(bot):
|
||||
bot.add_cog(General(bot))
|
||||
|
||||
async def get_search_suggestions(ctx: discord.AutocompleteContext) -> list[str]:
|
||||
if not ctx.interaction.user or not ctx.value:
|
||||
return []
|
||||
|
||||
users_db = BaseUsersDatabase()
|
||||
token = users_db.get_ym_token(ctx.interaction.user.id)
|
||||
if not token:
|
||||
return ['❌ Укажите токен через /account login.']
|
||||
|
||||
try:
|
||||
client = await YMClient(token).init()
|
||||
except yandex_music.exceptions.UnauthorizedError:
|
||||
logging.info(f"User {ctx.interaction.user.id} provided invalid token")
|
||||
return ['❌ Недействительный токен.']
|
||||
|
||||
content_type = ctx.options['тип']
|
||||
search = await client.search(ctx.value)
|
||||
if not search:
|
||||
logging.warning(f"Failed to search for '{ctx.value}' for user {ctx.interaction.user.id}")
|
||||
return ["❌ Что-то пошло не так. Повторите попытку позже"]
|
||||
|
||||
res = []
|
||||
logging.debug(f"Searching for '{ctx.value}' for user {ctx.interaction.user.id}")
|
||||
|
||||
if content_type == 'Трек' and search.tracks:
|
||||
for item in search.tracks.results:
|
||||
res.append(f"{item.title} {f"({item.version})" if item.version else ''} - {", ".join(item.artists_name())}")
|
||||
elif content_type == 'Альбом' and search.albums:
|
||||
for item in search.albums.results:
|
||||
res.append(f"{item.title} - {", ".join(item.artists_name())}")
|
||||
elif content_type == 'Артист' and search.artists:
|
||||
for item in search.artists.results:
|
||||
res.append(f"{item.name}")
|
||||
elif content_type == 'Плейлист' and search.playlists:
|
||||
for item in search.playlists.results:
|
||||
res.append(f"{item.title}")
|
||||
elif content_type == "Свой плейлист":
|
||||
if not client.me or not client.me.account or not client.me.account.uid:
|
||||
logging.warning(f"Failed to get playlists for user {ctx.interaction.user.id}")
|
||||
return ["❌ Что-то пошло не так. Повторите попытку позже"]
|
||||
|
||||
playlists_list = await client.users_playlists_list(client.me.account.uid)
|
||||
res = [playlist.title if playlist.title else 'Без названия' for playlist in playlists_list]
|
||||
|
||||
return res
|
||||
|
||||
class General(Cog):
|
||||
|
||||
def __init__(self, bot):
|
||||
def __init__(self, bot: discord.Bot):
|
||||
self.bot = bot
|
||||
self.db = BaseGuildsDatabase()
|
||||
self.users_db = BaseUsersDatabase()
|
||||
@@ -36,6 +82,7 @@ class General(Cog):
|
||||
)
|
||||
async def help(self, ctx: discord.ApplicationContext, command: str) -> None:
|
||||
logging.info(f"Help command invoked by {ctx.user.id} for command '{command}'")
|
||||
|
||||
response_message = None
|
||||
embed = discord.Embed(
|
||||
title='Помощь',
|
||||
@@ -45,60 +92,76 @@ class General(Cog):
|
||||
embed.description = '__Использование__\n'
|
||||
|
||||
if command == 'all':
|
||||
embed.description = ("Данный бот позволяет вам слушать музыку из вашего аккаунта Yandex Music.\n"
|
||||
"Зарегистрируйте свой токен с помощью /login. Его можно получить [здесь](https://github.com/MarshalX/yandex-music-api/discussions/513).\n"
|
||||
"Для получения помощи для конкретной команды, введите /help <команда>.\n\n"
|
||||
"**Для доп. помощи, зайдите на [сервер любителей Яндекс Музыки](https://discord.gg/gkmFDaPMeC).**")
|
||||
embed.description = (
|
||||
"Этот бот позволяет слушать музыку из вашего аккаунта Yandex Music.\n"
|
||||
"Зарегистрируйте свой токен с помощью /login. Его можно получить [здесь](https://github.com/MarshalX/yandex-music-api/discussions/513).\n"
|
||||
"Для получения помощи по конкретной команде, введите /help <команда>.\n\n"
|
||||
"**Для дополнительной помощи, присоединяйтесь к [серверу любителей Яндекс Музыки](https://discord.gg/gkmFDaPMeC).**"
|
||||
)
|
||||
|
||||
embed.add_field(
|
||||
name='__Основные команды__',
|
||||
value="""
|
||||
`account`
|
||||
value="""`account`
|
||||
`find`
|
||||
`help`
|
||||
`like`
|
||||
`queue`
|
||||
`settings`
|
||||
`track`
|
||||
`voice`
|
||||
"""
|
||||
`voice`"""
|
||||
)
|
||||
|
||||
embed.set_footer(text='©️ Bananchiki')
|
||||
elif command == 'account':
|
||||
embed.description += ("Ввести токен от Яндекс Музыки. Его можно получить [здесь](https://github.com/MarshalX/yandex-music-api/discussions/513).\n"
|
||||
"```/account login <token>```\n"
|
||||
"Удалить токен из датабазы бота.\n```/account remove```\n"
|
||||
"Получить ваши плейлисты. Чтобы добавить плейлист в очередь, используйте команду /find.\n```/account playlists```\n"
|
||||
"Получить плейлист «Мне нравится». \n```/account likes```\n")
|
||||
embed.description += (
|
||||
"Ввести токен от Яндекс Музыки. Его можно получить [здесь](https://github.com/MarshalX/yandex-music-api/discussions/513).\n"
|
||||
"```/account login <token>```\n"
|
||||
"Удалить токен из базы данных бота.\n```/account remove```\n"
|
||||
"Получить ваши плейлисты. Чтобы добавить плейлист в очередь, используйте команду /find.\n```/account playlists```\n"
|
||||
"Получить плейлист «Мне нравится».\n```/account likes```\n"
|
||||
)
|
||||
elif command == 'find':
|
||||
embed.description += ("Вывести информацию о треке (по умолчанию), альбоме, авторе или плейлисте. Позволяет добавить музыку в очередь. "
|
||||
"В названии можно уточнить автора или версию. Возвращается лучшее совпадение.\n```/find <название> <тип>```")
|
||||
embed.description += (
|
||||
"Вывести информацию о треке (по умолчанию), альбоме, авторе или плейлисте. Позволяет добавить музыку в очередь. "
|
||||
"В названии можно уточнить автора или версию. Возвращается лучшее совпадение.\n```/find <название> <тип>```"
|
||||
)
|
||||
elif command == 'help':
|
||||
embed.description += ("Вывести список всех команд.\n```/help```\n"
|
||||
"Получить информацию о конкретной команде.\n```/help <команда>```")
|
||||
embed.description += (
|
||||
"Вывести список всех команд.\n```/help```\n"
|
||||
"Получить информацию о конкретной команде.\n```/help <команда>```"
|
||||
)
|
||||
elif command == 'like':
|
||||
embed.description += "Добавить трек в плейлист «Мне нравится». Пользовательские треки из этого плейлиста игнорируются.\n```/like```"
|
||||
embed.description += (
|
||||
"Добавить трек в плейлист «Мне нравится». Пользовательские треки из этого плейлиста игнорируются.\n```/like```"
|
||||
)
|
||||
elif command == 'queue':
|
||||
embed.description += ("Получить очередь треков. По 15 элементов на страницу.\n```/queue get```\n"
|
||||
"Очистить очередь треков и историю прослушивания. Доступно только если вы единственный в голосовом канале"
|
||||
"или имеете разрешение управления каналом.\n```/queue clear```\n")
|
||||
embed.description += (
|
||||
"Получить очередь треков. По 15 элементов на страницу.\n```/queue get```\n"
|
||||
"Очистить очередь треков и историю прослушивания. Доступно только если вы единственный в голосовом канале "
|
||||
"или имеете разрешение управления каналом.\n```/queue clear```\n"
|
||||
)
|
||||
elif command == 'settings':
|
||||
embed.description += ("Получить текущие настройки.\n```/settings show```\n"
|
||||
"Разрешить или запретить воспроизведение Explicit треков и альбомов. Если автор или плейлист содержат Explicit треки, убираются кнопки для доступа к ним.\n```/settings explicit```\n"
|
||||
"Разрешить или запретить создание меню проигрывателя, когда в канале больше одного человека.\n```/settings menu```\n"
|
||||
"Разрешить или запретить голосование.\n```/settings vote <тип голосования>```\n"
|
||||
"`Примечание`: Только пользователи с разрешением управления каналом могут менять настройки.")
|
||||
embed.description += (
|
||||
"Получить текущие настройки.\n```/settings show```\n"
|
||||
"Разрешить или запретить воспроизведение Explicit треков и альбомов. Если автор или плейлист содержат Explicit треки, убираются кнопки для доступа к ним.\n```/settings explicit```\n"
|
||||
"Разрешить или запретить создание меню проигрывателя, когда в канале больше одного человека.\n```/settings menu```\n"
|
||||
"Разрешить или запретить голосование.\n```/settings vote <тип голосования>```\n"
|
||||
"`Примечание`: Только пользователи с разрешением управления каналом могут менять настройки."
|
||||
)
|
||||
elif command == 'track':
|
||||
embed.description += ("`Примечание`: Если вы один в голосовом канале или имеете разрешение управления каналом, голосование не начинается.\n\n"
|
||||
"Переключиться на следующий трек в очереди. \n```/track next```\n"
|
||||
"Приостановить текущий трек.\n ```/track pause```\n"
|
||||
"Возобновить текущий трек.\n ```/track resume```\n"
|
||||
"Прервать проигрывание, удалить историю, очередь и текущий плеер.\n ```/track stop```")
|
||||
embed.description += (
|
||||
"`Примечание`: Если вы один в голосовом канале или имеете разрешение управления каналом, голосование не начинается.\n\n"
|
||||
"Переключиться на следующий трек в очереди. \n```/track next```\n"
|
||||
"Приостановить текущий трек.\n ```/track pause```\n"
|
||||
"Возобновить текущий трек.\n ```/track resume```\n"
|
||||
"Прервать проигрывание, удалить историю, очередь и текущий плеер.\n ```/track stop```"
|
||||
)
|
||||
elif command == 'voice':
|
||||
embed.description += ("Присоединить бота в голосовой канал. Требует разрешения управления каналом.\n ```/voice join```\n"
|
||||
"Заставить бота покинуть голосовой канал. Требует разрешения управления каналом.\n ```/voice leave```\n"
|
||||
"Создать меню проигрывателя. Доступность зависит от настроек сервера. По умолчанию работает только когда в канале один человек.\n```/voice menu```")
|
||||
embed.description += (
|
||||
"Присоединить бота в голосовой канал. Требует разрешения управления каналом.\n ```/voice join```\n"
|
||||
"Заставить бота покинуть голосовой канал. Требует разрешения управления каналом.\n ```/voice leave```\n"
|
||||
"Создать меню проигрывателя. Доступность зависит от настроек сервера. По умолчанию работает только когда в канале один человек.\n```/voice menu```"
|
||||
)
|
||||
else:
|
||||
response_message = '❌ Неизвестная команда.'
|
||||
embed = None
|
||||
@@ -160,43 +223,52 @@ class General(Cog):
|
||||
@account.command(description="Получить ваши плейлисты.")
|
||||
async def playlists(self, ctx: discord.ApplicationContext) -> None:
|
||||
logging.info(f"Playlists command invoked by user {ctx.user.id} in guild {ctx.guild_id}")
|
||||
|
||||
token = self.users_db.get_ym_token(ctx.user.id)
|
||||
if not token:
|
||||
await ctx.respond('❌ Необходимо указать свой токен доступа с помощью команды /login.', delete_after=15, ephemeral=True)
|
||||
return
|
||||
|
||||
client = await YMClient(token).init()
|
||||
if not client.me or not client.me.account or not client.me.account.uid:
|
||||
await ctx.respond('❌ Что-то пошло не так. Повторите попытку позже.', delete_after=15, ephemeral=True)
|
||||
return
|
||||
|
||||
playlists_list = await client.users_playlists_list(client.me.account.uid)
|
||||
playlists: list[tuple[str, int]] = [
|
||||
(playlist.title if playlist.title else 'Без названия', playlist.track_count if playlist.track_count else 0) for playlist in playlists_list
|
||||
]
|
||||
|
||||
self.users_db.update(ctx.user.id, {'playlists': playlists, 'playlists_page': 0})
|
||||
embed = generate_playlists_embed(0, playlists)
|
||||
|
||||
logging.info(f"Successfully fetched playlists for user {ctx.user.id}")
|
||||
await ctx.respond(embed=embed, view=MyPlaylists(ctx), ephemeral=True)
|
||||
|
||||
|
||||
discord.Option
|
||||
@discord.slash_command(description="Найти контент и отправить информацию о нём. Возвращается лучшее совпадение.")
|
||||
@discord.option(
|
||||
"name",
|
||||
description="Название контента для поиска (По умолчанию трек).",
|
||||
type=discord.SlashCommandOptionType.string
|
||||
)
|
||||
@discord.option(
|
||||
"content_type",
|
||||
description="Тип искомого контента.",
|
||||
"тип",
|
||||
parameter_name='content_type',
|
||||
description="Тип контента для поиска.",
|
||||
type=discord.SlashCommandOptionType.string,
|
||||
choices=['Трек', 'Альбом', 'Артист', 'Плейлист', 'Свой плейлист'],
|
||||
default='Трек'
|
||||
)
|
||||
@discord.option(
|
||||
"запрос",
|
||||
parameter_name='name',
|
||||
description="Название контента для поиска (По умолчанию трек).",
|
||||
type=discord.SlashCommandOptionType.string,
|
||||
autocomplete=discord.utils.basic_autocomplete(get_search_suggestions)
|
||||
)
|
||||
async def find(
|
||||
self,
|
||||
ctx: discord.ApplicationContext,
|
||||
name: str,
|
||||
content_type: Literal['Трек', 'Альбом', 'Артист', 'Плейлист', 'Свой плейлист'] = 'Трек'
|
||||
content_type: Literal['Трек', 'Альбом', 'Артист', 'Плейлист', 'Свой плейлист'],
|
||||
name: str
|
||||
) -> None:
|
||||
logging.info(f"Find command invoked by user {ctx.user.id} in guild {ctx.guild_id} for '{content_type}' with name '{name}'")
|
||||
|
||||
guild = self.db.get_guild(ctx.guild_id)
|
||||
token = self.users_db.get_ym_token(ctx.user.id)
|
||||
if not token:
|
||||
|
||||
@@ -27,6 +27,7 @@ class VoiceExtension:
|
||||
Returns:
|
||||
bool: True if updated, False if not.
|
||||
"""
|
||||
from MusicBot.ui import MenuView
|
||||
logging.debug(
|
||||
f"Updating player embed using " +
|
||||
"interaction context" if isinstance(ctx, Interaction) else
|
||||
@@ -41,7 +42,7 @@ class VoiceExtension:
|
||||
logging.warning("Guild ID or User ID not found in context inside 'update_player_embed'")
|
||||
return False
|
||||
|
||||
player = await self.get_player_message(ctx, player_mid)
|
||||
player = await self.get_menu_message(ctx, player_mid)
|
||||
if not player:
|
||||
return False
|
||||
|
||||
@@ -63,14 +64,14 @@ class VoiceExtension:
|
||||
|
||||
if isinstance(ctx, Interaction) and ctx.message and ctx.message.id == player_mid:
|
||||
# If interaction from player buttons
|
||||
await ctx.edit(embed=embed)
|
||||
await ctx.edit(embed=embed, view=await MenuView(ctx).init())
|
||||
else:
|
||||
# If interaction from other buttons or commands. They should have their own response.
|
||||
await player.edit(embed=embed)
|
||||
await player.edit(embed=embed, view=await MenuView(ctx).init())
|
||||
|
||||
return True
|
||||
|
||||
async def get_player_message(self, ctx: ApplicationContext | Interaction | RawReactionActionEvent, player_mid: int) -> discord.Message | None:
|
||||
async def get_menu_message(self, ctx: ApplicationContext | Interaction | RawReactionActionEvent, player_mid: int) -> discord.Message | None:
|
||||
"""Fetch the player message by its id. Return the message if found, None if not.
|
||||
Reset `current_player` field in the database if not found.
|
||||
|
||||
@@ -369,7 +370,7 @@ class VoiceExtension:
|
||||
|
||||
return None
|
||||
|
||||
async def get_likes(self, ctx: ApplicationContext | Interaction) -> list[TrackShort] | 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.
|
||||
|
||||
@@ -380,12 +381,14 @@ class VoiceExtension:
|
||||
list[Track] | None: List of tracks or None.
|
||||
"""
|
||||
|
||||
if not ctx.guild or not ctx.user:
|
||||
logging.warning("Guild or User not found in context inside 'like_track'")
|
||||
gid = ctx.guild_id if isinstance(ctx, discord.RawReactionActionEvent) else ctx.guild.id if ctx.guild else None
|
||||
uid = ctx.user_id if isinstance(ctx, discord.RawReactionActionEvent) else ctx.user.id if ctx.user else None
|
||||
if not gid or not uid:
|
||||
logging.warning("Guild ID or User ID not found in context inside 'play_track'")
|
||||
return None
|
||||
|
||||
current_track = self.db.get_track(ctx.guild.id, 'current')
|
||||
token = self.users_db.get_ym_token(ctx.user.id)
|
||||
current_track = self.db.get_track(gid, 'current')
|
||||
token = self.users_db.get_ym_token(uid)
|
||||
if not current_track or not token:
|
||||
logging.debug("Current track or token not found")
|
||||
return None
|
||||
|
||||
@@ -59,7 +59,7 @@ class Voice(Cog, VoiceExtension):
|
||||
logging.info(f"Reaction added by user {payload.user_id} in channel {payload.channel_id}")
|
||||
if not self.typed_bot.user or not payload.member:
|
||||
return
|
||||
|
||||
|
||||
bot_id = self.typed_bot.user.id
|
||||
if payload.user_id == bot_id:
|
||||
return
|
||||
@@ -76,13 +76,17 @@ class Voice(Cog, VoiceExtension):
|
||||
await message.remove_reaction(payload.emoji, payload.member)
|
||||
await channel.send("Для участия в голосовании необходимо авторизоваться через /account login.", delete_after=15)
|
||||
return
|
||||
|
||||
|
||||
guild_id = payload.guild_id
|
||||
if not guild_id:
|
||||
return
|
||||
guild = self.db.get_guild(guild_id)
|
||||
votes = guild['votes']
|
||||
|
||||
if payload.message_id not in votes:
|
||||
logging.info(f"Message {payload.message_id} not found in votes")
|
||||
return
|
||||
|
||||
vote_data = votes[str(payload.message_id)]
|
||||
if payload.emoji.name == '✅':
|
||||
logging.info(f"User {payload.user_id} voted positively for message {payload.message_id}")
|
||||
@@ -214,7 +218,7 @@ class Voice(Cog, VoiceExtension):
|
||||
|
||||
if guild['current_player']:
|
||||
logging.info(f"Deleteing old player menu {guild['current_player']} in guild {ctx.guild.id}")
|
||||
message = await self.get_player_message(ctx, guild['current_player'])
|
||||
message = await self.get_menu_message(ctx, guild['current_player'])
|
||||
if message:
|
||||
await message.delete()
|
||||
|
||||
@@ -354,7 +358,7 @@ class Voice(Cog, VoiceExtension):
|
||||
|
||||
current_player = self.db.get_current_player(ctx.guild.id)
|
||||
if current_player:
|
||||
player = await self.get_player_message(ctx, current_player)
|
||||
player = await self.get_menu_message(ctx, current_player)
|
||||
if player:
|
||||
await player.delete()
|
||||
|
||||
|
||||
@@ -121,7 +121,7 @@ class PlayButton(Button, VoiceExtension):
|
||||
|
||||
current_player = None
|
||||
if guild['current_player']:
|
||||
current_player = await self.get_player_message(interaction, guild['current_player'])
|
||||
current_player = await self.get_menu_message(interaction, guild['current_player'])
|
||||
|
||||
if current_player and interaction.message:
|
||||
logging.debug(f"Deleting interaction message {interaction.message.id}: current player {current_player.id} found")
|
||||
@@ -130,7 +130,7 @@ class PlayButton(Button, VoiceExtension):
|
||||
await interaction.respond(response_message, delete_after=15)
|
||||
|
||||
class ListenView(View):
|
||||
def __init__(self, item: Track | Album | Artist | Playlist | list[Track], *items: Item, timeout: float | None = None, disable_on_timeout: bool = False):
|
||||
def __init__(self, item: Track | Album | Artist | Playlist | list[Track], *items: Item, timeout: float | None = 3600, disable_on_timeout: bool = False):
|
||||
super().__init__(*items, timeout=timeout, disable_on_timeout=disable_on_timeout)
|
||||
logging.debug(f"Creating view for type: '{type(item).__name__}'")
|
||||
|
||||
|
||||
@@ -140,7 +140,7 @@ class LyricsButton(Button, VoiceExtension):
|
||||
|
||||
class MenuView(View, VoiceExtension):
|
||||
|
||||
def __init__(self, ctx: ApplicationContext | Interaction, *items: Item, timeout: float | None = 3600, disable_on_timeout: bool = True):
|
||||
def __init__(self, ctx: ApplicationContext | Interaction | RawReactionActionEvent, *items: Item, timeout: float | None = 3600, disable_on_timeout: bool = False):
|
||||
View.__init__(self, *items, timeout=timeout, disable_on_timeout=disable_on_timeout)
|
||||
VoiceExtension.__init__(self, None)
|
||||
if not ctx.guild_id:
|
||||
@@ -167,7 +167,7 @@ class MenuView(View, VoiceExtension):
|
||||
self.add_item(self.next_button)
|
||||
self.add_item(self.shuffle_button)
|
||||
|
||||
if len(cast(VoiceChannel, self.ctx.channel).members) > 2:
|
||||
if isinstance(self.ctx, RawReactionActionEvent) or len(cast(VoiceChannel, self.ctx.channel).members) > 2:
|
||||
self.like_button.disabled = True
|
||||
elif likes and current_track and str(current_track['id']) in [str(like.id) for like in likes]:
|
||||
self.like_button.style = ButtonStyle.success
|
||||
@@ -178,4 +178,18 @@ class MenuView(View, VoiceExtension):
|
||||
self.add_item(self.like_button)
|
||||
self.add_item(self.lyrics_button)
|
||||
|
||||
return self
|
||||
return self
|
||||
|
||||
async def on_timeout(self) -> None:
|
||||
logging.debug('Menu timed out...')
|
||||
if not self.ctx.guild_id:
|
||||
return
|
||||
|
||||
if self.guild['current_player']:
|
||||
self.db.update(self.ctx.guild_id, {'current_player': None, 'previous_tracks': []})
|
||||
message = await self.get_menu_message(self.ctx, self.guild['current_player'])
|
||||
if message:
|
||||
await message.delete()
|
||||
logging.debug('Successfully deleted menu message')
|
||||
else:
|
||||
logging.debug('No menu message found')
|
||||
Reference in New Issue
Block a user