Asked for: the alliances move to their own Discord server with the roles Force Commander, Legion Lead, Legion Officer and Legionär. Decided: everyone with a role there sees the Consilium (our members too), no role per legion (assignment stays on the website), new accounts are approved by our admins, rights per role as planned, a permission for Consilium calendar entries that is assigned later. Server ID 1553882046793777283. - Two Discord servers: settings.consilium_guild_id (seeded, Verwaltung -> Discord-Bot with a check whether the bot is there); a group's role lies on one of them (roles.discord_guild_id, NULL = legion, schema 71). Login and the periodic sync read both memberships (discord_fetch_memberships/discord_apply_memberships) and sync groups per server; name, avatar, joined date and classes come from the legion server, for allies from the Consilium server. - Leaving: whoever leaves a server loses its groups; an account is deactivated only when it is on neither (discord_member_left_guild; bot_api.php takes in_guilds from the bot, the sync checks all servers, older bot versions cannot deactivate Consilium members). Logout text no longer names a server. - Group form lists the roles of both servers, the group list marks Consilium roles; Consilium-Raids channel is chosen from the Consilium server's channels. - Rights per area: consilium.raids / consilium.raids.manage for Consilium raids (create, manage, restore, delete), raids.create / raids.manage only for legion raids (raid_area_can_create, raid_area_can_manage_all, raid_can_manage asks the raid's area). consilium.calendar: only with it Consilium calendar entries (allies without it cannot add entries; legion members with it get "Für wen?" with legion/Consilium). - Consilium member list: everyone with a role on the Consilium server - our legion as its own section, allied legions, without legion - with the Consilium ranks as tags; legion list and profiles leave the Consilium groups out for those who do not see the Consilium. Allies pick their class in the profile. - Consilium admin page: setup checklist for the new structure with the groups of the Consilium server and their rights (setup only for roles.manage). Privacy page, README. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
140 lines
6.5 KiB
PHP
140 lines
6.5 KiB
PHP
<?php
|
||
/** Formulare von Verwaltung -> 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<int, array<string, mixed>> $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 des Consiliums 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 des ' . 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 – der Bot ist aber noch nicht darauf. Bitte einladen, sonst gibt es dort keine Consilium-Raids.');
|
||
}
|
||
} 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');
|