Multi-channel feed
This commit is contained in:
4
.env
4
.env
@@ -2,9 +2,9 @@
|
||||
# Get this from https://discord.com/developers/applications
|
||||
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)
|
||||
DISCORD_CHANNEL_ID=600347996080701506
|
||||
DISCORD_CHANNEL_IDS=600347996080701506,720537868996444230,1480636166150816007
|
||||
|
||||
# Server port (optional, defaults to 3000)
|
||||
PORT=8000
|
||||
|
||||
BIN
data/messages.db
BIN
data/messages.db
Binary file not shown.
37
src/index.js
37
src/index.js
@@ -35,6 +35,13 @@ app.get('/health', (req, res) => {
|
||||
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
|
||||
app.post('/fetch/:channelId', async (req, res) => {
|
||||
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() {
|
||||
// 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]);
|
||||
|
||||
if (missing.length > 0) {
|
||||
@@ -71,9 +98,9 @@ async function start() {
|
||||
console.log('Logging in to Discord...');
|
||||
await login(process.env.DISCORD_TOKEN);
|
||||
|
||||
// Initial fetch
|
||||
console.log('Fetching initial messages...');
|
||||
await fetchChannelMessages(process.env.DISCORD_CHANNEL_ID, 100);
|
||||
// Initial fetch from all configured channels
|
||||
console.log('Fetching initial messages from all channels...');
|
||||
await fetchAllChannels();
|
||||
|
||||
// Start HTTP server
|
||||
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;
|
||||
setInterval(async () => {
|
||||
try {
|
||||
await fetchChannelMessages(process.env.DISCORD_CHANNEL_ID, 100);
|
||||
await fetchAllChannels();
|
||||
} catch (error) {
|
||||
console.error('Periodic fetch failed:', error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user