impr: Voice checks and menu fixes.

This commit is contained in:
Lemon4ksan
2025-01-10 16:19:27 +03:00
parent bcaba81f41
commit a108799e63
4 changed files with 326 additions and 173 deletions

View File

@@ -1,6 +1,11 @@
from typing import cast
import discord
from discord.ext.commands import Cog
from yandex_music import Track, ClientAsync
from MusicBot.cogs.utils.find import process_track
from MusicBot.cogs.utils.voice import VoiceExtension
from MusicBot.cogs.utils.player import Player
@@ -9,66 +14,61 @@ def setup(bot: discord.Bot):
class Voice(Cog, VoiceExtension):
toggle = discord.SlashCommandGroup("toggle", "Команды, связанные с переключением опций.", [1247100229535141899])
voice = discord.SlashCommandGroup("voice", "Команды, связанные с голосовым каналом.", [1247100229535141899])
queue = discord.SlashCommandGroup("queue", "Команды, связанные с очередью треков.", [1247100229535141899])
track = discord.SlashCommandGroup("track", "Команды, связанные с текущим треком.", [1247100229535141899])
async def voice_check(self, ctx: discord.ApplicationContext) -> bool:
"""Check if bot can perform voice tasks and respond if failed.
Args:
ctx (discord.ApplicationContext): Command context.
Returns:
bool: Check result.
"""
channel = ctx.channel
if not isinstance(channel, discord.VoiceChannel):
await ctx.respond("Вы должны отправить команду в голосовом канале.", delete_after=15, ephemeral=True)
return False
channels = ctx.bot.voice_clients
voice_chat = discord.utils.get(channels, guild=ctx.guild)
if not voice_chat:
await ctx.respond("Добавьте бота в голосовой канал при помощи команды /voice join.", delete_after=15, ephemeral=True)
return False
return True
@toggle.command(name="menu", description="Toggle player menu. Available only if you're the only one in the vocie channel.")
@voice.command(name="menu", description="Toggle player menu. Available only if you're the only one in the vocie channel.")
async def menu(self, ctx: discord.ApplicationContext) -> None:
if self.voice_check:
await ctx.respond("Меню", view=Player(ctx), ephemeral=True)
if not await self.voice_check(ctx):
return
current_track = self.db.get_track(ctx.guild.id, 'current')
try:
embed = await process_track(Track.de_json(current_track, client=ClientAsync())) # type: ignore
vc = self.get_voice_client(ctx)
if not vc:
return
if not vc.is_paused():
embed.set_footer(text='Приостановлено')
else:
embed.remove_footer()
except AttributeError:
embed = None
interaction = cast(discord.Interaction, await ctx.respond(view=Player(ctx), embed=embed, delete_after=3600))
response = await interaction.original_response()
self.db.update(ctx.guild.id, {'current_player': response.id})
@voice.command(name="join", description="Join the voice channel you're currently in.")
async def join(self, ctx: discord.ApplicationContext) -> None:
vc = self.get_voice_client(ctx)
if vc is not None and vc.is_playing():
await ctx.respond("Бот уже находится в голосовом канале. Выключите его с помощью команды /voice leave.", delete_after=15, ephemeral=True)
await ctx.respond("Бот уже находится в голосовом канале. Выключите его с помощью команды /voice leave.", delete_after=15, ephemeral=True)
elif ctx.channel is not None and isinstance(ctx.channel, discord.VoiceChannel):
await ctx.channel.connect(timeout=15)
await ctx.respond("Подключение успешно!", delete_after=15, ephemeral=True)
else:
await ctx.respond("Вы должны отправить команду в голосовом канале.", delete_after=15, ephemeral=True)
await ctx.respond("Вы должны отправить команду в голосовом канале.", delete_after=15, ephemeral=True)
@voice.command(description="Force the bot to leave the voice channel.")
async def leave(self, ctx: discord.ApplicationContext) -> None:
vc = self.get_voice_client(ctx)
if await self.voice_check(ctx) and vc is not None:
self.stop_playing(ctx)
self.db.clear_history(ctx.guild.id)
await vc.disconnect(force=True)
await ctx.respond("Отключение успешно!", delete_after=15, ephemeral=True)
@queue.command(description="Clear tracks queue.")
@queue.command(description="Clear tracks queue and history.")
async def clear(self, ctx: discord.ApplicationContext) -> None:
self.clear_queue(ctx)
await ctx.respond("Очередь сброшена.", delete_after=15, ephemeral=True)
if not await self.voice_check(ctx):
return
self.db.clear_history(ctx.guild.id)
await ctx.respond("Очередь и история сброшены.", delete_after=15, ephemeral=True)
@queue.command(description="Get tracks queue.")
async def get(self, ctx: discord.ApplicationContext) -> None:
if await self.voice_check(ctx):
guild = self.db.get_guild(ctx.guild.id)
tracks_list = guild.get('tracks_list')
tracks_list = self.db.get_tracks_list(ctx.guild.id, 'next')
embed = discord.Embed(
title='Список треков',
color=discord.Color.dark_purple()
@@ -99,10 +99,10 @@ class Voice(Cog, VoiceExtension):
else:
await ctx.respond("Воспроизведение не на паузе.", delete_after=15, ephemeral=True)
@track.command(description="Stop the current track and clear the queue.")
@track.command(description="Stop the current track and clear the queue and history.")
async def stop(self, ctx: discord.ApplicationContext) -> None:
if await self.voice_check(ctx):
self.clear_queue(ctx)
self.db.clear_history(ctx.guild.id)
self.stop_playing(ctx)
await ctx.respond("Воспроизведение остановлено.", delete_after=15, ephemeral=True)
@@ -110,7 +110,7 @@ class Voice(Cog, VoiceExtension):
async def next(self, ctx: discord.ApplicationContext) -> None:
if await self.voice_check(ctx):
gid = ctx.guild.id
tracks_list = self.db.get_tracks_list(gid)
tracks_list = self.db.get_tracks_list(gid, 'next')
if not tracks_list:
await ctx.respond("Нет песенен в очереди.", delete_after=15, ephemeral=True)
return