feat: Agregar lógica para verificar la presencia del bot en los gremios y actualizar la interfaz de usuario
This commit is contained in:
@@ -1418,6 +1418,29 @@ export const server = createServer(
|
|||||||
// ignore; fallback to no roles
|
// ignore; fallback to no roles
|
||||||
}
|
}
|
||||||
// Render dashboard with selected guild context; show dashboard nav
|
// Render dashboard with selected guild context; show dashboard nav
|
||||||
|
// Ensure we know whether the bot is in each guild (so small selectors/nav show correct state)
|
||||||
|
try {
|
||||||
|
const botToken = process.env.DISCORD_BOT_TOKEN ?? process.env.TOKEN;
|
||||||
|
if (botToken && Array.isArray(guilds) && guilds.length) {
|
||||||
|
await Promise.all(
|
||||||
|
guilds.map(async (g: any) => {
|
||||||
|
try {
|
||||||
|
const check = await fetch(
|
||||||
|
`https://discord.com/api/guilds/${encodeURIComponent(
|
||||||
|
String(g.id)
|
||||||
|
)}`,
|
||||||
|
{ headers: { Authorization: `Bot ${botToken}` } }
|
||||||
|
);
|
||||||
|
g.botInGuild = check.ok;
|
||||||
|
} catch (e) {
|
||||||
|
g.botInGuild = undefined;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
// If caller requested a fragment, render only the page template (no layout)
|
// If caller requested a fragment, render only the page template (no layout)
|
||||||
if (fragment) {
|
if (fragment) {
|
||||||
// Render the dashboard page and extract the inner #dashContent fragment
|
// Render the dashboard page and extract the inner #dashContent fragment
|
||||||
|
|||||||
@@ -14,18 +14,32 @@
|
|||||||
if(!btn) return;
|
if(!btn) return;
|
||||||
btn.addEventListener('click', async function(e){
|
btn.addEventListener('click', async function(e){
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const guildId = '<%= selectedGuild %>';
|
const injected = '<%= selectedGuild || "" %>';
|
||||||
|
let guildId = injected || '';
|
||||||
|
if(!guildId && typeof location !== 'undefined'){
|
||||||
|
try{
|
||||||
|
const parts = location.pathname.split('/').filter(Boolean);
|
||||||
|
// expect /dashboard/<guildId>/...
|
||||||
|
if(parts.length >= 2 && parts[0] === 'dashboard') guildId = parts[1];
|
||||||
|
}catch(e){ guildId = '' }
|
||||||
|
}
|
||||||
if(!guildId) return;
|
if(!guildId) return;
|
||||||
|
|
||||||
|
// visual feedback: disable while loading
|
||||||
|
btn.disabled = true;
|
||||||
|
btn.classList.add('opacity-50','pointer-events-none');
|
||||||
try{
|
try{
|
||||||
const res = await fetch(`/api/dashboard/${encodeURIComponent(guildId)}/roles`, { method: 'GET', headers: { 'Accept': 'application/json' } });
|
const res = await fetch(`/api/dashboard/${encodeURIComponent(guildId)}/roles`, { method: 'GET', headers: { 'Accept': 'application/json' } });
|
||||||
if(!res.ok) {
|
if(res && res.ok){
|
||||||
return;
|
const j = await res.json();
|
||||||
}
|
if(j && Array.isArray(j.roles)){
|
||||||
const j = await res.json();
|
window.dispatchEvent(new CustomEvent('roles:loaded', { detail: { roles: j.roles } }));
|
||||||
if(j && Array.isArray(j.roles)){
|
}
|
||||||
window.dispatchEvent(new CustomEvent('roles:loaded', { detail: { roles: j.roles } }));
|
|
||||||
}
|
}
|
||||||
}catch(err){ /* ignore errors */ }
|
}catch(err){ /* ignore errors */ }
|
||||||
|
// restore
|
||||||
|
btn.disabled = false;
|
||||||
|
btn.classList.remove('opacity-50','pointer-events-none');
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -135,6 +135,39 @@
|
|||||||
// close other menus first
|
// close other menus first
|
||||||
closeAllMenus();
|
closeAllMenus();
|
||||||
openDropdown(btn, list);
|
openDropdown(btn, list);
|
||||||
|
// runtime-check: for items that were not marked as having the bot, try fetching roles
|
||||||
|
(async function refreshMissingBotFlags(){
|
||||||
|
try{
|
||||||
|
const items = Array.from(list.querySelectorAll('.guild-item')) || [];
|
||||||
|
for(const el of items){
|
||||||
|
try{
|
||||||
|
const botAttr = el.getAttribute('data-bot');
|
||||||
|
if(botAttr === '1') continue;
|
||||||
|
const gid = el.getAttribute('data-id');
|
||||||
|
if(!gid) continue;
|
||||||
|
const res = await fetch(`/api/dashboard/${encodeURIComponent(gid)}/roles`, { method: 'GET', headers: { 'Accept': 'application/json' } });
|
||||||
|
if(res && res.ok){
|
||||||
|
// update element to show the bot is present
|
||||||
|
el.setAttribute('data-bot','1');
|
||||||
|
el.classList.remove('opacity-60');
|
||||||
|
const infoSpan = el.querySelector('.text-xs');
|
||||||
|
if(infoSpan) infoSpan.textContent = '(Bot)';
|
||||||
|
const nameDiv = el.querySelector('.flex-1');
|
||||||
|
if(nameDiv){
|
||||||
|
// ensure (Bot) marker exists after name
|
||||||
|
let marker = nameDiv.querySelector('.bot-marker');
|
||||||
|
if(!marker){
|
||||||
|
marker = document.createElement('span');
|
||||||
|
marker.className = 'ml-2 text-xs text-emerald-300 bot-marker';
|
||||||
|
marker.textContent = '(Bot)';
|
||||||
|
nameDiv.appendChild(marker);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}catch(e){ /* ignore per-item errors */ }
|
||||||
|
}
|
||||||
|
}catch(e){ /* ignore */ }
|
||||||
|
})();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// Use event delegation on the list so clicks on children are handled reliably
|
// Use event delegation on the list so clicks on children are handled reliably
|
||||||
|
|||||||
Reference in New Issue
Block a user