feat: Obtener roles de guild desde la API de Discord y actualizar la interfaz de configuración
This commit is contained in:
@@ -1167,6 +1167,30 @@ export const server = createServer(
|
|||||||
} catch {
|
} catch {
|
||||||
guildConfig = null;
|
guildConfig = null;
|
||||||
}
|
}
|
||||||
|
// Attempt to fetch guild roles via Discord Bot API if token available
|
||||||
|
let guildRoles: Array<{ id: string; name: string }> = [];
|
||||||
|
try {
|
||||||
|
const botToken = process.env.DISCORD_BOT_TOKEN;
|
||||||
|
if (botToken) {
|
||||||
|
const rolesRes = await fetch(
|
||||||
|
`https://discord.com/api/guilds/${encodeURIComponent(
|
||||||
|
String(guildId)
|
||||||
|
)}/roles`,
|
||||||
|
{ headers: { Authorization: `Bot ${botToken}` } }
|
||||||
|
);
|
||||||
|
if (rolesRes.ok) {
|
||||||
|
const rolesJson = await rolesRes.json();
|
||||||
|
if (Array.isArray(rolesJson)) {
|
||||||
|
guildRoles = rolesJson.map((r: any) => ({
|
||||||
|
id: String(r.id),
|
||||||
|
name: String(r.name || r.id),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
// ignore; fallback to no roles
|
||||||
|
}
|
||||||
// Render dashboard with selected guild context; show dashboard nav
|
// Render dashboard with selected guild context; show dashboard nav
|
||||||
await renderTemplate(req, res, "dashboard", {
|
await renderTemplate(req, res, "dashboard", {
|
||||||
appName: pkg.name ?? "Amayo Bot",
|
appName: pkg.name ?? "Amayo Bot",
|
||||||
@@ -1176,6 +1200,7 @@ export const server = createServer(
|
|||||||
selectedGuildId: guildId,
|
selectedGuildId: guildId,
|
||||||
selectedGuildName,
|
selectedGuildName,
|
||||||
guildConfig,
|
guildConfig,
|
||||||
|
guildRoles,
|
||||||
page,
|
page,
|
||||||
hideNavbar: false,
|
hideNavbar: false,
|
||||||
useDashboardNav: true,
|
useDashboardNav: true,
|
||||||
|
|||||||
@@ -58,8 +58,18 @@
|
|||||||
<textarea name="aiRolePrompt" id="aiRoleInput" rows="4" class="w-full rounded p-2 bg-transparent border border-white/6" placeholder="E.g. Actúa como un moderador amigable..."><%= (guildConfig && guildConfig.aiRolePrompt) || '' %></textarea>
|
<textarea name="aiRolePrompt" id="aiRoleInput" rows="4" class="w-full rounded p-2 bg-transparent border border-white/6" placeholder="E.g. Actúa como un moderador amigable..."><%= (guildConfig && guildConfig.aiRolePrompt) || '' %></textarea>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label class="block text-sm text-slate-200 mb-1">Roles de staff (IDs separadas por coma)</label>
|
<label class="block text-sm text-slate-200 mb-1">Roles de staff</label>
|
||||||
<input type="text" name="staff" id="staffInput" value="<%= (guildConfig && (Array.isArray(guildConfig.staff) ? guildConfig.staff.join(',') : guildConfig.staff)) || '' %>" class="w-full rounded p-2 bg-transparent border border-white/6" placeholder="123... , 456..." />
|
<% if (typeof guildRoles !== 'undefined' && guildRoles && guildRoles.length) { %>
|
||||||
|
<select id="staffSelect" name="staffSelect" multiple class="w-full rounded p-2 bg-transparent border border-white/6 h-36">
|
||||||
|
<% const selectedStaff = (guildConfig && Array.isArray(guildConfig.staff) ? guildConfig.staff.map(String) : (guildConfig && guildConfig.staff ? String(guildConfig.staff).split(',') : [])) || []; %>
|
||||||
|
<% guildRoles.forEach(r => { %>
|
||||||
|
<option value="<%= r.id %>" <%= selectedStaff.includes(String(r.id)) ? 'selected' : '' %>><%= r.name %> — <%= r.id %></option>
|
||||||
|
<% }) %>
|
||||||
|
</select>
|
||||||
|
<% } else { %>
|
||||||
|
<input type="text" name="staff" id="staffInput" value="<%= (guildConfig && (Array.isArray(guildConfig.staff) ? guildConfig.staff.join(',') : guildConfig.staff)) || '' %>" class="w-full rounded p-2 bg-transparent border border-white/6" placeholder="123... , 456..." />
|
||||||
|
<div class="text-xs text-slate-300 mt-1">No se pudo obtener roles desde la API. Introduce IDs manualmente separadas por coma.</div>
|
||||||
|
<% } %>
|
||||||
</div>
|
</div>
|
||||||
<div class="flex items-center gap-3">
|
<div class="flex items-center gap-3">
|
||||||
<button type="submit" class="pixel-btn">Guardar</button>
|
<button type="submit" class="pixel-btn">Guardar</button>
|
||||||
@@ -72,17 +82,20 @@
|
|||||||
(function(){
|
(function(){
|
||||||
const form = document.getElementById('guildSettingsForm');
|
const form = document.getElementById('guildSettingsForm');
|
||||||
const status = document.getElementById('saveStatus');
|
const status = document.getElementById('saveStatus');
|
||||||
form.addEventListener('submit', async (e)=>{
|
form.addEventListener('submit', async (e)=>{
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
status.textContent = 'Guardando...';
|
status.textContent = 'Guardando...';
|
||||||
const prefix = document.getElementById('prefixInput').value.trim();
|
const prefix = document.getElementById('prefixInput').value.trim();
|
||||||
const aiRolePrompt = document.getElementById('aiRoleInput').value.trim();
|
const aiRolePrompt = document.getElementById('aiRoleInput').value.trim();
|
||||||
const staffRaw = document.getElementById('staffInput').value.trim();
|
let staffArr = [];
|
||||||
const payload = {
|
const staffSelect = document.getElementById('staffSelect');
|
||||||
prefix: prefix,
|
if (staffSelect) {
|
||||||
aiRolePrompt: aiRolePrompt.length ? aiRolePrompt : null,
|
for (const o of staffSelect.selectedOptions) staffArr.push(o.value);
|
||||||
staff: staffRaw ? staffRaw.split(',').map(s=>s.trim()).filter(Boolean) : [],
|
} else {
|
||||||
};
|
const staffRaw = (document.getElementById('staffInput') && document.getElementById('staffInput').value) ? document.getElementById('staffInput').value.trim() : '';
|
||||||
|
staffArr = staffRaw ? staffRaw.split(',').map(s=>s.trim()).filter(Boolean) : [];
|
||||||
|
}
|
||||||
|
const payload = { prefix: prefix, aiRolePrompt: aiRolePrompt.length ? aiRolePrompt : null, staff: staffArr };
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`/api/dashboard/${encodeURIComponent('<%= selectedGuild %>')}/settings`, {
|
const res = await fetch(`/api/dashboard/${encodeURIComponent('<%= selectedGuild %>')}/settings`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|||||||
Reference in New Issue
Block a user