forked from ChrisChrome/weather-bot
Add automatic retry to fetching product text for filtering
This commit is contained in:
parent
9393bf8f94
commit
a31573b7c4
57
index.js
57
index.js
|
@ -421,19 +421,26 @@ xmpp.on("stanza", (stanza) => {
|
||||||
if (!channel) return console.log(`${colors.red("[ERROR]")} Channel ${row.channelid} not found`);
|
if (!channel) return console.log(`${colors.red("[ERROR]")} Channel ${row.channelid} not found`);
|
||||||
|
|
||||||
// fetch the product text
|
// fetch the product text
|
||||||
fetch(`https://mesonet.agron.iastate.edu/api/1/nwstext/${product_id_raw}`).then((res) => {
|
trySend = () => {
|
||||||
// If neither the body nor the product text contains the filter, ignore it
|
fetch(`https://mesonet.agron.iastate.edu/api/1/nwstext/${product_id_raw}`).then((res) => {
|
||||||
res.text().then((text) => {
|
// If neither the body nor the product text contains the filter, ignore it
|
||||||
if (!filters.some((filter) => body.toLowerCase().includes(filter)) && !filters.some((filter) => text.toLowerCase().includes(filter))) return;
|
res.text().then((text) => {
|
||||||
thisMsg = JSON.parse(JSON.stringify(discordMsg));
|
if (!filters.some((filter) => body.toLowerCase().includes(filter)) && !filters.some((filter) => text.toLowerCase().includes(filter))) return;
|
||||||
thisMsg.content = row.custommessage || null;
|
thisMsg = JSON.parse(JSON.stringify(discordMsg));
|
||||||
channel.send(thisMsg).catch((err) => {
|
thisMsg.content = row.custommessage || null;
|
||||||
console.error(err);
|
channel.send(thisMsg).catch((err) => {
|
||||||
}).then((msg) => {
|
console.error(err);
|
||||||
if (msg.channel.type === Discord.ChannelType.GuildAnnouncement) msg.crosspost();
|
}).then((msg) => {
|
||||||
|
if (msg.channel.type === Discord.ChannelType.GuildAnnouncement) msg.crosspost();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
}).catch((err) => {
|
||||||
|
setTimeout(() => {
|
||||||
|
console.log(`${colors.red("[ERROR]")} Failed to fetch product text, retrying... ${err}`)
|
||||||
|
trySend();
|
||||||
|
})
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -460,18 +467,24 @@ xmpp.on("stanza", (stanza) => {
|
||||||
if (!user) return console.log(`${colors.red("[ERROR]")} User ${row.userid} not found`);
|
if (!user) return console.log(`${colors.red("[ERROR]")} User ${row.userid} not found`);
|
||||||
|
|
||||||
// fetch the product text
|
// fetch the product text
|
||||||
fetch(`https://mesonet.agron.iastate.edu/api/1/nwstext/${product_id_raw}`).then((res) => {
|
trySend = () => {
|
||||||
// If neither the body nor the product text contains the filter, ignore it
|
fetch(`https://mesonet.agron.iastate.edu/api/1/nwstext/${product_id_raw}`).then((res) => {
|
||||||
res.text().then((text) => {
|
// If neither the body nor the product text contains the filter, ignore it
|
||||||
if (!filters.some((filter) => body.toLowerCase().includes(filter)) && !filters.some((filter) => text.toLowerCase().includes(filter))) return;
|
res.text().then((text) => {
|
||||||
thisMsg = JSON.parse(JSON.stringify(discordMsg));
|
if (!filters.some((filter) => body.toLowerCase().includes(filter)) && !filters.some((filter) => text.toLowerCase().includes(filter))) return;
|
||||||
thisMsg.content = row.custommessage || null;
|
thisMsg = JSON.parse(JSON.stringify(discordMsg));
|
||||||
user.send(thisMsg).catch((err) => {
|
thisMsg.content = row.custommessage || null;
|
||||||
console.error(err);
|
user.send(thisMsg).catch((err) => {
|
||||||
|
console.error(err);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
}).catch((err) => {
|
||||||
});
|
setTimeout(() => {
|
||||||
|
console.log(`${colors.red("[ERROR]")} Failed to fetch product text, retrying... ${err}`)
|
||||||
|
trySend();
|
||||||
|
})
|
||||||
|
});;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue