Add raid creation from the website and handle auto-repost on save
The bot API now lets the website create raids without knowing a
Discord channel: POST /api/raids accepts guild_id and resolves the
target channel via the server's /set_raidchannel default. Adds a
"+ Neuer Raid" panel on raids.php (raids.manage only) with the same
title/date/max slots/gearscore grid and requirements textarea as the
edit form, plus the group-name row editor reused without its lock
toggle (data-locks="0" on .group-editor, since creation doesn't
support locked_groups) - one shared component instead of a second
implementation. Needs raids_api.guild_id in config.php, documented in
README.md.
Also updates the editor's "Speichern" flow for the API's other
change: PATCH /api/raids/{id} now auto-reposts the Discord message
(via sync_raid_message) if it was deleted manually, returning the new
raid_id. update_raid() now returns the API response instead of
discarding it, and raid_edit_action.php surfaces a distinct message
when the id changed - the page reload right after already picks up
the new id either way, since it re-fetches the raid list fresh.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
4d4a030b95
commit
40d8bec259
8 changed files with 211 additions and 11 deletions
24
README.md
24
README.md
|
|
@ -139,6 +139,30 @@ das schon unterstützt. Zusätzlich gibt es die neue Berechtigung `raids.manage`
|
||||||
`raids.view` nachträglich unter Berechtigungsgruppen angelegt/zugewiesen werden muss, falls dein
|
`raids.view` nachträglich unter Berechtigungsgruppen angelegt/zugewiesen werden muss, falls dein
|
||||||
Account vor diesem Update installiert wurde.
|
Account vor diesem Update installiert wurde.
|
||||||
|
|
||||||
|
### Raids über die Website erstellen
|
||||||
|
|
||||||
|
Mitglieder mit `raids.manage` können über den Button "+ Neuer Raid" auf der Raids-Seite direkt
|
||||||
|
einen neuen Raid anlegen (Titel, Datum, Anforderungen, Gruppen, Gearscore, Max. Spieler) - ganz
|
||||||
|
ohne eine Discord-Kanal-ID zu kennen. Die API postet die Raid-Nachricht automatisch in den
|
||||||
|
Standard-Raid-Kanal des Servers, den du einmalig im Bot mit `/set_raidchannel` festlegst (oder
|
||||||
|
über `PATCH /api/settings/{guild_id}` mit `{"raid_channel": ...}`).
|
||||||
|
|
||||||
|
Dafür in `config.php` unter `raids_api` die Discord-**Server**-ID (nicht Kanal-ID) ergänzen:
|
||||||
|
```php
|
||||||
|
'raids_api' => [
|
||||||
|
'url' => 'https://api.maaxxs-coding.de/api/raids',
|
||||||
|
'key' => 'dein-api-key',
|
||||||
|
'guild_id' => 123456789012345678,
|
||||||
|
],
|
||||||
|
```
|
||||||
|
Ohne `/set_raidchannel` im Bot bzw. `guild_id` in `config.php` schlägt das Erstellen mit einer
|
||||||
|
verständlichen Fehlermeldung fehl - alle anderen Funktionen der Raids-Seite sind davon unabhängig.
|
||||||
|
|
||||||
|
Beim Bearbeiten eines Raids postet die API die Discord-Nachricht außerdem automatisch neu, falls
|
||||||
|
sie zwischenzeitlich manuell gelöscht wurde (die `raid_id` entspricht der Discord-Message-ID und
|
||||||
|
ändert sich in diesem Fall) - die Website zeigt das nach dem Speichern im Editor-Modus an und lädt
|
||||||
|
danach neu, sodass immer der aktuelle Stand sichtbar ist.
|
||||||
|
|
||||||
### Premades
|
### Premades
|
||||||
|
|
||||||
Bildet die Premade-Verwaltung des Discord-Bots (`/premade ...`) auf der Website nach: Gründen,
|
Bildet die Premade-Verwaltung des Discord-Bots (`/premade ...`) auf der Website nach: Gründen,
|
||||||
|
|
|
||||||
|
|
@ -400,6 +400,7 @@ input:focus, select:focus, textarea:focus {
|
||||||
.flash-error { background: rgba(217, 83, 79, 0.15); border: 1px solid var(--danger); color: var(--danger); }
|
.flash-error { background: rgba(217, 83, 79, 0.15); border: 1px solid var(--danger); color: var(--danger); }
|
||||||
|
|
||||||
.text-muted { color: var(--text-muted); }
|
.text-muted { color: var(--text-muted); }
|
||||||
|
.text-muted.error { color: var(--danger); }
|
||||||
|
|
||||||
.hero {
|
.hero {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
|
|
|
||||||
|
|
@ -192,23 +192,28 @@
|
||||||
});
|
});
|
||||||
|
|
||||||
// ---- Gruppen-Editor: Zeilen hinzufuegen/entfernen ----
|
// ---- Gruppen-Editor: Zeilen hinzufuegen/entfernen ----
|
||||||
function createGroupEditorRow(name) {
|
// withLock=false blendet den Sperren-Toggle aus (z.B. beim Erstellen eines neuen
|
||||||
|
// Raids, wo "Gruppen sperren" von der API noch nicht unterstuetzt wird).
|
||||||
|
function createGroupEditorRow(name, withLock) {
|
||||||
var row = document.createElement('div');
|
var row = document.createElement('div');
|
||||||
row.className = 'group-editor-row';
|
row.className = 'group-editor-row';
|
||||||
row.innerHTML =
|
var html = '<input type="text" class="group-editor-name" placeholder="Gruppenname">';
|
||||||
'<input type="text" class="group-editor-name" placeholder="Gruppenname">' +
|
if (withLock) {
|
||||||
'<label class="group-editor-lock" title="Gruppe sperren"><input type="checkbox" class="group-editor-lock-checkbox">' + ICON_LOCK + '</label>' +
|
html += '<label class="group-editor-lock" title="Gruppe sperren"><input type="checkbox" class="group-editor-lock-checkbox">' + ICON_LOCK + '</label>';
|
||||||
'<button type="button" class="icon-button group-editor-remove" title="Gruppe entfernen">' + ICON_X + '</button>';
|
}
|
||||||
|
html += '<button type="button" class="icon-button group-editor-remove" title="Gruppe entfernen">' + ICON_X + '</button>';
|
||||||
|
row.innerHTML = html;
|
||||||
row.querySelector('.group-editor-name').value = name || '';
|
row.querySelector('.group-editor-name').value = name || '';
|
||||||
return row;
|
return row;
|
||||||
}
|
}
|
||||||
|
|
||||||
document.querySelectorAll('.group-editor').forEach(function (editor) {
|
document.querySelectorAll('.group-editor').forEach(function (editor) {
|
||||||
|
var withLock = editor.getAttribute('data-locks') !== '0';
|
||||||
var rows = editor.querySelector('.group-editor-rows');
|
var rows = editor.querySelector('.group-editor-rows');
|
||||||
var addBtn = editor.querySelector('.group-editor-add');
|
var addBtn = editor.querySelector('.group-editor-add');
|
||||||
|
|
||||||
addBtn.addEventListener('click', function () {
|
addBtn.addEventListener('click', function () {
|
||||||
var row = createGroupEditorRow('');
|
var row = createGroupEditorRow('', withLock);
|
||||||
rows.appendChild(row);
|
rows.appendChild(row);
|
||||||
row.querySelector('.group-editor-name').focus();
|
row.querySelector('.group-editor-name').focus();
|
||||||
});
|
});
|
||||||
|
|
@ -226,6 +231,52 @@
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ---- Neuen Raid erstellen ----
|
||||||
|
var raidCreateToggle = document.getElementById('raid-create-toggle');
|
||||||
|
var raidCreatePanel = document.getElementById('raid-create-panel');
|
||||||
|
if (raidCreateToggle && raidCreatePanel) {
|
||||||
|
raidCreateToggle.addEventListener('click', function () {
|
||||||
|
raidCreatePanel.style.display = raidCreatePanel.style.display === 'none' ? 'block' : 'none';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
var raidCreateForm = document.getElementById('raid-create-form');
|
||||||
|
if (raidCreateForm) {
|
||||||
|
raidCreateForm.addEventListener('submit', function (e) {
|
||||||
|
e.preventDefault();
|
||||||
|
var feedback = raidCreateForm.querySelector('.raid-create-feedback');
|
||||||
|
|
||||||
|
var groupNames = [];
|
||||||
|
raidCreateForm.querySelectorAll('.group-editor-row').forEach(function (row) {
|
||||||
|
var name = row.querySelector('.group-editor-name').value.trim();
|
||||||
|
if (name !== '') groupNames.push(name);
|
||||||
|
});
|
||||||
|
if (groupNames.length === 0) {
|
||||||
|
feedback.textContent = 'Bitte mindestens eine Gruppe angeben.';
|
||||||
|
feedback.className = 'raid-create-feedback text-muted error';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
feedback.textContent = 'Wird erstellt …';
|
||||||
|
feedback.className = 'raid-create-feedback text-muted';
|
||||||
|
postJson('/raid_create_action.php', {
|
||||||
|
title: raidCreateForm.title.value,
|
||||||
|
date_raw: raidCreateForm.date_raw.value,
|
||||||
|
requirements: raidCreateForm.requirements.value,
|
||||||
|
gearscore: raidCreateForm.gearscore.value,
|
||||||
|
group_names: groupNames.join(', '),
|
||||||
|
max_slots: raidCreateForm.max_slots.value
|
||||||
|
}).then(function (result) {
|
||||||
|
feedback.textContent = result.message || '';
|
||||||
|
if (result.status === 'success') {
|
||||||
|
setTimeout(function () { window.location.reload(); }, 900);
|
||||||
|
}
|
||||||
|
}).catch(function () {
|
||||||
|
feedback.textContent = 'Verbindung fehlgeschlagen.';
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// ---- Premade einladen ----
|
// ---- Premade einladen ----
|
||||||
document.querySelectorAll('.raid-premade-form').forEach(function (form) {
|
document.querySelectorAll('.raid-premade-form').forEach(function (form) {
|
||||||
form.addEventListener('submit', function (e) {
|
form.addEventListener('submit', function (e) {
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,10 @@ return [
|
||||||
'raids_api' => [
|
'raids_api' => [
|
||||||
'url' => 'https://api.maaxxs-coding.de/api/raids',
|
'url' => 'https://api.maaxxs-coding.de/api/raids',
|
||||||
'key' => 'dein-api-key',
|
'key' => 'dein-api-key',
|
||||||
|
// Noetig, damit die Website selbst Raids erstellen kann: die API postet sie
|
||||||
|
// automatisch in den per /set_raidchannel im Bot konfigurierten Standard-Raid-Kanal
|
||||||
|
// dieses Servers. Ohne raid_channel-Konfiguration im Bot schlaegt das Erstellen fehl.
|
||||||
|
'guild_id' => 'deine-discord-server-id',
|
||||||
],
|
],
|
||||||
'discord' => [
|
'discord' => [
|
||||||
'client_id' => 'deine-discord-client-id',
|
'client_id' => 'deine-discord-client-id',
|
||||||
|
|
|
||||||
|
|
@ -167,11 +167,42 @@ function move_signup(int $raidId, int $discordId, int $group): void
|
||||||
* @param array<string, mixed> $fields Nur die zu aendernden Felder, z.B.
|
* @param array<string, mixed> $fields Nur die zu aendernden Felder, z.B.
|
||||||
* ['title' => ..., 'date_raw' => ..., 'requirements' => ..., 'gearscore' => ...,
|
* ['title' => ..., 'date_raw' => ..., 'requirements' => ..., 'gearscore' => ...,
|
||||||
* 'group_names' => ..., 'max_slots' => ..., 'locked_groups' => [1, 3]]
|
* 'group_names' => ..., 'max_slots' => ..., 'locked_groups' => [1, 3]]
|
||||||
|
* @return array<string, mixed> Antwort der API. Enthaelt die aktuell gueltige raid_id -
|
||||||
|
* die API postet die Discord-Nachricht automatisch neu, falls sie zwischenzeitlich
|
||||||
|
* manuell geloescht wurde, wobei sich die raid_id (= Discord-Message-ID) aendert.
|
||||||
* @throws RuntimeException
|
* @throws RuntimeException
|
||||||
*/
|
*/
|
||||||
function update_raid(int $raidId, array $fields): void
|
function update_raid(int $raidId, array $fields): array
|
||||||
{
|
{
|
||||||
raids_api_request('PATCH', '/' . $raidId, [], $fields);
|
return raids_api_request('PATCH', '/' . $raidId, [], $fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erstellt einen neuen Raid ueber den in config.php (raids_api.guild_id) hinterlegten
|
||||||
|
* Discord-Server. Die API postet ihn automatisch in den per /set_raidchannel im Bot
|
||||||
|
* konfigurierten Standard-Raid-Kanal dieses Servers - die Website muss dafuer keine
|
||||||
|
* Kanal-ID kennen/verwalten.
|
||||||
|
*
|
||||||
|
* @param array<string, mixed> $fields title, date_raw, requirements, group_names
|
||||||
|
* (Pflicht), max_slots, gearscore (optional)
|
||||||
|
* @return array<string, mixed> Antwort der API inkl. raid_id
|
||||||
|
* @throws RuntimeException
|
||||||
|
*/
|
||||||
|
function create_raid(array $fields): array
|
||||||
|
{
|
||||||
|
$configPath = __DIR__ . '/../config.php';
|
||||||
|
if (!file_exists($configPath)) {
|
||||||
|
throw new RuntimeException('Konfigurationsdatei fehlt.');
|
||||||
|
}
|
||||||
|
$config = require $configPath;
|
||||||
|
$guildId = $config['raids_api']['guild_id'] ?? null;
|
||||||
|
if (!$guildId) {
|
||||||
|
throw new RuntimeException('Raid-Erstellung ist nicht konfiguriert (config.php: raids_api.guild_id fehlt).');
|
||||||
|
}
|
||||||
|
|
||||||
|
$fields['guild_id'] = (int) $guildId;
|
||||||
|
|
||||||
|
return raids_api_request('POST', '', [], $fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @throws RuntimeException */
|
/** @throws RuntimeException */
|
||||||
|
|
|
||||||
32
raid_create_action.php
Normal file
32
raid_create_action.php
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/includes/bootstrap.php';
|
||||||
|
require_once __DIR__ . '/includes/raids_api.php';
|
||||||
|
require_permission('raids.manage');
|
||||||
|
|
||||||
|
$input = read_json_body();
|
||||||
|
verify_csrf_token($input['csrf_token'] ?? '');
|
||||||
|
|
||||||
|
$title = trim((string) ($input['title'] ?? ''));
|
||||||
|
$dateRaw = trim((string) ($input['date_raw'] ?? ''));
|
||||||
|
$requirements = trim((string) ($input['requirements'] ?? ''));
|
||||||
|
$groupNames = trim((string) ($input['group_names'] ?? ''));
|
||||||
|
$gearscore = trim((string) ($input['gearscore'] ?? ''));
|
||||||
|
$maxSlots = isset($input['max_slots']) && is_numeric($input['max_slots']) ? (int) $input['max_slots'] : 12;
|
||||||
|
|
||||||
|
if ($title === '' || $dateRaw === '' || $requirements === '' || $groupNames === '') {
|
||||||
|
json_response(false, 'Bitte Titel, Datum, Anforderungen und mindestens eine Gruppe angeben.');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$result = create_raid([
|
||||||
|
'title' => $title,
|
||||||
|
'date_raw' => $dateRaw,
|
||||||
|
'requirements' => $requirements,
|
||||||
|
'group_names' => $groupNames,
|
||||||
|
'gearscore' => $gearscore,
|
||||||
|
'max_slots' => $maxSlots,
|
||||||
|
]);
|
||||||
|
json_response(true, 'Raid wurde erstellt.', ['raid_id' => $result['raid_id'] ?? null]);
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
json_response(false, 'Erstellen fehlgeschlagen: ' . $e->getMessage());
|
||||||
|
}
|
||||||
|
|
@ -29,8 +29,16 @@ if (empty($fields)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
update_raid($raidId, $fields);
|
$result = update_raid($raidId, $fields);
|
||||||
json_response(true, 'Raid wurde aktualisiert.');
|
$newRaidId = (int) ($result['raid_id'] ?? $raidId);
|
||||||
|
|
||||||
|
// Die API postet die Discord-Nachricht automatisch neu, falls sie zwischenzeitlich
|
||||||
|
// manuell geloescht wurde - die raid_id (= Discord-Message-ID) aendert sich dann.
|
||||||
|
if ($newRaidId !== $raidId) {
|
||||||
|
json_response(true, 'Raid wurde aktualisiert. Die Discord-Nachricht war nicht mehr auffindbar und wurde neu gepostet.', ['raid_id' => $newRaidId]);
|
||||||
|
} else {
|
||||||
|
json_response(true, 'Raid wurde aktualisiert.', ['raid_id' => $newRaidId]);
|
||||||
|
}
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
json_response(false, 'Speichern fehlgeschlagen: ' . $e->getMessage());
|
json_response(false, 'Speichern fehlgeschlagen: ' . $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
49
raids.php
49
raids.php
|
|
@ -77,7 +77,12 @@ $pageTitle = 'Raids';
|
||||||
include __DIR__ . '/includes/header.php';
|
include __DIR__ . '/includes/header.php';
|
||||||
?>
|
?>
|
||||||
<div class="panel">
|
<div class="panel">
|
||||||
|
<div class="raid-header">
|
||||||
<h1>Raids</h1>
|
<h1>Raids</h1>
|
||||||
|
<?php if ($canManageRaids): ?>
|
||||||
|
<button type="button" class="button" id="raid-create-toggle">+ Neuer Raid</button>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
<p class="text-muted">Raid-Termine deiner Gilde.</p>
|
<p class="text-muted">Raid-Termine deiner Gilde.</p>
|
||||||
<div class="filter-tabs">
|
<div class="filter-tabs">
|
||||||
<?php foreach ($statusLabels as $value => $label): ?>
|
<?php foreach ($statusLabels as $value => $label): ?>
|
||||||
|
|
@ -86,6 +91,50 @@ include __DIR__ . '/includes/header.php';
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<?php if ($canManageRaids): ?>
|
||||||
|
<div class="panel" id="raid-create-panel" style="display:none;">
|
||||||
|
<h2>Neuen Raid erstellen</h2>
|
||||||
|
<form id="raid-create-form">
|
||||||
|
<div class="raid-edit-grid">
|
||||||
|
<div>
|
||||||
|
<label>Titel</label>
|
||||||
|
<input type="text" name="title" placeholder="z. B. Zauberreum HM" required>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label>Datum & Uhrzeit</label>
|
||||||
|
<input type="text" name="date_raw" placeholder="TT.MM.JJJJ HH:MM" required>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label>Max. Spieler</label>
|
||||||
|
<input type="text" name="max_slots" value="12">
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label>Gearscore</label>
|
||||||
|
<input type="text" name="gearscore" placeholder="optional">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="raid-edit-requirements">
|
||||||
|
<label>Anforderungen</label>
|
||||||
|
<textarea name="requirements" rows="3" placeholder="Setup / Rollenvorgabe" required></textarea>
|
||||||
|
</div>
|
||||||
|
<div class="group-editor" data-locks="0">
|
||||||
|
<label>Gruppen</label>
|
||||||
|
<div class="group-editor-rows">
|
||||||
|
<div class="group-editor-row">
|
||||||
|
<input type="text" class="group-editor-name" placeholder="Gruppe 1">
|
||||||
|
<button type="button" class="icon-button group-editor-remove" title="Gruppe entfernen"><?= icon('x') ?></button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<button type="button" class="button-secondary group-editor-add">+ Gruppe hinzufügen</button>
|
||||||
|
</div>
|
||||||
|
<div class="actions">
|
||||||
|
<button type="submit">Raid erstellen</button>
|
||||||
|
<span class="raid-create-feedback text-muted"></span>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
<?php if ($apiError): ?>
|
<?php if ($apiError): ?>
|
||||||
<div class="panel">
|
<div class="panel">
|
||||||
<div class="flash flash-error">Raid-Daten konnten nicht geladen werden: <?= e($apiError) ?></div>
|
<div class="flash flash-error">Raid-Daten konnten nicht geladen werden: <?= e($apiError) ?></div>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue