Fixed Invite Accordion
This commit is contained in:
@@ -90,10 +90,24 @@ async function listInvites(guildId) {
|
||||
async function addInvite(guildId, invite) {
|
||||
const path = `/api/servers/${guildId}/invites`;
|
||||
try {
|
||||
// If invite is an object with code property, it's already created - send full data
|
||||
// If it's just channelId/maxAge/etc, it's for creation
|
||||
const isExistingInvite = invite && typeof invite === 'object' && invite.code;
|
||||
|
||||
const body = isExistingInvite ? {
|
||||
code: invite.code,
|
||||
url: invite.url,
|
||||
channelId: invite.channelId,
|
||||
maxUses: invite.maxUses,
|
||||
maxAge: invite.maxAge,
|
||||
temporary: invite.temporary,
|
||||
createdAt: invite.createdAt
|
||||
} : invite;
|
||||
|
||||
const res = await tryFetch(path, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(invite),
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
return res && res.ok;
|
||||
} catch (e) {
|
||||
@@ -208,4 +222,44 @@ async function getAutoroleSettings(guildId) {
|
||||
return json || { enabled: false, roleId: '' };
|
||||
}
|
||||
|
||||
module.exports = { getServerSettings, upsertServerSettings, getCommands, toggleCommand, listInvites, addInvite, deleteInvite, getTwitchUsers, addTwitchUser, deleteTwitchUser, tryFetchTwitchStreams, _rawGetTwitchStreams, getKickUsers, addKickUser, deleteKickUser, getWelcomeLeaveSettings, getAutoroleSettings };
|
||||
async function reconcileInvites(guildId, currentDiscordInvites) {
|
||||
try {
|
||||
// Get invites from database
|
||||
const dbInvites = await listInvites(guildId) || [];
|
||||
|
||||
// Find invites in database that no longer exist in Discord
|
||||
const discordInviteCodes = new Set(currentDiscordInvites.map(inv => inv.code));
|
||||
const deletedInvites = dbInvites.filter(dbInv => !discordInviteCodes.has(dbInv.code));
|
||||
|
||||
// Delete each invite that no longer exists
|
||||
for (const invite of deletedInvites) {
|
||||
console.log(`🗑️ Reconciling deleted invite ${invite.code} for guild ${guildId}`);
|
||||
await deleteInvite(guildId, invite.code);
|
||||
|
||||
// Publish SSE event for frontend update
|
||||
try {
|
||||
await tryFetch('/api/events/publish', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
event: 'inviteDeleted',
|
||||
data: { code: invite.code, guildId }
|
||||
})
|
||||
});
|
||||
} catch (sseErr) {
|
||||
console.error('Failed to publish SSE event for reconciled invite deletion:', sseErr);
|
||||
}
|
||||
}
|
||||
|
||||
if (deletedInvites.length > 0) {
|
||||
console.log(`✅ Reconciled ${deletedInvites.length} deleted invites for guild ${guildId}`);
|
||||
}
|
||||
|
||||
return deletedInvites.length;
|
||||
} catch (e) {
|
||||
console.error(`Failed to reconcile invites for guild ${guildId}:`, e && e.message ? e.message : e);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { getServerSettings, upsertServerSettings, getCommands, toggleCommand, listInvites, addInvite, deleteInvite, getTwitchUsers, addTwitchUser, deleteTwitchUser, tryFetchTwitchStreams, _rawGetTwitchStreams, getKickUsers, addKickUser, deleteKickUser, getWelcomeLeaveSettings, getAutoroleSettings, reconcileInvites };
|
||||
|
||||
Reference in New Issue
Block a user