palico-bot/src/commands/jukebox/play.js
Shinuwa 3795ee06cb Add file headers, tidy the stylesheet and document the conventions
Headers:
- Every JavaScript file (sources, front, scripts, tests) starts with a
  /** @file … */ header, in the three levels used by sokko-g: a short
  description for simple files; Utilisé par / Attention / @see for
  shared modules; a full header (context, entry points, callers,
  dependencies, pitfalls) for index.js, the music queue, the web
  server, the storage manager and the jukebox page script.
- No @author, @version or @since: git already knows.
- test/fileHeaders.test.js fails `npm run check` when a header is
  missing or a central file's header is incomplete.

Stylesheet (no visual change, checked by comparing every element's
computed styles in Chromium at 1280, 800 and 400 px, hover included):
- Role header and section comments.
- Transparent shades derive from the colour variables with
  color-mix() instead of copied rgba() values; new tokens for repeated
  values (text, text on accent, accent gradient, white veils, shadow).
- Native nesting for buttons, the live indicator, upload rows and
  home cards; drop the unused .hero__visual--large and redundant rules.
- Utility classes move to the end so they win over components. The
  library list's u-mt-1, which never applied (.list reset it), is
  removed to keep the layout as it was.

Docs: CONTRIBUTING.md describes the language, header and stylesheet
conventions; CLAUDE.md points to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 21:05:59 +01:00

131 lines
4.3 KiB
JavaScript

/**
* @file Commande /play : ajoute à la file un fichier joint, un lien direct ou un morceau de la
* bibliothèque (avec autocomplétion), en passant par le même ajout que la page web.
*/
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 };