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(); $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, $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 === '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.'); } discord_bot_post_panel($kind, $channelId); $label = DISCORD_BOT_PANELS[$kind][0]; admin_log('🤖 ' . $label . ' im Discord gepostet', ['Kanal' => event_log_channel($channelId)], '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');