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 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user