feat: dodanie możliwości przesyłania własnych plików na serwer (wszystkie formaty ffmpeg), odtwarzanie jednym kliknięciem z web i komenda /playfile w Discord

This commit is contained in:
mmacierzanka committed 2026-07-04 15:33:43 +02:00
1 parent 444b797cd0
commit dfd681be04
6 files changed
+395 -5

No files matched your search

+69 -3
View File
@@ -135,7 +135,70 @@ class MusicBot(commands.Cog):
self.last_channels[guild_id] = interaction.channel
try:
player = await YTDLSource.from_url(query, loop=self.bot.loop, stream=True)
filepath = os.path.join("uploads", os.path.basename(query))
if os.path.exists(filepath) and not query.startswith("http"):
player = await YTDLSource.from_file(filepath, loop=self.bot.loop)
else:
player = await YTDLSource.from_url(query, loop=self.bot.loop, stream=True)
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: {discord.utils.escape_markdown(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(
self.play_next(guild_id),
self.bot.loop
)
)
await interaction.followup.send(
f"Teraz odtwarzane: {discord.utils.escape_markdown(player.title)}",
ephemeral=False
)
except Exception as e:
await interaction.followup.send(
f"Wystąpił błąd: {e}",
ephemeral=True
)
async def uploads_autocomplete(
self,
interaction: discord.Interaction,
current: str,
) -> list[app_commands.Choice[str]]:
uploads_dir = "uploads"
if not os.path.exists(uploads_dir):
return []
files = [f for f in os.listdir(uploads_dir) if os.path.isfile(os.path.join(uploads_dir, f)) and not f.startswith(".")]
filtered = [f for f in files if current.lower() in f.lower()][:25]
return [app_commands.Choice(name=f, value=f) for f in filtered]
@app_commands.command(name="playfile", description="Odtwórz plik przesłany na serwer")
@app_commands.describe(filename="Wybierz plik z listy przesłanych")
@app_commands.autocomplete(filename=uploads_autocomplete)
async def playfile(self, interaction: discord.Interaction, filename: str):
"""Odtwórz plik przesłany na serwer"""
if not await self.ensure_voice(interaction):
return
await interaction.response.defer()
guild_id = interaction.guild.id
self.last_channels[guild_id] = interaction.channel
filepath = os.path.join("uploads", os.path.basename(filename))
if not os.path.exists(filepath):
await interaction.followup.send("Nie znaleziono pliku na serwerze.", ephemeral=True)
return
try:
player = await YTDLSource.from_file(filepath, loop=self.bot.loop)
if interaction.guild.voice_client.is_playing():
self.get_queue(guild_id).append(player)
@@ -175,8 +238,11 @@ class MusicBot(commands.Cog):
if loop_current and current:
try:
url = current.data.get('webpage_url') or current.url
player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True)
if current.data.get('is_local'):
player = await YTDLSource.from_file(current.data['filepath'], loop=self.bot.loop, title=current.title)
else:
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(