From fb31de292f1af8a99cca331e947869537173a726 Mon Sep 17 00:00:00 2001 From: Scott Register Date: Sat, 13 Dec 2025 12:34:06 -0800 Subject: [PATCH] fix channel updating --- api/src/routes/channels.js | 27 +++++++++++++--- bot/src/index.js | 66 +++++++++++++++++++++++++++++--------- 2 files changed, 74 insertions(+), 19 deletions(-) diff --git a/api/src/routes/channels.js b/api/src/routes/channels.js index 52596f3..e1ea89a 100644 --- a/api/src/routes/channels.js +++ b/api/src/routes/channels.js @@ -5,12 +5,19 @@ const router = express.Router(); // Get all voice channels router.get('/', async (req, res) => { try { - const response = await fetch(`${req.app.locals.botUrl}/channels`); + const controller = new AbortController(); + const timeout = setTimeout(() => controller.abort(), 5000); + + const response = await fetch(`${req.app.locals.botUrl}/channels`, { + signal: controller.signal + }); + clearTimeout(timeout); + const result = await response.json(); res.json(result); } catch (error) { req.app.locals.logger.error(`Error getting channels: ${error.message}`); - res.status(500).json({ error: 'Failed to get channels' }); + res.status(500).json({ error: 'Failed to get channels', channels: [] }); } }); @@ -18,11 +25,17 @@ router.get('/', async (req, res) => { router.post('/join', async (req, res) => { try { const { channelId } = req.body; + const controller = new AbortController(); + const timeout = setTimeout(() => controller.abort(), 5000); + const response = await fetch(`${req.app.locals.botUrl}/join`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ channelId }) + body: JSON.stringify({ channelId }), + signal: controller.signal }); + clearTimeout(timeout); + const result = await response.json(); if (response.ok) { @@ -39,9 +52,15 @@ router.post('/join', async (req, res) => { // Disconnect from voice channel router.post('/disconnect', async (req, res) => { try { + const controller = new AbortController(); + const timeout = setTimeout(() => controller.abort(), 5000); + const response = await fetch(`${req.app.locals.botUrl}/disconnect`, { - method: 'POST' + method: 'POST', + signal: controller.signal }); + clearTimeout(timeout); + const result = await response.json(); if (response.ok) { diff --git a/bot/src/index.js b/bot/src/index.js index 3602e4b..acd5a49 100644 --- a/bot/src/index.js +++ b/bot/src/index.js @@ -412,23 +412,59 @@ app.post('/queue/add', (req, res) => { }); app.get('/channels', async (req, res) => { - try { - const guild = await client.guilds.fetch(DISCORD_GUILD_ID); - const channels = await guild.channels.fetch(); - const voiceChannels = channels - .filter(channel => channel.type === 2) // Type 2 is voice channel - .map(channel => ({ - id: channel.id, - name: channel.name, - userCount: channel.members?.size || 0, - current: connection?.joinConfig?.channelId === channel.id - })) - .sort((a, b) => a.name.localeCompare(b.name)); + const currentChannelId = connection?.joinConfig?.channelId; - res.json({ channels: voiceChannels }); + try { + // Try to get channels from cache first, then fetch if needed + const guild = client.guilds.cache.get(DISCORD_GUILD_ID); + + if (guild) { + // Use cached channels if available + let channels = guild.channels.cache; + + // If cache is empty, try fetching with timeout + if (channels.size === 0) { + try { + const fetchPromise = guild.channels.fetch(); + const timeoutPromise = new Promise((_, reject) => + setTimeout(() => reject(new Error('Timeout')), 2000) + ); + channels = await Promise.race([fetchPromise, timeoutPromise]); + } catch (fetchError) { + logger.warn(`Failed to fetch channels (${fetchError.message}), using cache`); + } + } + + const voiceChannels = channels + .filter(channel => channel.type === 2) // Type 2 is voice channel + .map(channel => ({ + id: channel.id, + name: channel.name, + userCount: channel.members?.size || 0, + current: currentChannelId === channel.id + })) + .sort((a, b) => a.name.localeCompare(b.name)); + + return res.json({ channels: voiceChannels }); + } + + // If no cached guild, return connection info + throw new Error('Guild not in cache'); } catch (error) { - logger.error(`Failed to fetch channels: ${error.message}`); - res.status(500).json({ error: 'Failed to fetch channels' }); + // On error, return cached connection info + logger.warn(`Failed to get channels (${error.message}), returning cached data`); + + if (currentChannelId) { + return res.json({ + channels: [{ + id: currentChannelId, + name: 'Connected', + userCount: 0, + current: true + }] + }); + } + res.json({ channels: [] }); } });