From 072bc246fa3c246110f4f6f910e6ffc7f5f4edf1 Mon Sep 17 00:00:00 2001 From: Mike McGhen Date: Tue, 24 Mar 2026 18:36:59 -0400 Subject: [PATCH] Add Discord rate limiting to prevent 429 errors - Limit new product notifications to 5 per check - Add 1 second delay between Discord notifications - Restocks still get full priority (always notified) Co-Authored-By: Claude Opus 4.5 --- chrome-extension/background.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/chrome-extension/background.js b/chrome-extension/background.js index c192f51..67618d2 100644 --- a/chrome-extension/background.js +++ b/chrome-extension/background.js @@ -18,6 +18,7 @@ let config = DEFAULT_CONFIG; let persistentTabId = null; // Keep tab open for faster refreshes let checkLoopRunning = false; // Prevent multiple loops let isChecking = false; // Prevent overlapping checks +let lastNotificationTime = 0; // Rate limiting for Discord // Initialize chrome.runtime.onInstalled.addListener(() => { @@ -130,18 +131,28 @@ async function runStockCheck() { const { newProducts, restockedProducts } = processProducts(products); - // Send notifications - if (config.notifyNewProducts) { - for (const product of newProducts) { - if (product.inStock) { - await sendDiscordNotification(product, "new_drop"); - } + // Send notifications with rate limiting + // Limit new product notifications to 5 per check to avoid Discord rate limits + const MAX_NEW_NOTIFICATIONS = 5; + let notificationsSent = 0; + + if (config.notifyNewProducts && newProducts.length > 0) { + const inStockNew = newProducts.filter(p => p.inStock); + if (inStockNew.length > MAX_NEW_NOTIFICATIONS) { + console.log(`Limiting notifications: ${inStockNew.length} new products, only sending ${MAX_NEW_NOTIFICATIONS}`); + } + for (const product of inStockNew.slice(0, MAX_NEW_NOTIFICATIONS)) { + await sendDiscordNotification(product, "new_drop"); + notificationsSent++; + if (notificationsSent > 1) await new Promise(r => setTimeout(r, 1000)); // 1 sec delay between notifications } } + // Always notify restocks (these are high priority) if (config.notifyRestocks) { for (const product of restockedProducts) { await sendDiscordNotification(product, "restock"); + await new Promise(r => setTimeout(r, 1000)); // 1 sec delay } }