LuminaWebsite/includes/discord_bot.php
Maaxxs 17ccd3e26f Discord bot as overview: everything is managed on the website, raid signup in one click
The bot (Aion2_RaidPlaner, deployed 2026-09-25) has no slash commands any more. Raids,
premades, events, activity checks and reports are created and managed only here; in Discord
members sign up for raids, answer invitations and activity checks and report players.

- Raid signup works the same in Discord and here: the bot assigns the group (perform_signup),
  the website only sends the wish (raid_signup(..., $wish)) and shows the reason when it did
  not fit (locked, role full, raid full). raid_wish_is_sensible() and the role constants are
  gone. Declining ("Abmelden") now keeps the player under "Abgemeldet" (raid_decline, new bot
  endpoint .../decline) like in Discord; the raid card shows "Du hast dich abgemeldet" with
  "Doch anmelden". Class input is checked against AION_CLASSES.
- The Discord message shows the raid lead and the Lumipunkte: raid_create_action/raid_edit_action
  send lead_id/lead_name/points_text, raid_sync_extras() on raids.php pushes them for raids
  where the bot is missing them (at most 3 per page view, follows changed raid IDs).
- bot_api.php?action=member: main class (profile choice) and whether the person has an
  (active) account - the bot preselects the class and hints that Lumipunkte need an account.
- New page Verwaltung -> Discord-Bot (admin/bot.php, includes/discord_bot.php, permission
  bot.manage, on update granted to groups with raids.manage; schema 53): raid, log and report
  channel, post the premade overview (updates itself) and the report panel (the bot replaces
  the previous one), resync all raid messages (reposted raids move their settings via
  raid_rename_id). Audit area "Discord-Bot", sidebar entry with a new bot icon.
- Reports: after picking a player in the filter, delete all of their reports at once
  (DELETE /api/reports/user/{id}), which also lifts the auto-mute.
- Calendar and raid logs no longer count declined players as signed up / on the bench.
- Texts that mentioned bot commands (events, activity check, config example, README) updated;
  privacy policy mentions the main-class lookup and that declines stay visible. Patch notes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 03:15:55 +02:00

103 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/events_api.php';
require_once __DIR__ . '/raid_attendance.php';
/**
* Verwaltung -> Discord-Bot (admin/bot.php): was frueher die Slash-Befehle der Legionsleitung im Bot
* erledigt haben. Seit dem Umbau (09/2026) wird im Discord nichts mehr erstellt oder verwaltet - der Bot
* zeigt an, erinnert und nimmt Raid-Anmeldungen und Meldungen entgegen. Hier legt die Leitung fest,
* wohin er postet: Raid-, Log- und Melde-Kanal, die Premade-Uebersicht und das Melde-Panel.
*/
// Die Kanaele, die der Bot kennt: Schluessel in /api/settings => [Bezeichnung, Erklaerung]
const DISCORD_BOT_CHANNELS = [
'raid_channel' => ['Raid-Kanal', 'Hier postet der Bot neue Raids von der Website - mit Anmeldung.'],
'log_channel' => ['Log-Kanal', 'Verwaltungsaktionen der Website und Meldungen des Bots (z.B. wer die Erinnerung zur Anwesenheit bekommen hat).'],
'report_channel' => ['Melde-Kanal', 'Hier landen Meldungen über „Spieler melden“ - ohne eigenen Kanal im Log-Kanal. Nur für die Leitung sichtbar machen!'],
];
// Nachrichten, die der Bot auf Wunsch postet und sich merkt: Art => [Bezeichnung, Pfad der Bot-API, Erklaerung]
const DISCORD_BOT_PANELS = [
'premades' => ['Premade-Übersicht', '/premades/panel', 'Alle Premade-Gruppen mit Lead, Kader und Reserve - aktualisiert sich bei jeder Änderung selbst.'],
'reports' => ['Melde-Panel', '/reports/panel', 'Der Knopf „Spieler melden“: Mitglieder melden damit vertraulich einen Verstoß.'],
];
/**
* @param array<string, mixed>|null $jsonBody
* @return array<string, mixed>
* @throws RuntimeException
*/
function discord_bot_request(string $method, string $path, ?array $jsonBody = null): array
{
// Die Bot-API liegt eine Ebene ueber /api/raids
$base = preg_replace('#/raids$#', '', raids_api_config()['url']);
return bot_api_request($method, $base . $path, [], $jsonBody, 'Bot-API');
}
/**
* Kanaele und wo Premade-Uebersicht und Melde-Panel stehen (Kanal-IDs als Strings, '' = nicht festgelegt).
*
* @return array<string, string>
* @throws RuntimeException
*/
function discord_bot_settings(): array
{
$data = discord_bot_request('GET', '/settings/' . events_guild_id());
$settings = [];
foreach (array_merge(array_keys(DISCORD_BOT_CHANNELS), ['premade_panel_channel', 'report_panel_channel']) as $key) {
$settings[$key] = !empty($data[$key]) ? (string) $data[$key] : '';
}
return $settings;
}
/**
* @param array<string, int> $channels Schluessel aus DISCORD_BOT_CHANNELS => Kanal-ID
* @throws RuntimeException
*/
function discord_bot_save_channels(array $channels): void
{
discord_bot_request('PATCH', '/settings/' . events_guild_id(), $channels);
}
/**
* Postet die Premade-Uebersicht bzw. das Melde-Panel; eine bisherige Nachricht davon loescht der Bot.
*
* @throws RuntimeException
*/
function discord_bot_post_panel(string $kind, int $channelId): void
{
discord_bot_request('POST', DISCORD_BOT_PANELS[$kind][1], ['channel_id' => $channelId]);
}
/**
* Alle aktiven Raid-Nachrichten abgleichen: aktualisieren, fehlende neu posten. Neu gepostete Raids haben
* eine neue ID - Einstellungen, Anwesenheit und Lumipunkte ziehen hier mit um (raid_rename_id).
*
* @return array{updated: int, reposted: array<int, array<string, mixed>>, failed: array<int, array<string, mixed>>}
* @throws RuntimeException
*/
function discord_bot_resync_raids(): array
{
$data = discord_bot_request('POST', '/raids/resync');
foreach ((array) ($data['reposted'] ?? []) as $moved) {
raid_rename_id((int) ($moved['old'] ?? 0), (int) ($moved['new'] ?? 0));
}
return [
'updated' => (int) ($data['updated'] ?? 0),
'reposted' => (array) ($data['reposted'] ?? []),
'failed' => (array) ($data['failed'] ?? []),
];
}
/** "#raids (Kategorie)" fuer eine Kanal-ID aus der Kanalliste - sonst die ID. */
function discord_bot_channel_label(string $channelId, array $channels): string
{
foreach ($channels as $channel) {
if ((string) ($channel['channel_id'] ?? '') === $channelId) {
return '#' . (string) ($channel['name'] ?? '') . (!empty($channel['category']) ? ' (' . $channel['category'] . ')' : '');
}
}
return 'Kanal ' . $channelId;
}