Spaces:
Runtime error
Runtime error
Update server.js
Browse files
server.js
CHANGED
|
@@ -57,6 +57,15 @@ console.error = (...args) => {
|
|
| 57 |
|
| 58 |
app.use(express.json());
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
// --- Helper: Get redirect URI ---
|
| 61 |
function getRedirectUri() {
|
| 62 |
if (REDIRECT_URI) return REDIRECT_URI;
|
|
@@ -254,47 +263,52 @@ app.get('/api/stats', requireAuth, (req, res) => {
|
|
| 254 |
// --- Server List ---
|
| 255 |
// --- Server List (shared with bot + permissions) ---
|
| 256 |
app.get('/api/servers', requireAuth, async (req, res) => {
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
// Improved Owner Recognition (Works for both Users and Teams)
|
| 263 |
-
const ownerId = app?.owner?.id;
|
| 264 |
-
const teamMembers = app?.owner?.members ? Array.from(app.owner.members.keys()) : [];
|
| 265 |
-
const isBotOwner = (ownerId === req.dashSession.user.id) || teamMembers.includes(req.dashSession.user.id);
|
| 266 |
|
| 267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
if (!g) return false;
|
| 281 |
-
const hasPerms = (BigInt(ug.permissions) & BigInt(0x20)) || (BigInt(ug.permissions) & BigInt(0x8));
|
| 282 |
-
return hasPerms;
|
| 283 |
-
});
|
| 284 |
-
}
|
| 285 |
|
| 286 |
-
|
| 287 |
-
const g = discordClient.guilds.cache.get(ug.id);
|
| 288 |
-
return {
|
| 289 |
name: g.name,
|
| 290 |
id: g.id,
|
| 291 |
-
members: g.memberCount,
|
| 292 |
icon: g.iconURL({ size: 64 }) || null
|
| 293 |
-
};
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
});
|
| 299 |
|
| 300 |
// --- Guild Config (Prefix) ---
|
|
|
|
| 57 |
|
| 58 |
app.use(express.json());
|
| 59 |
|
| 60 |
+
function hasDashboardPermissions(permissionBits) {
|
| 61 |
+
try {
|
| 62 |
+
const perms = BigInt(permissionBits || '0');
|
| 63 |
+
return (perms & BigInt(0x20)) !== BigInt(0) || (perms & BigInt(0x8)) !== BigInt(0);
|
| 64 |
+
} catch (e) {
|
| 65 |
+
return false;
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
// --- Helper: Get redirect URI ---
|
| 70 |
function getRedirectUri() {
|
| 71 |
if (REDIRECT_URI) return REDIRECT_URI;
|
|
|
|
| 263 |
// --- Server List ---
|
| 264 |
// --- Server List (shared with bot + permissions) ---
|
| 265 |
app.get('/api/servers', requireAuth, async (req, res) => {
|
| 266 |
+
try {
|
| 267 |
+
if (!discordClient || !discordClient.isReady()) {
|
| 268 |
+
return res.json({ pending: true, servers: [] });
|
| 269 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
|
| 271 |
+
const userGuilds = req.dashSession.guilds || [];
|
| 272 |
+
const app = await discordClient.application.fetch().catch(() => null);
|
| 273 |
+
|
| 274 |
+
// Owner recognition for both single-owner apps and team-owned apps.
|
| 275 |
+
const ownerCandidates = new Set();
|
| 276 |
+
if (app?.owner?.id) ownerCandidates.add(app.owner.id);
|
| 277 |
+
if (app?.owner?.ownerId) ownerCandidates.add(app.owner.ownerId);
|
| 278 |
+
if (app?.owner?.members) {
|
| 279 |
+
for (const member of app.owner.members.values()) {
|
| 280 |
+
if (member?.id) ownerCandidates.add(member.id);
|
| 281 |
+
if (member?.user?.id) ownerCandidates.add(member.user.id);
|
| 282 |
+
}
|
| 283 |
+
}
|
| 284 |
+
const isBotOwner = ownerCandidates.has(req.dashSession.user.id);
|
| 285 |
|
| 286 |
+
console.log(`[Dashboard] Request by ${req.dashSession.user.username} (Owner: ${isBotOwner})`);
|
| 287 |
+
|
| 288 |
+
let visibleGuilds = [];
|
| 289 |
+
if (isBotOwner) {
|
| 290 |
+
visibleGuilds = Array.from(discordClient.guilds.cache.values());
|
| 291 |
+
} else {
|
| 292 |
+
visibleGuilds = userGuilds
|
| 293 |
+
.filter(ug => hasDashboardPermissions(ug.permissions) && discordClient.guilds.cache.has(ug.id))
|
| 294 |
+
.map(ug => discordClient.guilds.cache.get(ug.id))
|
| 295 |
+
.filter(Boolean);
|
| 296 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
+
const list = visibleGuilds.map(g => ({
|
|
|
|
|
|
|
| 299 |
name: g.name,
|
| 300 |
id: g.id,
|
| 301 |
+
members: g.memberCount || 0,
|
| 302 |
icon: g.iconURL({ size: 64 }) || null
|
| 303 |
+
}));
|
| 304 |
+
|
| 305 |
+
if (isBotOwner) console.log(`[Dashboard] Owner access enabled for ${req.dashSession.user.username}`);
|
| 306 |
+
console.log(`[Dashboard] Serving ${list.length} managed servers to ${req.dashSession.user.username}`);
|
| 307 |
+
res.json({ pending: false, servers: list });
|
| 308 |
+
} catch (err) {
|
| 309 |
+
console.error('[Dashboard] /api/servers failed:', err);
|
| 310 |
+
res.status(500).json({ pending: false, servers: [], error: 'Failed to load servers' });
|
| 311 |
+
}
|
| 312 |
});
|
| 313 |
|
| 314 |
// --- Guild Config (Prefix) ---
|