LuminaWebsite/bot_api.php
Maaxxs fe1561f4f7 Raids: attendance list, automatic points on archive, reminder callback
Each raid gets an attendance list for raids.manage (present, absent, excused)
with quick actions, members added by hand and a "checked" mark with who and
when. The contribution form can now select a raid's present members for
points or its absent members for a deduction.

Raids can carry points for present members and a deduction for absent ones
(raid_settings). When a raid is archived - on the website or via the button
in Discord, detected on the next raid or contribution page view - the site
awards them once: by attendance if a list exists, otherwise to everyone in a
group without deduction, and not at all if points were already given for
that raid. Settings, attendance and awards follow a raid_id change when the
API re-posts a deleted message.

bot_api.php lets the bot ask which signed-up members have raids.manage when a
raid starts (shared key in config.php bot_callback.key); the bot then sends
them a DM with a link to the list. The bot side lives in the separate bot
repository (cogs/attendance.py).

Also fixes contribution_awards.raid_id being too small for Discord IDs (it
is widened automatically) and raid IDs losing precision in the contribution
form's JavaScript, which kept the raid preselection from ever matching.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 09:52:34 +02:00

72 lines
2.7 KiB
PHP

<?php
/**
* Rueckkanal fuer den Discord-Bot (cogs/attendance.py im Bot).
*
* Wenn die Uhrzeit eines Raids erreicht ist, fragt der Bot hier nach, wer von den Angemeldeten
* eine Erinnerung zur Anwesenheitsliste bekommen soll: alle mit einem aktiven Website-Konto und
* der Berechtigung raids.manage. Die Discord-PN schickt der Bot selbst.
*
* GET /bot_api.php?action=attendance_recipients&raid_id=...&discord_ids=1,2,3
* Header: X-Bot-Key: <config.php bot_callback.key>
*/
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/raid_attendance.php';
header('Content-Type: application/json; charset=UTF-8');
function bot_api_reply(int $status, array $data): void
{
http_response_code($status);
echo json_encode($data, JSON_UNESCAPED_UNICODE);
exit;
}
$expectedKey = bot_callback_key();
if ($expectedKey === null) {
bot_api_reply(503, ['status' => 'error', 'message' => 'bot_callback.key ist in config.php nicht eingetragen.']);
}
$givenKey = (string) ($_SERVER['HTTP_X_BOT_KEY'] ?? '');
if (!hash_equals($expectedKey, $givenKey)) {
bot_api_reply(403, ['status' => 'error', 'message' => 'Ungültiger Schlüssel.']);
}
$action = (string) ($_GET['action'] ?? '');
if ($action !== 'attendance_recipients') {
bot_api_reply(400, ['status' => 'error', 'message' => 'Unbekannte Aktion.']);
}
$raidId = (string) ($_GET['raid_id'] ?? '');
if (!ctype_digit($raidId)) {
bot_api_reply(400, ['status' => 'error', 'message' => 'raid_id fehlt.']);
}
$discordIds = array_values(array_filter(
array_map('trim', explode(',', (string) ($_GET['discord_ids'] ?? ''))),
static fn (string $id): bool => $id !== '' && ctype_digit($id)
));
$recipients = [];
if (!empty($discordIds)) {
$placeholders = implode(',', array_fill(0, count($discordIds), '?'));
$stmt = db()->prepare(
"SELECT DISTINCT u.discord_id FROM users u
JOIN user_roles ur ON ur.user_id = u.id
JOIN role_permissions rp ON rp.role_id = ur.role_id
JOIN permissions p ON p.id = rp.permission_id
WHERE u.status = 'active' AND p.`key` = 'raids.manage' AND u.discord_id IN ($placeholders)"
);
$stmt->execute($discordIds);
$recipients = array_map('strval', $stmt->fetchAll(PDO::FETCH_COLUMN));
}
$settings = raid_settings_get((int) $raidId);
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = (string) ($_SERVER['HTTP_HOST'] ?? '');
bot_api_reply(200, [
'status' => 'success',
'recipients' => $recipients,
// Schon kontrolliert (z.B. vor Raidbeginn)? Dann braucht es keine Erinnerung mehr.
'checked' => $settings['attendance_checked_at'] !== null,
'url' => $scheme . '://' . $host . '/raid_attendance.php?raid=' . $raidId,
]);