Multi-channel feed

This commit is contained in:
Ichitux
2026-04-07 20:12:28 +02:00
parent 1073a61672
commit 1afa7d0c17
3 changed files with 34 additions and 7 deletions

4
.env
View File

@@ -2,9 +2,9 @@
# Get this from https://discord.com/developers/applications # Get this from https://discord.com/developers/applications
DISCORD_TOKEN=MTQ5MTAxODYzOTc0MTI5MjY0NA.GxBqVk.dB3cWx8t8-kWLrMLGFL-g2ePkrs7l_XEIedHdk DISCORD_TOKEN=MTQ5MTAxODYzOTc0MTI5MjY0NA.GxBqVk.dB3cWx8t8-kWLrMLGFL-g2ePkrs7l_XEIedHdk
# Discord Channel ID to monitor # Discord Channel IDs to monitor (comma-separated)
# Right-click a channel and select "Copy Channel ID" (enable Developer Mode first) # Right-click a channel and select "Copy Channel ID" (enable Developer Mode first)
DISCORD_CHANNEL_ID=600347996080701506 DISCORD_CHANNEL_IDS=600347996080701506,720537868996444230,1480636166150816007
# Server port (optional, defaults to 3000) # Server port (optional, defaults to 3000)
PORT=8000 PORT=8000

Binary file not shown.

View File

@@ -35,6 +35,13 @@ app.get('/health', (req, res) => {
res.json({ status: 'ok' }); res.json({ status: 'ok' });
}); });
// Parse channel IDs from environment variable
function getChannelIds() {
const channelIdsStr = process.env.DISCORD_CHANNEL_IDS || process.env.DISCORD_CHANNEL_ID;
if (!channelIdsStr) return [];
return channelIdsStr.split(',').map(id => id.trim()).filter(id => id.length > 0);
}
// Fetch endpoint - trigger message fetch for a channel // Fetch endpoint - trigger message fetch for a channel
app.post('/fetch/:channelId', async (req, res) => { app.post('/fetch/:channelId', async (req, res) => {
try { try {
@@ -56,9 +63,29 @@ app.post('/fetch/:channelId', async (req, res) => {
} }
}); });
// Fetch all configured channels
async function fetchAllChannels() {
const channelIds = getChannelIds();
const results = await Promise.allSettled(
channelIds.map(id => fetchChannelMessages(id, 100))
);
let totalFetched = 0;
results.forEach((result, index) => {
if (result.status === 'fulfilled') {
totalFetched += result.value.length;
console.log(` Channel ${channelIds[index]}: ${result.value.length} messages`);
} else {
console.error(` Channel ${channelIds[index]}: failed - ${result.reason}`);
}
});
console.log(`Total messages fetched: ${totalFetched}`);
}
async function start() { async function start() {
// Validate required environment variables // Validate required environment variables
const requiredEnv = ['DISCORD_TOKEN', 'DISCORD_CHANNEL_ID']; const requiredEnv = ['DISCORD_TOKEN', 'DISCORD_CHANNEL_IDS'];
const missing = requiredEnv.filter(env => !process.env[env]); const missing = requiredEnv.filter(env => !process.env[env]);
if (missing.length > 0) { if (missing.length > 0) {
@@ -71,9 +98,9 @@ async function start() {
console.log('Logging in to Discord...'); console.log('Logging in to Discord...');
await login(process.env.DISCORD_TOKEN); await login(process.env.DISCORD_TOKEN);
// Initial fetch // Initial fetch from all configured channels
console.log('Fetching initial messages...'); console.log('Fetching initial messages from all channels...');
await fetchChannelMessages(process.env.DISCORD_CHANNEL_ID, 100); await fetchAllChannels();
// Start HTTP server // Start HTTP server
app.listen(PORT, '0.0.0.0', () => { app.listen(PORT, '0.0.0.0', () => {
@@ -85,7 +112,7 @@ async function start() {
const FETCH_INTERVAL = parseInt(process.env.FETCH_INTERVAL_MS, 10) || 5 * 60 * 1000; const FETCH_INTERVAL = parseInt(process.env.FETCH_INTERVAL_MS, 10) || 5 * 60 * 1000;
setInterval(async () => { setInterval(async () => {
try { try {
await fetchChannelMessages(process.env.DISCORD_CHANNEL_ID, 100); await fetchAllChannels();
} catch (error) { } catch (error) {
console.error('Periodic fetch failed:', error); console.error('Periodic fetch failed:', error);
} }