Files
zyrarbot/zyrarbot/ui/bot/cogs/queue_cog.py
T
mmacierzanka d3a5058fde
Backend / Lint i formatowanie (pull_request) Successful in 3s
Backend / Testy i kontrakt API (pull_request) Failing after 2m19s
Backend / Testowy build obrazu (pull_request) Skipped
Formatting, README and other friends
2026-08-21 13:41:38 +02:00

100 lines
4.3 KiB
Python

from __future__ import annotations
import discord
from discord import app_commands
from discord.ext import commands
from zyrarbot.music.music_player import MusicPlayer
from zyrarbot.settings import Settings
from zyrarbot.ui.bot.cogs.common import MusicCogBase
QUEUE_PREVIEW_LIMIT = 30
HISTORY_PREVIEW_LIMIT = 15
AUTOCOMPLETE_LIMIT = 25
class QueueCog(MusicCogBase, name="Queue"):
"""Komendy podglądu kolejki i historii odtwarzania."""
def __init__(self, bot: commands.Bot, player: MusicPlayer, settings: Settings):
super().__init__(bot, player)
self._settings = settings
@app_commands.command(name="queue", description="Pokaż aktualną kolejkę")
async def show_queue(self, interaction: discord.Interaction) -> None:
state = self.player.state(interaction.guild.id)
if not state.queue:
await interaction.response.send_message("Kolejka jest pusta.", ephemeral=True)
return
lines: list[str] = []
if state.current and not 0 <= state.index < len(state.queue):
title = discord.utils.escape_markdown(state.current.title or "")
plays = self.player.plays_left(state.current)
lines.append(
f"▶️ **{title}** (obecnie odtwarzane - ostatnie odtworzenie) [🔁 {plays}]\n---"
)
for position, track in enumerate(state.queue):
is_current = position == state.index
prefix = "▶️ **" if is_current else f"{position + 1}. "
suffix = "** (obecnie odtwarzane)" if is_current else ""
title = discord.utils.escape_markdown(track.title or "")
lines.append(f"{prefix}{title}{suffix} [🔁 {self.player.plays_left(track)}]")
preview = "\n".join(lines[:QUEUE_PREVIEW_LIMIT])
if len(lines) > QUEUE_PREVIEW_LIMIT:
preview += f"\n... i {len(lines) - QUEUE_PREVIEW_LIMIT} więcej."
await interaction.response.send_message(f"Aktualna kolejka:\n{preview}")
async def _history_autocomplete(
self, interaction: discord.Interaction, current: str
) -> list[app_commands.Choice[str]]:
entries = self.player.history(interaction.guild.id)
matches = [entry for entry in entries if current.lower() in entry.title.lower()]
return [
app_commands.Choice(name=entry.title[:100], value=entry.id)
for entry in matches[:AUTOCOMPLETE_LIMIT]
]
@app_commands.command(
name="history", description="Pokaż historię ostatnio odtwarzanych utworów"
)
async def show_history(self, interaction: discord.Interaction) -> None:
entries = self.player.history(interaction.guild.id)
if not entries:
await interaction.response.send_message(
"Historia odtwarzania jest pusta.", ephemeral=True
)
return
listing = "\n".join(
f"{position + 1}. {discord.utils.escape_markdown(entry.title)}"
for position, entry in enumerate(entries[:HISTORY_PREVIEW_LIMIT])
)
await interaction.response.send_message(
f"📜 Historia odtwarzania (ostatnie {HISTORY_PREVIEW_LIMIT}):\n{listing}"
)
@app_commands.command(
name="trackplays", description="Zmień liczbę odtworzeń dla utworu w kolejce"
)
@app_commands.describe(index="Pozycja w kolejce (od 1)", count="Nowa liczba odtworzeń (1-999)")
async def track_plays_command(
self, interaction: discord.Interaction, index: int, count: int
) -> None:
queue = self.player.state(interaction.guild.id).queue
if not 1 <= index <= len(queue):
await interaction.response.send_message(
"❌ Nieprawidłowy numer utworu w kolejce.", ephemeral=True
)
return
if not self._settings.is_valid_plays(count):
await interaction.response.send_message(
"❌ Liczba odtworzeń musi być z przedziału 1-999.", ephemeral=True
)
return
track = queue[index - 1]
track.plays_left = count
await self.player.trigger_state_change()
title = discord.utils.escape_markdown(track.title or "")
await interaction.response.send_message(
f"✅ Ustawiono liczbę odtworzeń dla utworu **{title}** na **{count}**."
)