Spaces:
Runtime error
Runtime error
Update server.js
Browse files
server.js
CHANGED
|
@@ -85,9 +85,8 @@ function hasDashboardPermissions(permissionBits) {
|
|
| 85 |
|
| 86 |
// --- Helper: Get redirect URI ---
|
| 87 |
function getRedirectUri(req) {
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
// Fallback: If we have a request object, use it to build the URI dynamically
|
| 91 |
if (req) {
|
| 92 |
const host = req.get('host');
|
| 93 |
const protocol = req.protocol === 'http' && host.includes('.hf.space') ? 'https' : req.protocol;
|
|
@@ -95,6 +94,8 @@ function getRedirectUri(req) {
|
|
| 95 |
return `${protocol}://${host}${path}`;
|
| 96 |
}
|
| 97 |
|
|
|
|
|
|
|
| 98 |
const spaceId = process.env.SPACE_ID;
|
| 99 |
if (spaceId) {
|
| 100 |
const host = spaceId.includes('/') ? spaceId.replace('/', '-').toLowerCase() : spaceId.toLowerCase();
|
|
@@ -482,6 +483,7 @@ app.get('/api/config/:guildId', requireAuth, (req, res) => {
|
|
| 482 |
if (!hasPerms) return res.status(403).json({ error: 'Missing permissions' });
|
| 483 |
|
| 484 |
const storage = require('./storage');
|
|
|
|
| 485 |
const config = storage.data.config[guildId] || {};
|
| 486 |
res.json({
|
| 487 |
prefix: storage.getPrefix(guildId),
|
|
@@ -497,7 +499,8 @@ app.get('/api/config/:guildId', requireAuth, (req, res) => {
|
|
| 497 |
remindersLead: config.remindersLead || 30,
|
| 498 |
aliases: config.aliases || {},
|
| 499 |
leaderboardChannel: config.leaderboardChannel || null,
|
| 500 |
-
statsFrequency: config.statsFrequency || 'never'
|
|
|
|
| 501 |
});
|
| 502 |
});
|
| 503 |
|
|
@@ -513,9 +516,28 @@ app.get('/api/channels/:guildId', requireAuth, (req, res) => {
|
|
| 513 |
res.json(channels);
|
| 514 |
});
|
| 515 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 516 |
app.post('/api/config/:guildId', requireAuth, (req, res) => {
|
| 517 |
const { guildId } = req.params;
|
| 518 |
-
const { prefix, autoFriendlyEnabled, autoFriendlyInterval, autoFriendlyChannel, postEnabled, postChannelId, friendlyText, cleanupHours, lineupCleanupHours, remindersEnabled, remindersLead, aliases, leaderboardChannel, statsFrequency } = req.body;
|
| 519 |
const userGuilds = req.dashSession.guilds || [];
|
| 520 |
const ug = userGuilds.find(g => g.id === guildId);
|
| 521 |
|
|
@@ -557,6 +579,12 @@ app.post('/api/config/:guildId', requireAuth, (req, res) => {
|
|
| 557 |
storage.data.config[guildId].statsFrequency = statsFrequency;
|
| 558 |
}
|
| 559 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 560 |
storage.save('config');
|
| 561 |
res.json({ success: true, message: 'Settings updated successfully' });
|
| 562 |
});
|
|
|
|
| 85 |
|
| 86 |
// --- Helper: Get redirect URI ---
|
| 87 |
function getRedirectUri(req) {
|
| 88 |
+
// Prioritize building the URI dynamically from the incoming request.
|
| 89 |
+
// This allows duplicated/cloned Spaces to work perfectly out-of-the-box.
|
|
|
|
| 90 |
if (req) {
|
| 91 |
const host = req.get('host');
|
| 92 |
const protocol = req.protocol === 'http' && host.includes('.hf.space') ? 'https' : req.protocol;
|
|
|
|
| 94 |
return `${protocol}://${host}${path}`;
|
| 95 |
}
|
| 96 |
|
| 97 |
+
if (REDIRECT_URI) return REDIRECT_URI;
|
| 98 |
+
|
| 99 |
const spaceId = process.env.SPACE_ID;
|
| 100 |
if (spaceId) {
|
| 101 |
const host = spaceId.includes('/') ? spaceId.replace('/', '-').toLowerCase() : spaceId.toLowerCase();
|
|
|
|
| 483 |
if (!hasPerms) return res.status(403).json({ error: 'Missing permissions' });
|
| 484 |
|
| 485 |
const storage = require('./storage');
|
| 486 |
+
const roleStorage = require('./roleStorage');
|
| 487 |
const config = storage.data.config[guildId] || {};
|
| 488 |
res.json({
|
| 489 |
prefix: storage.getPrefix(guildId),
|
|
|
|
| 499 |
remindersLead: config.remindersLead || 30,
|
| 500 |
aliases: config.aliases || {},
|
| 501 |
leaderboardChannel: config.leaderboardChannel || null,
|
| 502 |
+
statsFrequency: config.statsFrequency || 'never',
|
| 503 |
+
whitelistedRoles: roleStorage.roles[guildId] || []
|
| 504 |
});
|
| 505 |
});
|
| 506 |
|
|
|
|
| 516 |
res.json(channels);
|
| 517 |
});
|
| 518 |
|
| 519 |
+
app.get('/api/roles/:guildId', requireAuth, (req, res) => {
|
| 520 |
+
const { guildId } = req.params;
|
| 521 |
+
const userGuilds = req.dashSession.guilds || [];
|
| 522 |
+
const ug = userGuilds.find(g => g.id === guildId);
|
| 523 |
+
|
| 524 |
+
if (!ug) return res.status(403).json({ error: 'Access denied' });
|
| 525 |
+
const hasPerms = (BigInt(ug.permissions) & BigInt(0x20)) || (BigInt(ug.permissions) & BigInt(0x8));
|
| 526 |
+
if (!hasPerms) return res.status(403).json({ error: 'Missing permissions' });
|
| 527 |
+
|
| 528 |
+
if (!discordClient || !discordClient.isReady()) return res.json([]);
|
| 529 |
+
const guild = discordClient.guilds.cache.get(guildId);
|
| 530 |
+
if (!guild) return res.json([]);
|
| 531 |
+
|
| 532 |
+
const roles = guild.roles.cache
|
| 533 |
+
.filter(r => r.name !== '@everyone')
|
| 534 |
+
.map(r => ({ id: r.id, name: r.name, color: r.hexColor }));
|
| 535 |
+
res.json(roles);
|
| 536 |
+
});
|
| 537 |
+
|
| 538 |
app.post('/api/config/:guildId', requireAuth, (req, res) => {
|
| 539 |
const { guildId } = req.params;
|
| 540 |
+
const { prefix, autoFriendlyEnabled, autoFriendlyInterval, autoFriendlyChannel, postEnabled, postChannelId, friendlyText, cleanupHours, lineupCleanupHours, remindersEnabled, remindersLead, aliases, leaderboardChannel, statsFrequency, whitelistedRoles } = req.body;
|
| 541 |
const userGuilds = req.dashSession.guilds || [];
|
| 542 |
const ug = userGuilds.find(g => g.id === guildId);
|
| 543 |
|
|
|
|
| 579 |
storage.data.config[guildId].statsFrequency = statsFrequency;
|
| 580 |
}
|
| 581 |
|
| 582 |
+
if (whitelistedRoles !== undefined) {
|
| 583 |
+
const roleStorage = require('./roleStorage');
|
| 584 |
+
roleStorage.roles[guildId] = Array.isArray(whitelistedRoles) ? whitelistedRoles : [];
|
| 585 |
+
roleStorage.save();
|
| 586 |
+
}
|
| 587 |
+
|
| 588 |
storage.save('config');
|
| 589 |
res.json({ success: true, message: 'Settings updated successfully' });
|
| 590 |
});
|