Fixed Invite Accordion

This commit is contained in:
2025-10-10 05:12:54 -04:00
parent 900ce85e2c
commit 8236c1e0e7
7 changed files with 219 additions and 7 deletions

View File

@@ -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 };

View File

@@ -0,0 +1,49 @@
const api = require('../api');
module.exports = {
name: 'inviteCreate',
async execute(invite) {
try {
// Only track invites created by the bot or in channels the bot can access
const guildId = invite.guild.id;
// Check if this invite was created by our bot
const isBotCreated = invite.inviter && invite.inviter.id === invite.client.user.id;
if (isBotCreated) {
// Add to database if created by bot
const inviteData = {
code: invite.code,
guildId: guildId,
url: invite.url,
channelId: invite.channel.id,
createdAt: invite.createdAt ? invite.createdAt.toISOString() : new Date().toISOString(),
maxUses: invite.maxUses || 0,
maxAge: invite.maxAge || 0,
temporary: invite.temporary || false
};
// Use the API to add the invite to database
await api.addInvite(inviteData);
// Publish SSE event for real-time frontend updates
const bot = require('..');
if (bot && bot.publishEvent) {
bot.publishEvent(guildId, 'inviteCreated', {
code: invite.code,
url: invite.url,
channelId: invite.channel.id,
maxUses: invite.maxUses || 0,
maxAge: invite.maxAge || 0,
temporary: invite.temporary || false,
createdAt: invite.createdAt ? invite.createdAt.toISOString() : new Date().toISOString()
});
}
}
// Note: We don't automatically add invites created by other users to avoid spam
// Only bot-created invites are tracked for the web interface
} catch (error) {
console.error('Error handling inviteCreate:', error);
}
}
};

View File

@@ -0,0 +1,24 @@
const api = require('../api');
module.exports = {
name: 'inviteDelete',
async execute(invite) {
try {
const guildId = invite.guild.id;
const code = invite.code;
// Remove from database
await api.deleteInvite(guildId, code);
// Publish SSE event for real-time frontend updates
const bot = require('..');
if (bot && bot.publishEvent) {
bot.publishEvent(guildId, 'inviteDeleted', {
code: code
});
}
} catch (error) {
console.error('Error handling inviteDelete:', error);
}
}
};

View File

@@ -1,5 +1,6 @@
const { ActivityType } = require('discord.js');
const deployCommands = require('../deploy-commands');
const api = require('../api');
module.exports = {
name: 'clientReady',
@@ -16,6 +17,31 @@ module.exports = {
}
}
// Reconcile invites for all guilds to detect invites deleted while bot was offline
console.log('🔄 Reconciling invites for offline changes...');
let totalReconciled = 0;
for (const guildId of guildIds) {
try {
const guild = client.guilds.cache.get(guildId);
if (!guild) continue;
// Fetch current invites from Discord
const discordInvites = await guild.invites.fetch();
const currentInvites = Array.from(discordInvites.values());
// Reconcile with database
const reconciled = await api.reconcileInvites(guildId, currentInvites);
totalReconciled += reconciled;
} catch (e) {
console.error(`Failed to reconcile invites for guild ${guildId}:`, e && e.message ? e.message : e);
}
}
if (totalReconciled > 0) {
console.log(`✅ Invite reconciliation complete: removed ${totalReconciled} stale invites`);
} else {
console.log('✅ Invite reconciliation complete: no stale invites found');
}
const activities = [
{ name: 'Watch EhChad Live!', type: ActivityType.Streaming, url: 'https://twitch.tv/ehchad' },
{ name: 'Follow EhChad!', type: ActivityType.Streaming, url: 'https://twitch.tv/ehchad' },