diff --git a/assets/css/style.css b/assets/css/style.css index 7fb0b77..af3e696 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -10201,6 +10201,9 @@ a.hunt-review-spot-thumb { width: 64px; height: 40px; flex-shrink: 0; } .music-connection.is-online { background: rgba(76, 175, 80, 0.15); color: #8fdc93; } .music-connection.is-offline { background: rgba(217, 83, 79, 0.15); color: #f2a19e; } .music-now { display: flex; align-items: center; gap: 1.25rem; margin: 0.8rem 0 1rem; } +.music-art { position: relative; flex: none; width: 96px; height: 96px; } +.music-cover { position: absolute; inset: 0; width: 100%; height: 100%; border-radius: 12px; object-fit: cover; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45); background: #1b1b1b; } +.music-art:has(.music-cover:not([hidden])) .music-disc { display: none; } .music-disc { position: relative; flex: none; width: 96px; height: 96px; border-radius: 50%; background: repeating-radial-gradient(circle, #1b1b1b 0 2px, #262626 2px 4px); box-shadow: 0 0 0 2px rgba(201, 162, 39, 0.35), 0 6px 18px rgba(0, 0, 0, 0.45); @@ -10212,8 +10215,12 @@ a.hunt-review-spot-thumb { width: 64px; height: 40px; flex-shrink: 0; } .music-now-text { display: flex; flex: 1; flex-direction: column; gap: 0.2rem; min-width: 0; } .music-now-label { color: var(--text-muted); font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; } .music-now-title { font-size: 1.15rem; line-height: 1.3; overflow-wrap: anywhere; } +.music-now-meta { display: flex; flex-wrap: wrap; gap: 0.2rem 0.8rem; color: var(--text-muted); font-size: 0.8rem; } .music-now-link { width: max-content; font-size: 0.8rem; } +.music-next { margin-top: 0.3rem; color: var(--text-muted); font-size: 0.8rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .music-progress { position: relative; height: 6px; margin-top: 0.45rem; border-radius: 3px; background: rgba(255, 255, 255, 0.08); overflow: hidden; } +.music-progress.is-seekable { height: 8px; cursor: pointer; } +.music-progress.is-seekable:hover { background: rgba(255, 255, 255, 0.14); } .music-progress i { position: absolute; inset: 0 auto 0 0; width: 0; border-radius: 3px; background: linear-gradient(90deg, #a07d1c, #e3bd6e); transition: width 0.5s linear; } .music-times { display: flex; justify-content: space-between; color: var(--text-muted); font-size: 0.75rem; } .music-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; } @@ -10237,11 +10244,15 @@ a.hunt-review-spot-thumb { width: 64px; height: 40px; flex-shrink: 0; } .music-row select, .music-row input { flex: 1 1 12rem; width: auto; min-width: 0; } .music-list { display: flex; flex-direction: column; gap: 0.25rem; max-height: 22rem; margin: 0; padding: 0; overflow-y: auto; list-style: none; counter-reset: music; } .music-list li { display: flex; align-items: center; gap: 0.5rem; padding: 0.35rem 0.5rem; border-radius: 6px; background: rgba(255, 255, 255, 0.025); font-size: 0.88rem; } +.music-item-cover { flex: none; width: 40px; height: 24px; border-radius: 4px; object-fit: cover; background: #1b1b1b; } .music-list li:hover { background: rgba(255, 255, 255, 0.05); } ol.music-list li:not(.music-empty)::before { counter-increment: music; content: counter(music); min-width: 1.3rem; color: var(--text-muted); font-size: 0.75rem; text-align: right; } .music-item-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .music-item-meta { color: var(--text-muted); font-size: 0.75rem; } +.music-item-by { max-width: 7rem; overflow: hidden; color: var(--text-muted); font-size: 0.75rem; text-overflow: ellipsis; white-space: nowrap; } .music-item-btn { padding: 0.1rem 0.45rem; border: 1px solid transparent; background: none; box-shadow: none; color: var(--text-muted); font-size: 0.85rem; } +.music-list li .music-item-btn { opacity: 0.55; } +.music-list li:hover .music-item-btn, .music-list li:focus-within .music-item-btn { opacity: 1; } .music-item-btn:hover { border-color: var(--gold); background: rgba(201, 162, 39, 0.1); color: var(--text); box-shadow: none; } .music-item-btn.is-danger:hover { border-color: var(--danger); background: rgba(217, 83, 79, 0.12); } .music-empty { color: var(--text-muted); font-style: italic; } @@ -10260,6 +10271,10 @@ ol.music-list li:not(.music-empty)::before { counter-increment: music; content: .music-now { gap: 0.9rem; } .music-disc { width: 64px; height: 64px; } .music-disc span { inset: 22px; } + .music-art { width: 64px; height: 64px; } + .music-item-by, .music-item-cover { display: none; } + .music-list li { gap: 0.35rem; } + .music-item-btn { padding: 0.1rem 0.3rem; } .music-volume { flex-basis: 100%; margin-left: 0; } } @media (prefers-reduced-motion: reduce) { diff --git a/assets/js/music.js b/assets/js/music.js index 1bb5b8e..87bc0ed 100644 --- a/assets/js/music.js +++ b/assets/js/music.js @@ -1,7 +1,9 @@ /** * Musik-Seite (musik.php): Zustand des Bots alle paar Sekunden von musik_api.php holen (nur solange die Seite - * sichtbar ist), Knoepfe, Lautstaerke, Kanal waehlen, Titel/Links hinzufuegen, Warteschlange und Playlist - - * die Leitung (music.manage) laedt Titel hoch und loescht sie. Die Zeit des laufenden Titels zaehlt dazwischen weiter. + * sichtbar ist), Knoepfe, Lautstaerke, Spulen im Fortschrittsbalken, Kanal waehlen, Titel/Links hinzufuegen, + * Warteschlange (umsortieren, hinspringen), Verlauf und Playlist - die Leitung (music.manage) laedt Titel hoch + * und loescht sie. Die Zeit des laufenden Titels zaehlt zwischen zwei Abfragen weiter. Listen werden nur neu + * aufgebaut, wenn sich ihr Inhalt geaendert hat, damit Scrollposition und Klicks nicht verloren gehen. */ (function () { 'use strict'; @@ -16,8 +18,8 @@ var state = null; var stateAt = 0; var busy = false; - var pollTimer = null; var volumeTimer = null; + var signatures = {}; var $ = function (sel) { return root.querySelector(sel); }; var fmt = function (seconds) { @@ -28,6 +30,7 @@ var s = seconds % 60; return (h ? h + ':' + String(m).padStart(2, '0') : m) + ':' + String(s).padStart(2, '0'); }; + var icon = function (item) { return (SOURCE_ICONS[item.source] || '') + ' ' + item.title; }; var toastTimer = null; var toast = function (text, isError) { @@ -43,7 +46,7 @@ var request = function (fields, file) { var body = new FormData(); body.append('csrf_token', csrf); - Object.keys(fields).forEach(function (key) { body.append(key, fields[key]); }); + Object.keys(fields).forEach(function (key) { if (fields[key] !== null && fields[key] !== undefined) body.append(key, fields[key]); }); if (file) body.append('file', file); return fetch('/musik_api.php', { method: 'POST', body: body, credentials: 'same-origin', @@ -64,6 +67,18 @@ .catch(function (error) { toast(error.message, true); throw error; }) .finally(function () { busy = false; root.classList.remove('is-busy'); }); }; + var control = function (name, value) { + return act({ action: 'control', control: name, value: value === undefined ? null : String(value) }).catch(function () {}); + }; + // Abspielen: ist der Bot in keinem Kanal, kommt der gewaehlte Kanal mit (der Bot geht dann selbst hinein) + var play = function (query) { + var channel = $('[data-music-channels]').value; + if (state && !state.connected && !channel) { + toast('Bitte zuerst oben einen Sprachkanal wählen.', true); + return Promise.reject(new Error('kein Kanal')); + } + return act({ action: 'play', query: query, channel_id: state && !state.connected ? channel : null }); + }; var poll = function () { if (document.hidden || busy) return; @@ -89,15 +104,31 @@ $('[data-music-bar]').style.width = duration ? Math.min(100, elapsed / duration * 100) + '%' : '0%'; }; - var listItem = function (text, meta, buttons) { + var listItem = function (text, meta, buttons, cover, by) { var li = document.createElement('li'); + if (cover) { + var img = document.createElement('img'); + img.className = 'music-item-cover'; + img.src = cover; + img.alt = ''; + img.loading = 'lazy'; + li.appendChild(img); + } var label = document.createElement('span'); label.className = 'music-item-title'; label.textContent = text; + label.title = text; var info = document.createElement('span'); info.className = 'music-item-meta'; info.textContent = meta; li.appendChild(label); + if (by) { + var who = document.createElement('span'); + who.className = 'music-item-by'; + who.textContent = by; + who.title = 'Gewünscht von ' + by; + li.appendChild(who); + } li.appendChild(info); buttons.forEach(function (b) { var btn = document.createElement('button'); @@ -110,32 +141,74 @@ }); return li; }; + var emptyItem = function (text) { + var li = document.createElement('li'); + li.className = 'music-empty'; + li.textContent = text; + return li; + }; + // Liste nur neu aufbauen, wenn sich ihr Inhalt geaendert hat (sonst springt die Scrollposition alle 3 s) + var changed = function (key, data) { + var signature = JSON.stringify(data); + if (signatures[key] === signature) return false; + signatures[key] = signature; + return true; + }; var renderTracks = function () { - var list = $('[data-music-tracks]'); var filter = ($('[data-music-filter]').value || '').toLowerCase(); var tracks = (state && state.tracks) || []; + if (!changed('tracks', [tracks, filter])) return; + var list = $('[data-music-tracks]'); $('[data-music-tracks-count]').textContent = tracks.length ? '(' + tracks.length + ')' : ''; list.textContent = ''; var shown = tracks.filter(function (t) { return t.title.toLowerCase().indexOf(filter) !== -1; }); if (!shown.length) { - var empty = document.createElement('li'); - empty.className = 'music-empty'; - empty.textContent = tracks.length ? 'Kein Treffer.' : 'Noch keine Titel' + (canManage ? ' – lade unten welche hoch.' : '.'); - list.appendChild(empty); + list.appendChild(emptyItem(tracks.length ? 'Kein Treffer.' : 'Noch keine Titel' + (canManage ? ' – lade unten welche hoch.' : '.'))); return; } shown.forEach(function (t) { - var buttons = [{ label: '▶', title: 'In die Warteschlange', onClick: function () { act({ action: 'play', query: String(t.id) }); } }]; + var buttons = [{ label: '▶', title: 'In die Warteschlange', onClick: function () { play(String(t.id)).catch(function () {}); } }]; if (canManage) { buttons.push({ label: '🗑', title: 'Aus der Playlist löschen', danger: true, onClick: function () { - if (window.confirm('„' + t.title + '“ aus der Playlist löschen?')) act({ action: 'delete', track_id: String(t.id) }); + if (window.confirm('„' + t.title + '“ aus der Playlist löschen?')) act({ action: 'delete', track_id: String(t.id) }).catch(function () {}); } }); } list.appendChild(listItem('📁 ' + t.title, fmt(t.duration), buttons)); }); }; + var renderQueue = function (music) { + $('[data-music-queue-count]').textContent = music.queue_total ? '(' + music.queue_total + ')' : ''; + if (!changed('queue', [music.queue, music.queue_total])) return; + var queue = $('[data-music-queue]'); + queue.textContent = ''; + if (!music.queue.length) queue.appendChild(emptyItem('Leer.')); + music.queue.forEach(function (item, index) { + var buttons = []; + if (index > 0) buttons.push({ label: '⤴', title: 'Als Nächstes', onClick: function () { control('move', index); } }); + buttons.push({ label: '▶', title: 'Jetzt abspielen', onClick: function () { control('jump', index); } }); + buttons.push({ label: '✕', title: 'Aus der Warteschlange', onClick: function () { control('remove', index); } }); + queue.appendChild(listItem(icon(item), fmt(item.duration), buttons, item.thumbnail, item.added_by)); + }); + if (music.queue_total > music.queue.length) { + queue.appendChild(emptyItem('… und ' + (music.queue_total - music.queue.length) + ' weitere')); + } + }; + + var renderHistory = function (music) { + var history = music.history || []; + $('[data-music-history-card]').hidden = !history.length; + if (!changed('history', history)) return; + var list = $('[data-music-history]'); + list.textContent = ''; + history.forEach(function (item) { + list.appendChild(listItem(icon(item), fmt(item.duration), [ + { label: '↻', title: 'Noch einmal in die Warteschlange', onClick: function () { play(item.replay).catch(function () {}); } } + ], item.thumbnail, item.added_by)); + }); + }; + var render = function (music) { state = music; stateAt = Date.now(); @@ -147,16 +220,29 @@ var current = music.current; $('[data-music-status]').textContent = current ? (music.paused ? '⏸️ Pausiert' : '🎶 Läuft gerade') : 'Gerade läuft nichts'; - $('[data-music-title]').textContent = current ? (SOURCE_ICONS[current.source] || '') + ' ' + current.title : '–'; + $('[data-music-title]').textContent = current ? icon(current) : '–'; + var by = $('[data-music-by]'); + by.hidden = !(current && current.added_by); + by.textContent = current && current.added_by ? 'von ' + current.added_by : ''; var link = $('[data-music-link]'); link.hidden = !(current && current.url); if (current && current.url) link.href = current.url; + var cover = $('[data-music-cover]'); + var coverUrl = current && current.thumbnail ? current.thumbnail : ''; + if (cover.getAttribute('src') !== coverUrl) { + if (coverUrl) cover.src = coverUrl; else cover.removeAttribute('src'); + } + cover.hidden = !coverUrl; $('[data-music-duration]').textContent = current ? fmt(current.duration) : '0:00'; + $('[data-music-progress]').classList.toggle('is-seekable', !!(current && current.duration)); if (!current) { $('[data-music-elapsed]').textContent = '0:00'; $('[data-music-bar]').style.width = '0%'; } tick(); + var next = $('[data-music-next]'); + next.hidden = !music.queue.length; + next.textContent = music.queue.length ? 'Als Nächstes: ' + music.queue[0].title : ''; root.querySelectorAll('[data-music-control="shuffle"]').forEach(function (b) { b.classList.toggle('is-on', !!music.shuffle); }); root.querySelectorAll('[data-music-control="loop"]').forEach(function (b) { b.classList.toggle('is-on', !!music.loop); }); @@ -171,7 +257,7 @@ // Sprachkanaele var select = $('[data-music-channels]'); - if (document.activeElement !== select) { + if (document.activeElement !== select && changed('channels', [music.voice_channels, music.channel])) { var chosen = select.value || (music.channel ? String(music.channel.id) : ''); select.textContent = ''; var first = document.createElement('option'); @@ -192,27 +278,8 @@ $('[data-music-leave]').hidden = !music.connected; $('[data-music-join]').textContent = music.connected ? 'Umziehen' : 'Holen'; - // Warteschlange - var queue = $('[data-music-queue]'); - queue.textContent = ''; - $('[data-music-queue-count]').textContent = music.queue_total ? '(' + music.queue_total + ')' : ''; - if (!music.queue.length) { - var empty = document.createElement('li'); - empty.className = 'music-empty'; - empty.textContent = 'Leer.'; - queue.appendChild(empty); - } - music.queue.forEach(function (item, index) { - queue.appendChild(listItem((SOURCE_ICONS[item.source] || '') + ' ' + item.title, fmt(item.duration), [ - { label: '✕', title: 'Aus der Warteschlange', onClick: function () { act({ action: 'control', control: 'remove', value: String(index) }); } } - ])); - }); - if (music.queue_total > music.queue.length) { - var more = document.createElement('li'); - more.className = 'music-empty'; - more.textContent = '… und ' + (music.queue_total - music.queue.length) + ' weitere'; - queue.appendChild(more); - } + renderQueue(music); + renderHistory(music); renderTracks(); }; @@ -220,7 +287,7 @@ root.querySelectorAll('[data-music-control]').forEach(function (button) { button.addEventListener('click', function () { if (button.dataset.musicControl === 'clear' && !window.confirm('Warteschlange leeren?')) return; - act({ action: 'control', control: button.dataset.musicControl }); + control(button.dataset.musicControl); }); }); var volume = $('[data-music-volume]'); @@ -229,9 +296,23 @@ clearTimeout(volumeTimer); volumeTimer = setTimeout(function () { volumeTimer = null; - act({ action: 'control', control: 'volume', value: volume.value }).catch(function () {}); + control('volume', volume.value); }, 350); }); + // Spulen: Klick in den Fortschrittsbalken + $('[data-music-progress]').addEventListener('click', function (event) { + if (!state || !state.current || !state.current.duration) return; + var rect = event.currentTarget.getBoundingClientRect(); + var ratio = Math.min(1, Math.max(0, (event.clientX - rect.left) / rect.width)); + control('seek', Math.floor(ratio * state.current.duration)); + }); + // Tastatur: Leertaste = Pause/Weiter, N = naechster Titel (nicht in Eingabefeldern) + document.addEventListener('keydown', function (event) { + var tag = (event.target.tagName || '').toLowerCase(); + if (tag === 'input' || tag === 'textarea' || tag === 'select' || event.ctrlKey || event.altKey || event.metaKey) return; + if (event.key === ' ') { event.preventDefault(); control('toggle'); } + if (event.key === 'n' || event.key === 'N') control('skip'); + }); $('[data-music-join]').addEventListener('click', function () { var channel = $('[data-music-channels]').value; if (!channel) { toast('Bitte zuerst einen Sprachkanal wählen.', true); return; } @@ -246,12 +327,12 @@ var button = event.target.querySelector('button'); button.disabled = true; button.textContent = 'Suche …'; - act({ action: 'play', query: query }) + play(query) .then(function () { input.value = ''; }) .catch(function () {}) .finally(function () { button.disabled = false; button.textContent = 'Abspielen'; }); }); - $('[data-music-play-all]').addEventListener('click', function () { act({ action: 'play', query: '' }).catch(function () {}); }); + $('[data-music-play-all]').addEventListener('click', function () { play('').catch(function () {}); }); $('[data-music-filter]').addEventListener('input', renderTracks); var upload = $('[data-music-upload]'); @@ -276,7 +357,7 @@ } poll(); - pollTimer = setInterval(poll, POLL_MS); + setInterval(poll, POLL_MS); setInterval(tick, 500); document.addEventListener('visibilitychange', function () { if (!document.hidden) poll(); }); })(); diff --git a/includes/music.php b/includes/music.php index 0e74228..bf884d0 100644 --- a/includes/music.php +++ b/includes/music.php @@ -13,7 +13,7 @@ const MUSIC_API_TIMEOUT = 60; const MUSIC_UPLOAD_MAX = 25 * 1024 * 1024; const MUSIC_UPLOAD_EXTENSIONS = ['mp3', 'ogg', 'm4a', 'wav', 'flac', 'opus']; const MUSIC_QUERY_MAX = 500; -const MUSIC_ACTIONS = ['toggle', 'pause', 'resume', 'skip', 'stop', 'shuffle', 'loop', 'volume', 'remove', 'clear']; +const MUSIC_ACTIONS = ['toggle', 'pause', 'resume', 'skip', 'stop', 'shuffle', 'loop', 'volume', 'remove', 'clear', 'move', 'jump', 'seek']; /** Basis der Bot-API (eine Ebene ueber /api/raids). */ function music_api_base(): string diff --git a/includes/patchnotes.php b/includes/patchnotes.php index 5646512..ab4a4e7 100644 --- a/includes/patchnotes.php +++ b/includes/patchnotes.php @@ -27,6 +27,7 @@ const PATCHNOTES = [ 'changes' => [ ['type' => 'neu', 'area' => 'Musik', 'text' => 'Neue Seite „Musik“: Bot in einen Sprachkanal holen oder rausschicken, YouTube-/YouTube-Music-/Spotify-Links (auch Playlists und Alben) oder einfach einen Suchbegriff abspielen, Warteschlange ansehen, Titel überspringen, pausieren, Zufall, Schleife und Lautstärke.'], ['type' => 'neu', 'area' => 'Discord', 'text' => 'Dasselbe geht im Discord mit /musik (join, play, skip, pause, weiter, stop, leave, …) – diese Befehle darf jedes Mitglied nutzen. Nach 5 Minuten allein im Kanal geht der Bot von selbst.'], + ['type' => 'neu', 'area' => 'Musik', 'text' => 'Zum laufenden Titel gibt es das Vorschaubild, wer ihn gewünscht hat und was als Nächstes kommt. Ein Klick in den Fortschrittsbalken spult, Leertaste pausiert, N springt weiter. In der Warteschlange lässt sich ein Titel nach vorn holen (⤴), sofort abspielen (▶) oder entfernen. Unter „Zuletzt gespielt“ holst du einen Titel mit ↻ noch einmal. Ist der Bot in keinem Kanal, kommt er beim Abspielen gleich in den gewählten.'], ['type' => 'neu', 'area' => 'Musik', 'audience' => 'leitung', 'text' => 'Eigene Audiodateien (mp3, ogg, m4a, wav, flac, opus – bis 25 MB) für die Playlist der Legion hochladen und löschen, auf der Seite oder mit /musik hochladen. Dafür gibt es das neue Recht „music.manage“, das die Leitung in der Rollenverwaltung vergibt.'], ], ], diff --git a/musik.php b/musik.php index fa456eb..372dfb1 100644 --- a/musik.php +++ b/musik.php @@ -1,8 +1,8 @@ Verbinde mit dem Bot …
- +
+ + +
Gerade läuft nichts – - - + +
0:000:00
+
- - + +
diff --git a/musik_api.php b/musik_api.php index 1820017..21171ac 100644 --- a/musik_api.php +++ b/musik_api.php @@ -6,8 +6,8 @@ * GET ?action=state Zustand (Kanal, Titel, Warteschlange, Sprachkanaele, Playlist) * POST action=join, channel_id Bot in einen Sprachkanal holen * POST action=leave Bot aus dem Sprachkanal schicken - * POST action=play, query Titel-ID, YouTube-/Spotify-Link, Suche oder leer = ganze Playlist - * POST action=control, control, value toggle, skip, stop, shuffle, loop, volume (0-100), remove (Index), clear + * POST action=play, query, channel_id Titel-ID, YouTube-/Spotify-Link, Suche oder leer = ganze Playlist (channel_id: Kanal, falls der Bot in keinem ist) + * POST action=control, control, value toggle, skip, stop, shuffle, loop, volume (0-100), remove/move/jump (Index), seek (Sekunde), clear * POST action=upload, file, title Datei in die Playlist (music.manage) * POST action=delete, track_id Titel aus der Playlist (music.manage) * Steuern: music.control, Playlist verwalten: music.manage. POST mit CSRF-Token. @@ -61,8 +61,15 @@ try { $reply(true, (string) ($data['message'] ?? ''), ['music' => $data['music'] ?? null]); case 'play': + // Ist der Bot noch in keinem Sprachkanal, holt ihn der gewaehlte Kanal gleich mit $query = mb_substr(trim((string) ($_POST['query'] ?? '')), 0, MUSIC_QUERY_MAX); - $data = music_api_request('POST', $guild . '/play', ['query' => $query]); + $channel = (string) ($_POST['channel_id'] ?? ''); + $user = current_user(); + $data = music_api_request('POST', $guild . '/play', [ + 'query' => $query, + 'user' => (string) ($user['display_name'] !== '' ? $user['display_name'] : $user['username']), + 'channel_id' => ctype_digit($channel) ? (int) $channel : null, + ]); $reply(true, (string) ($data['message'] ?? ''), ['music' => $data['music'] ?? null]); case 'control':