Trying to debounce PIL > Channel ID
Some checks are pending
ptero-push / build (push) Waiting to run
Some checks are pending
ptero-push / build (push) Waiting to run
This commit is contained in:
parent
92861365b2
commit
5039f566ea
19
index.js
19
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 handleDiscord = function (data) {
|
||||||
const rawBody = data.data.body;
|
const rawBody = data.data.body;
|
||||||
const text = data.data.productText;
|
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;
|
if (!filters.some((filter) => body.string.toLowerCase().includes(filter)) && !filters.some((filter) => text.toLowerCase().includes(filter))) return;
|
||||||
thisMsg = JSON.parse(JSON.stringify(discordMsg));
|
thisMsg = JSON.parse(JSON.stringify(discordMsg));
|
||||||
thisMsg.content = row.custommessage || null;
|
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) => {
|
channel.send(thisMsg).catch((err) => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
}).then((msg) => {
|
}).then((msg) => {
|
||||||
|
|
Loading…
Reference in a new issue