feat: zmigrowano panel webowy na WebSockets w czasie rzeczywistym

This commit is contained in:
mmacierzanka committed 2026-05-23 15:10:19 +02:00
1 parent 3eee27edfd
commit a8b2c89735
4 files changed
+169 -10

No files matched your search

+21
View File
@@ -20,6 +20,7 @@ class MusicBot(commands.Cog):
self.volumes = {} # guild_id -> volume float
self.loop_currents = {} # guild_id -> bool
self.last_channels = {} # guild_id -> text channel
self.on_state_change_callback = None
def get_queue(self, guild_id: int):
if guild_id not in self.queues:
@@ -44,6 +45,16 @@ class MusicBot(commands.Cog):
def set_loop_current(self, guild_id: int, loop: bool):
self.loop_currents[guild_id] = loop
async def trigger_state_change(self):
if self.on_state_change_callback:
try:
if asyncio.iscoroutinefunction(self.on_state_change_callback):
await self.on_state_change_callback()
else:
self.on_state_change_callback()
except Exception as e:
print(f"Error executing on_state_change_callback: {e}")
def load_saved_channel_id(self, guild_id: int):
config_path = "config/config.json"
if os.path.exists(config_path):
@@ -128,12 +139,14 @@ class MusicBot(commands.Cog):
if interaction.guild.voice_client.is_playing():
self.get_queue(guild_id).append(player)
await self.trigger_state_change()
await interaction.followup.send(
f"Dodano do kolejki: {player.title}",
ephemeral=False
)
else:
self.set_current(guild_id, player)
await self.trigger_state_change()
interaction.guild.voice_client.play(
player,
after=lambda e: asyncio.run_coroutine_threadsafe(
@@ -165,6 +178,7 @@ class MusicBot(commands.Cog):
url = current.data.get('webpage_url') or current.url
player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True)
self.set_current(guild_id, player)
await self.trigger_state_change()
voice_client.play(
player,
after=lambda e: asyncio.run_coroutine_threadsafe(
@@ -181,6 +195,7 @@ class MusicBot(commands.Cog):
if len(queue) > 0:
next_player = queue.pop(0)
self.set_current(guild_id, next_player)
await self.trigger_state_change()
voice_client.play(
next_player,
after=lambda e: asyncio.run_coroutine_threadsafe(
@@ -191,6 +206,7 @@ class MusicBot(commands.Cog):
await self.send_to_last_channel(guild_id, f"Teraz odtwarzane: {next_player.title}")
else:
self.set_current(guild_id, None)
await self.trigger_state_change()
await self.send_to_last_channel(guild_id, "Kolejka jest pusta.")
async def send_to_last_channel(self, guild_id: int, text: str):
@@ -224,6 +240,7 @@ class MusicBot(commands.Cog):
"""Pomiń aktualny utwór"""
if interaction.guild.voice_client.is_playing():
interaction.guild.voice_client.stop()
await self.trigger_state_change()
await interaction.response.send_message(
"Pominięto aktualny utwór.",
ephemeral=False
@@ -239,6 +256,7 @@ class MusicBot(commands.Cog):
"""Wstrzymaj odtwarzanie"""
if interaction.guild.voice_client.is_playing():
interaction.guild.voice_client.pause()
await self.trigger_state_change()
await interaction.response.send_message(
"Odtwarzanie wstrzymane.",
ephemeral=False
@@ -254,6 +272,7 @@ class MusicBot(commands.Cog):
"""Wznów odtwarzanie"""
if interaction.guild.voice_client.is_paused():
interaction.guild.voice_client.resume()
await self.trigger_state_change()
await interaction.response.send_message(
"Odtwarzanie wznowione.",
ephemeral=False
@@ -272,6 +291,7 @@ class MusicBot(commands.Cog):
interaction.guild.voice_client.stop()
self.get_queue(guild_id).clear()
self.set_current(guild_id, None)
await self.trigger_state_change()
await interaction.response.send_message(
"Zatrzymano odtwarzanie i wyczyszczono kolejkę.",
ephemeral=False
@@ -291,6 +311,7 @@ class MusicBot(commands.Cog):
if 0 < volume <= 100:
self.set_volume(guild_id, volume / 100)
interaction.guild.voice_client.source.volume = self.get_volume(guild_id)
await self.trigger_state_change()
await interaction.response.send_message(
f"Zmieniono głośność na {volume}%",
ephemeral=False