79 lines
3.3 KiB
PHP
79 lines
3.3 KiB
PHP
<?php
|
|
/**
|
|
* Musik im Discord-Sprachkanal (Nezekan-Bot, cogs/music.py + music_api.py auf dem Bot): Steuerung von der
|
|
* Website aus (musik.php, musik_api.php, assets/js/music.js). Abspielen lassen sich die Playlist der Legion
|
|
* (eigene Dateien, Upload mit music.manage), YouTube-/YouTube-Music-Links, Spotify-Links und eine freie Suche -
|
|
* aufgeloest und abgespielt wird alles auf dem Bot, die Website schickt nur Befehle.
|
|
*/
|
|
require_once __DIR__ . '/raids_api.php';
|
|
require_once __DIR__ . '/events_api.php';
|
|
|
|
// Playlists und Spotify-Listen aufzuloesen dauert - laenger warten als bei anderen Bot-Aufrufen
|
|
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'];
|
|
|
|
/** Basis der Bot-API (eine Ebene ueber /api/raids). */
|
|
function music_api_base(): string
|
|
{
|
|
return (string) preg_replace('#/raids$#', '', raids_api_config()['url']);
|
|
}
|
|
|
|
/**
|
|
* Aufruf der Musik-API des Bots.
|
|
*
|
|
* @param array<string, mixed>|null $json JSON-Body
|
|
* @param array{0: string, 1: string, 2: string}|null $file [Pfad, Dateiname, Titel] fuer den Upload (multipart)
|
|
* @return array<string, mixed>
|
|
* @throws RuntimeException mit verstaendlicher Meldung
|
|
*/
|
|
function music_api_request(string $method, string $path, ?array $json = null, ?array $file = null): array
|
|
{
|
|
if (!function_exists('curl_init')) {
|
|
throw new RuntimeException('Die PHP-cURL-Erweiterung ist auf diesem Server nicht aktiviert.');
|
|
}
|
|
$headers = ['X-API-Key: ' . raids_api_config()['key']];
|
|
$ch = curl_init(music_api_base() . $path);
|
|
$options = [
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_CUSTOMREQUEST => $method,
|
|
CURLOPT_TIMEOUT => MUSIC_API_TIMEOUT,
|
|
CURLOPT_CONNECTTIMEOUT => 5,
|
|
];
|
|
if ($file !== null) {
|
|
$options[CURLOPT_POSTFIELDS] = [
|
|
'file' => new CURLFile($file[0], 'application/octet-stream', $file[1]),
|
|
'title' => $file[2],
|
|
'user_id' => (string) (current_user()['discord_id'] ?? '0'),
|
|
];
|
|
} elseif ($json !== null) {
|
|
$headers[] = 'Content-Type: application/json';
|
|
$options[CURLOPT_POSTFIELDS] = json_encode($json, JSON_UNESCAPED_UNICODE);
|
|
}
|
|
$options[CURLOPT_HTTPHEADER] = $headers;
|
|
curl_setopt_array($ch, $options);
|
|
$response = curl_exec($ch);
|
|
$status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
if ($response === false) {
|
|
throw new RuntimeException('Der Bot ist gerade nicht erreichbar (' . $error . ').');
|
|
}
|
|
$data = json_decode((string) $response, true);
|
|
if ($status < 200 || $status >= 300) {
|
|
$detail = is_array($data) ? ($data['detail'] ?? $data['message'] ?? '') : '';
|
|
throw new RuntimeException(is_string($detail) && $detail !== '' ? $detail : 'Der Bot antwortet mit Status ' . $status . '.');
|
|
}
|
|
if (!is_array($data)) {
|
|
throw new RuntimeException('Unerwartete Antwort vom Bot.');
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
/** Zustand der Musik auf dem Legions-Server (Kanal, Titel, Warteschlange, Playlist). */
|
|
function music_state(): array
|
|
{
|
|
return (array) (music_api_request('GET', '/music/' . events_guild_id())['music'] ?? []);
|
|
}
|