fix channel updating
This commit is contained in:
@@ -5,12 +5,19 @@ const router = express.Router();
|
|||||||
// Get all voice channels
|
// Get all voice channels
|
||||||
router.get('/', async (req, res) => {
|
router.get('/', async (req, res) => {
|
||||||
try {
|
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();
|
const result = await response.json();
|
||||||
res.json(result);
|
res.json(result);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
req.app.locals.logger.error(`Error getting channels: ${error.message}`);
|
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) => {
|
router.post('/join', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { channelId } = req.body;
|
const { channelId } = req.body;
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeout = setTimeout(() => controller.abort(), 5000);
|
||||||
|
|
||||||
const response = await fetch(`${req.app.locals.botUrl}/join`, {
|
const response = await fetch(`${req.app.locals.botUrl}/join`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ channelId })
|
body: JSON.stringify({ channelId }),
|
||||||
|
signal: controller.signal
|
||||||
});
|
});
|
||||||
|
clearTimeout(timeout);
|
||||||
|
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
@@ -39,9 +52,15 @@ router.post('/join', async (req, res) => {
|
|||||||
// Disconnect from voice channel
|
// Disconnect from voice channel
|
||||||
router.post('/disconnect', async (req, res) => {
|
router.post('/disconnect', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeout = setTimeout(() => controller.abort(), 5000);
|
||||||
|
|
||||||
const response = await fetch(`${req.app.locals.botUrl}/disconnect`, {
|
const response = await fetch(`${req.app.locals.botUrl}/disconnect`, {
|
||||||
method: 'POST'
|
method: 'POST',
|
||||||
|
signal: controller.signal
|
||||||
});
|
});
|
||||||
|
clearTimeout(timeout);
|
||||||
|
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
|
|||||||
@@ -412,23 +412,59 @@ app.post('/queue/add', (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.get('/channels', async (req, res) => {
|
app.get('/channels', async (req, res) => {
|
||||||
|
const currentChannelId = connection?.joinConfig?.channelId;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const guild = await client.guilds.fetch(DISCORD_GUILD_ID);
|
// Try to get channels from cache first, then fetch if needed
|
||||||
const channels = await guild.channels.fetch();
|
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
|
const voiceChannels = channels
|
||||||
.filter(channel => channel.type === 2) // Type 2 is voice channel
|
.filter(channel => channel.type === 2) // Type 2 is voice channel
|
||||||
.map(channel => ({
|
.map(channel => ({
|
||||||
id: channel.id,
|
id: channel.id,
|
||||||
name: channel.name,
|
name: channel.name,
|
||||||
userCount: channel.members?.size || 0,
|
userCount: channel.members?.size || 0,
|
||||||
current: connection?.joinConfig?.channelId === channel.id
|
current: currentChannelId === channel.id
|
||||||
}))
|
}))
|
||||||
.sort((a, b) => a.name.localeCompare(b.name));
|
.sort((a, b) => a.name.localeCompare(b.name));
|
||||||
|
|
||||||
res.json({ channels: voiceChannels });
|
return res.json({ channels: voiceChannels });
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no cached guild, return connection info
|
||||||
|
throw new Error('Guild not in cache');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Failed to fetch channels: ${error.message}`);
|
// On error, return cached connection info
|
||||||
res.status(500).json({ error: 'Failed to fetch channels' });
|
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