bug fixes

This commit is contained in:
2025-10-10 18:51:23 -04:00
parent 8236c1e0e7
commit 61ab1e1d9e
15 changed files with 4463 additions and 7 deletions

View File

@@ -1216,6 +1216,75 @@ app.post('/api/servers/:guildId/admin-logs-settings', async (req, res) => {
}
});
// REACTION ROLES: CRUD
app.get('/api/servers/:guildId/reaction-roles', async (req, res) => {
try {
const { guildId } = req.params;
const rows = await pgClient.listReactionRoles(guildId);
res.json(rows);
} catch (err) {
console.error('Error listing reaction roles:', err);
res.status(500).json({ success: false, message: 'Internal Server Error' });
}
});
app.post('/api/servers/:guildId/reaction-roles', async (req, res) => {
try {
const { guildId } = req.params;
const { channelId, name, embed, buttons, messageId } = req.body || {};
if (!channelId || !name || !embed || !Array.isArray(buttons) || buttons.length === 0) {
return res.status(400).json({ success: false, message: 'channelId, name, embed, and at least one button are required' });
}
const created = await pgClient.createReactionRole({ guildId, channelId, name, embed, buttons, messageId });
// publish SSE
publishEvent(guildId, 'reactionRolesUpdate', { action: 'create', reactionRole: created });
res.json({ success: true, reactionRole: created });
} catch (err) {
console.error('Error creating reaction role:', err && err.message ? err.message : err);
// If the pg helper threw a validation error, return 400 with message
if (err && err.message && err.message.startsWith('Invalid reaction role payload')) {
return res.status(400).json({ success: false, message: err.message });
}
res.status(500).json({ success: false, message: 'Internal Server Error' });
}
});
app.put('/api/servers/:guildId/reaction-roles/:id', async (req, res) => {
try {
const { guildId, id } = req.params;
const updates = req.body || {};
const existing = await pgClient.getReactionRole(id);
if (!existing || existing.guild_id !== guildId) return res.status(404).json({ success: false, message: 'Not found' });
const mapped = {
channel_id: updates.channelId || existing.channel_id,
message_id: typeof updates.messageId !== 'undefined' ? updates.messageId : existing.message_id,
name: typeof updates.name !== 'undefined' ? updates.name : existing.name,
embed: typeof updates.embed !== 'undefined' ? updates.embed : existing.embed,
buttons: typeof updates.buttons !== 'undefined' ? updates.buttons : existing.buttons
};
const updated = await pgClient.updateReactionRole(id, mapped);
publishEvent(guildId, 'reactionRolesUpdate', { action: 'update', reactionRole: updated });
res.json({ success: true, reactionRole: updated });
} catch (err) {
console.error('Error updating reaction role:', err);
res.status(500).json({ success: false, message: 'Internal Server Error' });
}
});
app.delete('/api/servers/:guildId/reaction-roles/:id', async (req, res) => {
try {
const { guildId, id } = req.params;
const existing = await pgClient.getReactionRole(id);
if (!existing || existing.guild_id !== guildId) return res.status(404).json({ success: false, message: 'Not found' });
await pgClient.deleteReactionRole(id);
publishEvent(guildId, 'reactionRolesUpdate', { action: 'delete', id });
res.json({ success: true });
} catch (err) {
console.error('Error deleting reaction role:', err);
res.status(500).json({ success: false, message: 'Internal Server Error' });
}
});
app.get('/api/servers/:guildId/admin-logs', async (req, res) => {
try {
const { guildId } = req.params;
@@ -1537,6 +1606,40 @@ app.post('/internal/test-live', express.json(), async (req, res) => {
}
});
// Internal: ask bot to publish a reaction role message for a reaction role ID
app.post('/internal/publish-reaction-role', express.json(), async (req, res) => {
try {
// If BOT_SECRET is configured, require the request to include it in the header
const requiredSecret = process.env.BOT_SECRET;
if (requiredSecret) {
const provided = (req.get('x-bot-secret') || req.get('X-Bot-Secret') || '').toString();
if (!provided || provided !== requiredSecret) {
console.warn('/internal/publish-reaction-role: missing or invalid x-bot-secret header');
return res.status(401).json({ success: false, message: 'Unauthorized' });
}
}
const { guildId, id } = req.body || {};
if (!guildId || !id) return res.status(400).json({ success: false, message: 'guildId and id required' });
const rr = await pgClient.getReactionRole(id);
if (!rr || rr.guild_id !== guildId) return res.status(404).json({ success: false, message: 'Not found' });
const result = await bot.postReactionRoleMessage(guildId, rr);
if (result && result.success) {
// update db already attempted by bot; publish SSE update
publishEvent(guildId, 'reactionRolesUpdate', { action: 'posted', id, messageId: result.messageId });
} else {
// If the channel or message cannot be created because it no longer exists, remove the DB entry
if (result && result.message && result.message.toLowerCase && (result.message.includes('Channel not found') || result.message.includes('Guild not found'))) {
try { await pgClient.deleteReactionRole(id); publishEvent(guildId, 'reactionRolesUpdate', { action: 'delete', id }); } catch(e){}
}
}
res.json(result);
} catch (e) {
console.error('Error in /internal/publish-reaction-role:', e);
res.status(500).json({ success: false, message: 'Internal error' });
}
});
app.listen(port, host, () => {
console.log(`Server is running on ${host}:${port}`);
});