Discord-Bot (admin/bot.php). */ require_once __DIR__ . '/../includes/bootstrap.php'; require_once __DIR__ . '/../includes/discord_bot.php'; require_permission('bot.manage'); if ($_SERVER['REQUEST_METHOD'] !== 'POST') { redirect('/admin/bot.php'); } verify_csrf(); /** * Kanal-ID aus dem Formular, wenn der Bot dort posten darf - sonst null. * * @param array> $channels */ function bot_channel_input(string $raw, array $channels): ?int { foreach ($channels as $channel) { if ((string) ($channel['channel_id'] ?? '') === $raw && !empty($channel['can_post'])) { return (int) $raw; } } return null; } $action = (string) ($_POST['action'] ?? ''); try { if ($action === 'channels') { $before = discord_bot_settings(); $channels = fetch_guild_channels(); // Consilium-Raids: Kanal auf dem Server des Consiliums, sobald einer eingetragen ist require_once __DIR__ . '/../includes/consilium.php'; $consiliumGuild = consilium_guild_id(); $changes = []; $logFields = []; foreach (DISCORD_BOT_CHANNELS as $key => [$label]) { $raw = trim((string) ($_POST[$key] ?? '')); if ($raw === '' || $raw === ($before[$key] ?? '')) { continue; // leer lassen = unveraendert (den Kanal abwaehlen kennt der Bot nicht) } $channelId = bot_channel_input($raw, $key === 'consilium_raid_channel' && $consiliumGuild !== '' ? fetch_guild_channels($consiliumGuild) : $channels); if ($channelId === null) { throw new RuntimeException($label . ': In diesem Kanal darf der Bot nicht posten.'); } $changes[$key] = $channelId; $logFields[$label] = admin_log_change(event_log_channel($before[$key] ?? '') ?: 'keiner', event_log_channel($channelId)); } if (empty($changes)) { set_flash('info', 'Keine Änderungen.'); } else { discord_bot_save_channels($changes); admin_log('🤖 Discord-Bot: Kanäle geändert', $logFields); set_flash('success', 'Kanäle gespeichert.'); } } elseif ($action === 'consilium_guild') { // Server des Consilium Elyseum (includes/consilium.php) - leer = keiner require_once __DIR__ . '/../includes/consilium.php'; $raw = trim((string) ($_POST['consilium_guild_id'] ?? '')); $before = consilium_guild_id(); if ($raw !== '' && preg_match('/^[0-9]{5,20}$/', $raw) !== 1) { throw new RuntimeException('Die Server-ID besteht nur aus Ziffern.'); } if ($raw !== '' && $raw === discord_guild_id()) { throw new RuntimeException('Das ist der Server der Legion – hier gehört der Server von ' . CONSILIUM_NAME . ' hin.'); } if ($raw === $before) { set_flash('info', 'Keine Änderungen.'); } else { setting_set('consilium_guild_id', $raw); // Alle beim naechsten Seitenaufruf mit Discord abgleichen (Gruppen des Consiliums) db()->exec('UPDATE users SET discord_synced_at = NULL'); admin_log('🤖 Discord-Bot: Server von ' . CONSILIUM_NAME . ' geändert', [ 'Server' => admin_log_change($before !== '' ? $before : 'keiner', $raw !== '' ? $raw : 'keiner'), ]); $botThere = true; if ($raw !== '') { try { fetch_guild_channels($raw); } catch (Throwable $e) { $botThere = false; } } set_flash($botThere ? 'success' : 'info', $botThere ? 'Server gespeichert.' : 'Server gespeichert – ' . (siel_api_config() !== null ? 'Siel ist' : 'der Bot ist') . ' aber noch nicht darauf. Bitte einladen, sonst gibt es dort keine Raids von ' . CONSILIUM_NAME . '.'); } } elseif ($action === 'panel') { $kind = (string) ($_POST['kind'] ?? ''); if (!isset(DISCORD_BOT_PANELS[$kind])) { throw new RuntimeException('Unbekannte Übersicht.'); } $channelId = bot_channel_input(trim((string) ($_POST['channel_id'] ?? '')), fetch_guild_channels()); if ($channelId === null) { throw new RuntimeException('Bitte einen Kanal wählen, in dem der Bot posten darf.'); } $roles = []; $logFields = ['Kanal' => event_log_channel($channelId)]; if ($kind === 'reports') { // Rollen des Melde-Panels: nur solche, die es auf dem Server gibt ('' = keine) $knownRoles = array_map(static fn (array $r): string => (string) ($r['role_id'] ?? ''), fetch_guild_roles()); foreach (['ping_role_id' => 'Ping', 'lead_role_id' => 'Leitung im Text'] as $field => $fieldLabel) { $raw = trim((string) ($_POST[$field] ?? '')); if ($raw !== '' && !in_array($raw, $knownRoles, true)) { throw new RuntimeException($fieldLabel . ': Diese Rolle gibt es auf dem Discord-Server nicht (mehr).'); } $roles[$field] = $raw !== '' ? (int) $raw : null; $logFields[$fieldLabel] = $raw !== '' ? '<@&' . $raw . '>' : 'keine'; } } discord_bot_post_panel($kind, $channelId, $roles); $label = DISCORD_BOT_PANELS[$kind][0]; admin_log('🤖 ' . $label . ' im Discord gepostet', $logFields, 'success'); set_flash('success', $label . ' ist gepostet.'); } elseif ($action === 'resync') { $result = discord_bot_resync_raids(); $reposted = array_map(static fn (array $r): string => (string) ($r['title'] ?? ''), $result['reposted']); $failed = array_map(static fn (array $r): string => (string) ($r['title'] ?? '') . ' (' . (string) ($r['error'] ?? '') . ')', $result['failed']); admin_log('🔄 Raid-Nachrichten abgeglichen', [ 'Aktualisiert' => (string) $result['updated'], 'Neu gepostet' => implode(', ', $reposted), 'Fehlgeschlagen' => implode(', ', $failed), ]); $message = $result['updated'] . ' Raid-Nachricht' . ($result['updated'] === 1 ? '' : 'en') . ' aktualisiert'; if ($reposted) { $message .= ', neu gepostet: ' . implode(', ', $reposted); } set_flash('success', $message . '.'); if ($failed) { set_flash('error', 'Nicht abgeglichen: ' . implode(', ', $failed)); } } else { throw new RuntimeException('Unbekannte Aktion.'); } } catch (Throwable $e) { set_flash('error', $e->getMessage()); } redirect('/admin/bot.php');