fix channel updating
This commit is contained in:
@@ -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: [] });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user