Add tracks from a direct link and from Discord with /play, /skip, /queue

- Download direct audio links server side (web page and /play), refusing
  internal and special addresses at connection time and after every
  redirect, with size and time limits.
- /play takes an attachment, a direct link or a library track
  (autocomplete), and can keep the file in the library; /skip and /queue
  control and show the shared queue.
- Share one ingest path between uploads, links and Discord attachments.
- Answer 413 instead of 500 when the library is full.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Shinuwa 2026-11-29 12:13:12 +01:00
parent 98f4d29ac7
commit 39d2b6acfb
14 changed files with 715 additions and 54 deletions

View file

@ -0,0 +1,126 @@
import { MessageFlags, SlashCommandBuilder, escapeMarkdown } from "discord.js";
import { downloadAudio } from "../../media/downloadAudio.js";
import { ingestAudioFile } from "../../music/ingest.js";
import { trackLabel } from "../../features/formatTrack.js";
import {
TEMP_DIR,
createQueueTrackFromLibrary,
findPermanentTrack,
getPermanentLibrary,
} from "../../web/storage/storageManager.js";
const MAX_ATTACHMENT_SIZE = 150 * 1024 * 1024;
function positionMessage(queue, track) {
if (queue.currentTrack?.id === track.id) {
return "c'est parti ! 🎶";
}
const index = queue.queue.findIndex((queued) => queued.id === track.id);
return index >= 0 ? `position ${index + 1} dans la file.` : "ajouté à la file.";
}
const command = {
data: new SlashCommandBuilder()
.setName("play")
.setDescription("Ajoute un morceau à la file du jukebox")
.addAttachmentOption((option) =>
option.setName("fichier").setDescription("Fichier audio à jouer")
)
.addStringOption((option) =>
option
.setName("lien")
.setDescription("Lien direct vers un fichier audio (pas YouTube ni Spotify)")
.setMaxLength(2000)
)
.addStringOption((option) =>
option
.setName("morceau")
.setDescription("Morceau de la bibliothèque")
.setAutocomplete(true)
)
.addBooleanOption((option) =>
option
.setName("garder")
.setDescription("Garder le fichier ou le lien dans la bibliothèque")
),
async autocomplete(interaction) {
const query = interaction.options.getFocused().toLowerCase();
const choices = getPermanentLibrary()
.filter((entry) =>
`${entry.title || ""} ${entry.originalName || ""}`.toLowerCase().includes(query)
)
.slice(0, 25)
.map((entry) => ({ name: trackLabel(entry).slice(0, 100), value: entry.id }));
await interaction.respond(choices);
},
async execute(interaction) {
const queue = interaction.client.musicQueue;
const attachment = interaction.options.getAttachment("fichier");
const link = interaction.options.getString("lien");
const libraryId = interaction.options.getString("morceau");
const keep = interaction.options.getBoolean("garder") ?? false;
const sources = [attachment, link, libraryId].filter(Boolean);
if (sources.length !== 1) {
await interaction.reply({
content: "Choisis une seule source : `fichier`, `lien` ou `morceau`.",
flags: MessageFlags.Ephemeral,
});
return;
}
const uploader = interaction.member?.displayName || interaction.user.username;
if (libraryId) {
const entry = findPermanentTrack(libraryId);
if (!entry) {
await interaction.reply({
content: "Morceau introuvable dans la bibliothèque.",
flags: MessageFlags.Ephemeral,
});
return;
}
const track = queue.enqueue(createQueueTrackFromLibrary(entry, uploader));
await interaction.reply(
`**${escapeMarkdown(trackLabel(track))}** : ${positionMessage(queue, track)}`
);
return;
}
if (attachment && attachment.size > MAX_ATTACHMENT_SIZE) {
await interaction.reply({
content: "Fichier trop volumineux (150 Mo au plus).",
flags: MessageFlags.Ephemeral,
});
return;
}
await interaction.deferReply();
try {
const file = await downloadAudio(attachment?.url ?? link, { destinationDir: TEMP_DIR });
if (attachment?.name) {
file.originalname = attachment.name;
}
const track = await ingestAudioFile(
file,
{ permanent: keep, enqueue: true, uploader },
{ queue }
);
await interaction.editReply(
`**${escapeMarkdown(trackLabel(track))}** : ${positionMessage(queue, track)}`
);
} catch (error) {
if (!error.status) throw error;
// Erreur attendue (lien invalide, fichier non audio…) : on l'explique sans la publier.
await interaction.deleteReply().catch(() => {});
await interaction.followUp({
content: `Impossible d'ajouter ce morceau : ${error.message}`,
flags: MessageFlags.Ephemeral,
});
}
},
};
export { command };