Implement polling to fetch new posts for Discord synchronization

Replace Supabase Realtime subscription with a polling mechanism in `discord-bot/listeners/feedSync.js` to fetch new community posts and send them to Discord. This change removes the direct HTTP call from the server to the bot and introduces a `POLL_INTERVAL` of 5000ms, along with a `cleanup` function to clear the interval.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 9203795e-937a-4306-b81d-b4d5c78c240e
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Event-Id: 381cfcae-1168-4daa-b95e-ba1416e8ce9c
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7c94b7a0-29c7-4f2e-94ef-44b2153872b7/9203795e-937a-4306-b81d-b4d5c78c240e/duiWnI1
Replit-Helium-Checkpoint-Created: true
This commit is contained in:
sirpiglr 2025-12-03 03:56:41 +00:00
commit 1fd24003ae

View file

@ -10,7 +10,11 @@ const FEED_CHANNEL_ID = process.env.DISCORD_MAIN_CHAT_CHANNELS
? process.env.DISCORD_MAIN_CHAT_CHANNELS.split(",")[0].trim()
: null;
const POLL_INTERVAL = 5000; // Check every 5 seconds
let discordClient = null;
let lastCheckedTime = null;
let pollInterval = null;
function getArmColor(arm) {
const colors = {
@ -59,7 +63,6 @@ async function sendPostToDiscord(post, authorInfo = null) {
}
if (content.source === "discord") {
console.log("[Feed Bridge] Skipping Discord-sourced post to prevent loop");
return { success: true, skipped: true, reason: "Discord-sourced post" };
}
@ -128,6 +131,48 @@ async function sendPostToDiscord(post, authorInfo = null) {
}
}
async function checkForNewPosts() {
if (!discordClient || !FEED_CHANNEL_ID) return;
try {
const { data: posts, error } = await supabase
.from("community_posts")
.select("*")
.gt("created_at", lastCheckedTime.toISOString())
.order("created_at", { ascending: true });
if (error) {
console.error("[Feed Bridge] Error fetching new posts:", error);
return;
}
if (posts && posts.length > 0) {
console.log(`[Feed Bridge] Found ${posts.length} new post(s)`);
for (const post of posts) {
let content = {};
try {
content = typeof post.content === "string" ? JSON.parse(post.content) : post.content;
} catch {
content = { text: post.content };
}
if (content.source === "discord") {
console.log(`[Feed Bridge] Skipping Discord-sourced post ${post.id}`);
continue;
}
console.log(`[Feed Bridge] Bridging post ${post.id} to Discord...`);
await sendPostToDiscord(post);
}
lastCheckedTime = new Date(posts[posts.length - 1].created_at);
}
} catch (error) {
console.error("[Feed Bridge] Poll error:", error);
}
}
function setupFeedListener(client) {
discordClient = client;
@ -136,6 +181,12 @@ function setupFeedListener(client) {
return;
}
lastCheckedTime = new Date();
console.log("[Feed Bridge] Starting polling for new posts (every 5 seconds)...");
pollInterval = setInterval(checkForNewPosts, POLL_INTERVAL);
console.log("[Feed Bridge] ✅ Feed bridge ready (channel: " + FEED_CHANNEL_ID + ")");
}
@ -147,4 +198,11 @@ function getFeedChannelId() {
return FEED_CHANNEL_ID;
}
module.exports = { setupFeedListener, sendPostToDiscord, getDiscordClient, getFeedChannelId };
function cleanup() {
if (pollInterval) {
clearInterval(pollInterval);
console.log("[Feed Bridge] Stopped polling");
}
}
module.exports = { setupFeedListener, sendPostToDiscord, getDiscordClient, getFeedChannelId, cleanup };