/* Editor fuer einen Fragebogen: Fragen-Baukasten, Ausloeser, Start-Knopf, Abschluss. */ (function () { 'use strict'; const raw = document.getElementById('qn-data'); if (!raw) return; const { q, preset, types, guild, user } = JSON.parse(raw.textContent); const { el } = SielEditor; const form = document.getElementById('qn-form'); const roles = guild.roles.filter((r) => !r.managed); const roleById = new Map(guild.roles.map((r) => [r.id, r])); const hex6 = () => Math.random().toString(16).slice(2, 8).padEnd(6, '0'); // --- Ausgangslage: gespeicherter Fragebogen oder Vorlage function fromPreset(name) { const base = { name: '', enabled: false, intro: '', outro: '', questions: [], add_roles: [], remove_roles: [], log_channel: null, allow_retake: false, trigger_join: false, trigger_role: null, button: { channel: null, label: 'Fragebogen starten', emoji: '📝', message: null } }; if (name !== 'vorstellung') { base.questions = [{ id: hex6(), type: 'choice', text: '', description: '', required: true, max: 1, options: [{ label: '', emoji: '', description: '', roles: [] }, { label: '', emoji: '', description: '', roles: [] }] }]; return base; } const classes = [['templar', 'Templer', '🛡️'], ['gladiator', 'Gladiator', '⚔️'], ['assassin', 'Assassine', '🗡️'], ['ranger', 'Jäger', '🏹'], ['sorcerer', 'Magier', '🔥'], ['spiritmaster', 'Beschwörer', '🌀'], ['cleric', 'Kleriker', '✨'], ['chanter', 'Kantor', '🎶']]; const lead = /^((?:\p{Regional_Indicator}{2})|(?:\p{Extended_Pictographic}️?))\s*/u; const langs = roles.filter((r) => /^\p{Regional_Indicator}{2}/u.test(r.name)); const verified = roles.find((r) => /verifiziert|verified/i.test(r.name) && !/un-?verifi/i.test(r.name)); const textCh = guild.channels.filter((c) => c.type === 'text'); const log = textCh.find((c) => /verifizierungs-?log/i.test(c.name)) || textCh.find((c) => /log/i.test(c.name)); return Object.assign(base, { name: 'Vorstellung', intro: 'Schön, dass du da bist! Drei kurze Fragen, damit wir dich besser kennenlernen – danach bekommst du die passenden Rollen.', outro: 'Danke! Viel Spaß in der Elyos Allianz. 🪽', trigger_role: verified ? verified.id : null, log_channel: log ? log.id : null, questions: [ { id: hex6(), type: 'multi', text: 'Welche Klassen spielst du?', description: '', required: true, max: 3, options: classes.map(([key, label, emoji]) => { const r = roles.find((x) => x.name.toLowerCase().includes(key)); return { label, emoji, description: '', roles: r ? [r.id] : [] }; }) }, { id: hex6(), type: 'multi', text: 'Welche Sprachen sprichst du?', description: '', required: false, max: Math.max(1, langs.length), options: langs.length ? langs.map((r) => { const m = r.name.match(lead); return { label: m ? r.name.slice(m[0].length) : r.name, emoji: m ? m[1] : '', description: '', roles: [r.id] }; }) : [{ label: 'Deutsch', emoji: '🇩🇪', description: '', roles: [] }, { label: 'English', emoji: '🇬🇧', description: '', roles: [] }] }, { id: hex6(), type: 'paragraph', text: 'Erzähl kurz etwas über dich', description: 'z. B. Legion, Spielzeiten, was dir Spaß macht', required: false, max: 1, options: [] }, ], }); } const start = q || fromPreset(preset || 'leer'); const state = JSON.parse(JSON.stringify(start)); state.questions = (state.questions || []).map((x) => Object.assign({ id: hex6(), type: 'choice', text: '', description: '', required: true, max: 1, options: [] }, x)); state.button = Object.assign({ channel: null, label: 'Fragebogen starten', emoji: '📝', message: null }, state.button || {}); // --- Allgemein const bind = (id, key, prop = 'value') => { const e = document.getElementById(id); e[prop] = state[key] ?? (prop === 'checked' ? false : ''); e.addEventListener(prop === 'checked' ? 'change' : 'input', () => { state[key] = e[prop]; }); }; bind('qn-name', 'name'); bind('qn-intro', 'intro'); bind('qn-outro', 'outro'); bind('qn-enabled', 'enabled', 'checked'); bind('qn-join', 'trigger_join', 'checked'); bind('qn-retake', 'allow_retake', 'checked'); const trig = document.getElementById('qn-trigger-role'); trig.value = state.trigger_role || ''; trig.addEventListener('change', () => { state.trigger_role = trig.value || null; }); form.elements.log_channel.value = state.log_channel || ''; form.elements.button_channel.value = state.button.channel || ''; const bl = document.getElementById('qn-button-label'); bl.value = state.button.label || ''; const be = document.getElementById('qn-button-emoji'); be.value = state.button.emoji || ''; function roleChecks(box, selected) { for (const r of roles) { const cb = el('input', { type: 'checkbox', value: r.id }); cb.checked = selected.includes(r.id); if (!r.assignable && !cb.checked) cb.disabled = true; const dot = el('span', { class: 'role-dot' }); if (r.color) dot.style.background = r.color; box.append(el('label', { class: 'role-item' + (r.assignable ? '' : ' disabled') }, cb, dot, r.name)); } return () => Array.from(box.querySelectorAll('input:checked')).map((i) => i.value); } const getAdd = roleChecks(document.getElementById('qn-add-roles'), state.add_roles || []); const getRemove = roleChecks(document.getElementById('qn-remove-roles'), state.remove_roles || []); // --- Nachricht mit Start-Knopf const defaultMsg = { mode: 'embed', content: '', embed: { color: '#c9a227', title: '📝 ' + (state.name || 'Fragebogen'), description: state.intro || 'Klick auf den Knopf, um zu starten.', fields: [], author: '', image: '', thumbnail: '', footer: '' } }; const buttonEditor = SielEditor.mount(document.getElementById('qn-button-editor'), state.button.message && (state.button.message.content || (state.button.message.embed || {}).title || (state.button.message.embed || {}).description) ? state.button.message : defaultMsg, { botName: 'Siel', botAvatar: guild.bot_avatar || '/static/img/favicon-180.png', userName: user.name, userAvatar: user.avatar || '', guildName: guild.name, memberCount: guild.member_count || 1, channelName: (id) => (guild.channels.find((c) => c.id === id) || {}).name, roleName: (id) => (roleById.get(id) || {}).name, extra: () => el('div', { class: 'dc-buttons' }, el('span', { class: 'dc-button dc-button-primary' }, el('span', { text: be.value || '📝' }), el('span', { text: bl.value || 'Fragebogen starten' }))), }); bl.addEventListener('input', () => buttonEditor.redraw()); be.addEventListener('input', () => buttonEditor.redraw()); // --- Fragen const box = document.getElementById('qn-questions'); const addBtn = document.getElementById('qn-add'); let open = 0; function optionRow(qq, o, idx) { const emoji = el('input', { type: 'text', class: 'emoji-input', maxlength: '80', placeholder: '🙂' }); emoji.value = o.emoji || ''; emoji.addEventListener('input', () => { o.emoji = emoji.value.trim(); }); const label = el('input', { type: 'text', maxlength: '80', placeholder: 'Antwort ' + (idx + 1) }); label.value = o.label || ''; label.addEventListener('input', () => { o.label = label.value; summary.textContent = rolesText(); }); const desc = el('input', { type: 'text', maxlength: '100', placeholder: 'Beschreibung (optional)' }); desc.value = o.description || ''; desc.addEventListener('input', () => { o.description = desc.value; }); const rolesBox = el('div', { class: 'role-list role-list-sm' }); o.roles = o.roles || []; const rolesText = () => o.roles.length ? '🎭 ' + o.roles.map((id) => (roleById.get(id) || {}).name || '?').join(', ') : 'Keine Rolle'; for (const r of roles) { const cb = el('input', { type: 'checkbox', value: r.id }); cb.checked = o.roles.includes(r.id); if (!r.assignable && !cb.checked) cb.disabled = true; cb.addEventListener('change', () => { o.roles = Array.from(rolesBox.querySelectorAll('input:checked')).map((i) => i.value); summary.textContent = rolesText(); }); rolesBox.append(el('label', { class: 'role-item' + (r.assignable ? '' : ' disabled') }, cb, r.name)); } const summary = el('summary', { text: rolesText() }); const details = el('details', { class: 'qn-roles' }, summary, rolesBox); const remove = el('button', { type: 'button', class: 'icon-btn danger', text: '✕', title: 'Antwort entfernen', disabled: qq.options.length <= 2, onclick: () => { qq.options.splice(idx, 1); paint(); } }); return el('div', { class: 'qn-option' }, emoji, label, desc, details, remove); } function paint() { box.textContent = ''; state.questions.forEach((qq, idx) => { const typeSel = el('select', {}); for (const [k, l] of Object.entries(types)) typeSel.append(el('option', { value: k, text: l })); typeSel.value = qq.type; typeSel.addEventListener('change', () => { qq.type = typeSel.value; if ((qq.type === 'choice' || qq.type === 'multi') && qq.options.length < 2) { while (qq.options.length < 2) qq.options.push({ label: '', emoji: '', description: '', roles: [] }); } paint(); }); const text = el('input', { type: 'text', maxlength: '200', placeholder: 'Frage' }); text.value = qq.text; text.addEventListener('input', () => { qq.text = text.value; head.querySelector('.qn-q-title').textContent = qq.text || 'Neue Frage'; }); const desc = el('input', { type: 'text', maxlength: '500', placeholder: 'Hinweis unter der Frage (optional)' }); desc.value = qq.description || ''; desc.addEventListener('input', () => { qq.description = desc.value; }); const req = el('input', { type: 'checkbox' }); req.checked = qq.required; req.addEventListener('change', () => { qq.required = req.checked; }); const head = el('div', { class: 'tk-type-head' }, el('button', { type: 'button', class: 'icon-btn', text: '↑', title: 'Nach oben', disabled: idx === 0, onclick: () => { state.questions.splice(idx - 1, 0, state.questions.splice(idx, 1)[0]); open = idx - 1; paint(); } }), el('button', { type: 'button', class: 'qn-q-toggle', onclick: () => { open = open === idx ? -1 : idx; paint(); } }, el('span', { class: 'qn-q-num', text: String(idx + 1) }), el('span', { class: 'qn-q-title', text: qq.text || 'Neue Frage' }), el('span', { class: 'muted small', text: types[qq.type] })), el('button', { type: 'button', class: 'icon-btn danger', text: '✕', title: 'Frage entfernen', disabled: state.questions.length <= 1, onclick: () => { if (confirm('Frage entfernen?')) { state.questions.splice(idx, 1); open = -1; paint(); } } })); const card = el('div', { class: 'tk-type' + (open === idx ? ' open' : '') }, head); if (open === idx) { const bodyEl = el('div', { class: 'tk-type-body' }, el('div', { class: 'field-row' }, el('label', { class: 'field' }, el('span', { text: 'Art' }), typeSel), el('label', { class: 'check qn-req' }, req, ' Pflichtfrage')), el('label', { class: 'field field-wide' }, el('span', { text: 'Frage' }), text), el('label', { class: 'field field-wide' }, el('span', { text: 'Hinweis' }), desc)); if (qq.type === 'multi') { const max = el('input', { type: 'number', min: '1', max: String(Math.max(1, qq.options.length)), class: 'input-sm' }); max.value = qq.max || qq.options.length; max.addEventListener('input', () => { qq.max = Number(max.value) || 1; }); bodyEl.append(el('label', { class: 'field' }, el('span', { text: 'Höchstens so viele Antworten' }), max)); } if (qq.type === 'choice' || qq.type === 'multi') { const opts = el('div', { class: 'qn-options' }); qq.options.forEach((o, j) => opts.append(optionRow(qq, o, j))); const addOpt = el('button', { type: 'button', class: 'btn btn-sm btn-ghost', text: '+ Antwort hinzufügen', hidden: qq.options.length >= 25, onclick: () => { qq.options.push({ label: '', emoji: '', description: '', roles: [] }); paint(); } }); bodyEl.append(el('div', { class: 'field field-wide' }, el('span', { text: 'Antworten – die Rollen einer Antwort bekommt, wer sie wählt' }), opts, addOpt)); } card.append(bodyEl); } box.append(card); }); addBtn.hidden = state.questions.length >= 10; } addBtn.addEventListener('click', () => { state.questions.push({ id: hex6(), type: 'choice', text: '', description: '', required: true, max: 1, options: [{ label: '', emoji: '', description: '', roles: [] }, { label: '', emoji: '', description: '', roles: [] }] }); open = state.questions.length - 1; paint(); }); paint(); // --- Speichern let dirty = false; const note = document.getElementById('dirty-note'); const mark = () => { dirty = true; note.textContent = 'Ungespeicherte Änderungen'; }; form.addEventListener('input', mark); form.addEventListener('change', mark); box.addEventListener('click', (e) => { if (e.target.closest('button')) mark(); }); window.addEventListener('beforeunload', (ev) => { if (dirty) { ev.preventDefault(); ev.returnValue = ''; } }); form.addEventListener('submit', () => { document.getElementById('qn-payload').value = JSON.stringify({ name: state.name, enabled: document.getElementById('qn-enabled').checked, intro: state.intro, outro: state.outro, questions: state.questions, add_roles: getAdd(), remove_roles: getRemove(), log_channel: form.elements.log_channel.value || null, allow_retake: document.getElementById('qn-retake').checked, trigger_join: document.getElementById('qn-join').checked, trigger_role: trig.value || null, button: { channel: form.elements.button_channel.value || null, label: bl.value, emoji: be.value.trim(), message: buttonEditor.get() }, }); dirty = false; }); })();