Skip to content

Commit

Permalink
useLoopReq: fix after stop selected network and back to looping
Browse files Browse the repository at this point in the history
- whatsapp-web.js: ^1.22.1

Signed-off-by: Salman Wahib <[email protected]>
  • Loading branch information
sxlmnwb committed Aug 11, 2023
1 parent ac1d1db commit f3a691f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"ping": "^0.4.4",
"qrcode-terminal": "^0.12.0",
"validator": "^13.9.0",
"whatsapp-web.js": "^1.21.0"
"whatsapp-web.js": "^1.22.1"
},
"devDependencies": {
"@babel/cli": "^7.22.9",
Expand Down
52 changes: 33 additions & 19 deletions src/useLoopReq.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ let networks = {};

const stopLoop = async (networkInputs, chat) => {
networkInputs.forEach(async networkInput => {
if (networks[networkInput] && networks[networkInput].intervalId) {
clearInterval(networks[networkInput].intervalId);
clearInterval(networks[networkInput].messageIntervalId);
networks[networkInput].intervalId = null;
networks[networkInput].messageIntervalId = null;
if (networks[networkInput]) {
if (networks[networkInput].intervalId) {
clearInterval(networks[networkInput].intervalId);
networks[networkInput].intervalId = null;
}
if (networks[networkInput].lastStatusMessage) {
await networks[networkInput].lastStatusMessage.delete(true);
networks[networkInput].lastStatusMessage = null;
}
chat.sendMessage(`Stopped sending ${networkInput} status updates.`);
delete networks[networkInput];
} else {
chat.sendMessage(`ERROR no status ${networkInput} updates to stop 💀⁉️`);
}
Expand All @@ -23,10 +24,12 @@ const stopLoop = async (networkInputs, chat) => {

module.exports = {
startLoop: async (client, networkInputs, intervalString, chat) => {
if (intervalString === '--stop') {
const stopIndex = networkInputs.indexOf('--stop');
if (stopIndex !== -1) {
networkInputs.splice(stopIndex, 1);
stopLoop(networkInputs, chat);
return;
}
return;
}

const intervalNumber = parseInt(intervalString.slice(0, -1));
const intervalUnit = intervalString.slice(-1);
Expand Down Expand Up @@ -63,30 +66,41 @@ module.exports = {
const messageIntervalId = setInterval(async () => {
let statusMessage = '';
for (const networkInput of networkInputs) {
if (networks[networkInput].lastStatusMainnet) {
if (networks[networkInput] && networks[networkInput].lastStatusMainnet) {
statusMessage += networks[networkInput].lastStatusMainnet + `\n------------------------------------------------------------------\n`;
}
}

networkInputs.forEach(async networkInput => {
if (networks[networkInput].lastStatusMessage) {
await networks[networkInput].lastStatusMessage.delete(true);
if (networks[networkInput] && networks[networkInput].lastStatusMessage) {
try {
await networks[networkInput].lastStatusMessage.delete(true);
} catch (error) {
console.error(`WARN [${networkInput}] not delete`);
}
networks[networkInput].lastStatusMessage = null;
}
});

const newStatusMessage = await chat.sendMessage(statusMessage.trim());
if (statusMessage.trim() !== '') {
const newStatusMessage = await chat.sendMessage(statusMessage.trim());

networkInputs.forEach(networkInput => {
networks[networkInput].lastStatusMessage = newStatusMessage;
});
networkInputs.forEach(networkInput => {
if (networks[networkInput]) {
networks[networkInput].lastStatusMessage = newStatusMessage;
}
});
}
}, intervalMillis);

networkInputs.forEach(networkInput => {
networks[networkInput].messageIntervalId = messageIntervalId;
if (networks[networkInput]) {
networks[networkInput].messageIntervalId = messageIntervalId;
}
});

chat.sendMessage(`Started sending ${networkInputs.join(', ')} status updates every ${intervalNumber} ${intervalUnitWord}.`);
},

stopLoop
};

0 comments on commit f3a691f

Please sign in to comment.