From 5039f566ea5ed8d8f719051179a2c872f02b2d4d Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Mon, 26 May 2025 01:08:15 -0600 Subject: [PATCH] Trying to debounce PIL > Channel ID --- index.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/index.js b/index.js index 26bf4ea..508a174 100644 --- a/index.js +++ b/index.js @@ -355,6 +355,8 @@ Image and product text are optional, but if they exist, they will be in the data */ +var sent = {} + const handleDiscord = function (data) { const rawBody = data.data.body; const text = data.data.productText; @@ -428,6 +430,23 @@ const handleDiscord = function (data) { if (!filters.some((filter) => body.string.toLowerCase().includes(filter)) && !filters.some((filter) => text.toLowerCase().includes(filter))) return; thisMsg = JSON.parse(JSON.stringify(discordMsg)); thisMsg.content = row.custommessage || null; + // Debounce by channel id and pil to prevent duplicate alerts + if (!sent[row.channelid]) sent[row.channelid] = {}; + if (sent[row.channelid][product_id.pil]) { + // Already sent this pil to this channel recently, skip + console.log(`${colors.yellow("[WARN]")} Already sent ${product_id.pil} to ${channel.guild.name}/${channel.name} (${channel.guild.id}/${channel.id}), skipping...`); + return; + } + sent[row.channelid][product_id.pil] = Date.now(); + // Optionally, clean up old entries after some time (e.g., 10 minutes) + setTimeout(() => { + if (sent[row.channelid]) { + delete sent[row.channelid][product_id.pil]; + if (Object.keys(sent[row.channelid]).length === 0) { + delete sent[row.channelid]; + } + } + }, 10 * 60 * 1000); channel.send(thisMsg).catch((err) => { console.error(err); }).then((msg) => {