Reported: the manual Discord sync said "Discord war gerade nicht erreichbar". Since the Consilium has its own server,
every sync asks Discord twice in a row (legion and Consilium server), and setting up linked groups triggers extra
syncs - most likely Discord answered 429.
- discord_fetch_member(): on 429 wait retry_after (up to 5 s) and try once more; if Discord still refuses, a clear
message ("Discord bremst gerade zu viele Abfragen - in N Sekunden noch einmal versuchen").
- discord_live_check() remembers the reason (discord_last_error(), only own messages, no database errors) and writes
it to the error log; the manual sync in the user form and in the profile show it instead of "nicht erreichbar".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
59 lines
2.8 KiB
PHP
59 lines
2.8 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../includes/bootstrap.php';
|
|
require_permission('users.edit');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
redirect('/admin/users.php');
|
|
}
|
|
verify_csrf();
|
|
|
|
// Manueller Abgleich mit Discord - fuer ein Konto (aus dem Benutzerformular) oder fuer alle.
|
|
$userId = (int) ($_POST['user_id'] ?? 0);
|
|
|
|
if ($userId > 0) {
|
|
$result = discord_live_check($userId, true);
|
|
$messages = [
|
|
'ok' => ['success', 'Mit Discord abgeglichen - die Berechtigungsgruppen sind aktuell.'],
|
|
'left' => ['error', 'Der Account ist nicht mehr auf dem Discord-Server - das Konto ist deaktiviert, die Discord-Gruppen sind entzogen.'],
|
|
'revoked' => ['error', 'Die Discord-Anmeldung dieses Kontos ist abgelaufen. Abgeglichen wird wieder, sobald sich der Benutzer über Discord anmeldet.'],
|
|
'no_token' => ['error', 'Dieses Konto hat sich noch nie über Discord angemeldet - abgleichen lässt es sich erst nach der ersten Discord-Anmeldung.'],
|
|
'error' => ['error', 'Abgleich mit Discord fehlgeschlagen' . (discord_last_error() !== '' ? ': ' . discord_last_error() : '.')
|
|
. ' Bitte später erneut versuchen.'],
|
|
];
|
|
[$type, $message] = $messages[$result] ?? ['error', 'Abgleich fehlgeschlagen.'];
|
|
admin_log('🔄 Discord-Abgleich (ein Konto)', ['Benutzer' => admin_log_user($userId), 'Ergebnis' => $message]);
|
|
set_flash($type, $message);
|
|
redirect('/admin/user_form.php?id=' . $userId);
|
|
}
|
|
|
|
// Alle Konten mit Discord-Verknuepfung. Pro Konto zwei kurze Anfragen an Discord - bei
|
|
// einer Legion mit ein paar Dutzend Mitgliedern dauert das einige Sekunden.
|
|
if (function_exists('set_time_limit')) {
|
|
@set_time_limit(300);
|
|
}
|
|
|
|
$ids = db()->query('SELECT id FROM users WHERE discord_id IS NOT NULL ORDER BY id')->fetchAll(PDO::FETCH_COLUMN);
|
|
$counts = ['ok' => 0, 'left' => 0, 'revoked' => 0, 'no_token' => 0, 'error' => 0];
|
|
foreach ($ids as $id) {
|
|
$result = discord_live_check((int) $id, true);
|
|
$counts[$result] = ($counts[$result] ?? 0) + 1;
|
|
}
|
|
|
|
$parts = [$counts['ok'] . ' abgeglichen'];
|
|
if ($counts['left'] > 0) {
|
|
$parts[] = $counts['left'] . ' nicht mehr auf dem Server (deaktiviert, Discord-Gruppen entzogen)';
|
|
}
|
|
if ($counts['revoked'] > 0) {
|
|
$parts[] = $counts['revoked'] . ' mit abgelaufener Discord-Anmeldung';
|
|
}
|
|
if ($counts['no_token'] > 0) {
|
|
$parts[] = $counts['no_token'] . ' noch nie über Discord angemeldet';
|
|
}
|
|
if ($counts['error'] > 0) {
|
|
$parts[] = $counts['error'] . ' fehlgeschlagen (Discord nicht erreichbar)';
|
|
}
|
|
|
|
$onlyOk = $counts['left'] + $counts['revoked'] + $counts['no_token'] + $counts['error'] === 0;
|
|
admin_log('🔄 Discord-Abgleich (alle Konten)', ['Ergebnis' => implode(', ', $parts)]);
|
|
set_flash($onlyOk ? 'success' : 'info', 'Discord-Abgleich: ' . implode(', ', $parts) . '.');
|
|
redirect('/admin/users.php');
|