From 0d0a45f3b1c344781d5bb66a1d19c8299c95d0f2 Mon Sep 17 00:00:00 2001 From: Salman Wahib Date: Sat, 12 Aug 2023 03:08:30 +0000 Subject: [PATCH] useLoopReq: added useTestnet for looping - the notion of updating stop to --stop Signed-off-by: Salman Wahib --- index.js | 6 ++-- src/useCommand.js | 4 +-- src/useLoopReq.js | 89 ++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 89 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 0013a18..b9a8db3 100755 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ require('dotenv').config(); const { useMainnet, useTestnet, usePing, useCommand, startWhatsapp } = require('./src'); -const { startLoop, stopLoop } = require('./src/useLoopReq'); +const { startMainnetLoop, startTestnetLoop, stopLoop } = require('./src/useLoopReq'); const start = async () => { const client = await startWhatsapp(); @@ -28,7 +28,7 @@ client.on('message', async msg => { if (intervalString === '--stop') { stopLoop(networks, chat); } else { - startLoop(client, networks, intervalString, chat); + startMainnetLoop(client, networks, intervalString, chat); } } else if (commandParts.length === 2) { const network = commandParts[1]; @@ -52,7 +52,7 @@ client.on('message', async msg => { if (intervalString === '--stop') { stopLoop(networks, chat); } else { - startLoop(client, networks, intervalString, chat); + startTestnetLoop(client, networks, intervalString, chat); } } else if (commandParts.length === 2) { const network = commandParts[1]; diff --git a/src/useCommand.js b/src/useCommand.js index 91379c6..908b607 100644 --- a/src/useCommand.js +++ b/src/useCommand.js @@ -7,13 +7,13 @@ module.exports = function useCommand(msg) { return '```/mainnet ```\n```/mainnet ```\n```/mainnet --help```'; case '/mainnet --help': - return 'This tool was created to simplify validator monitoring with cosmos based node, with .env COSMOS_* variables that you can customize.\n\nFor use ```/testnet ```, where `````` is a number followed by "m" (for minutes) or "h" (for hours).\n\nFor example :\n```/mainnet cosmos```\n```/mainnet cosmos 1m```\n```/mainnet cosmos 12h```\n```/mainnet cosmos stop```'; + return 'This tool was created to simplify validator monitoring with cosmos based node, with .env COSMOS_* variables that you can customize.\n\nFor use ```/testnet ```, where `````` is a number followed by "m" (for minutes) or "h" (for hours).\n\nFor example :\n```/mainnet cosmos```\n```/mainnet cosmos 1m```\n```/mainnet cosmos 12h```\n```/mainnet cosmos --stop```'; case '/testnet': return '```/testnet ```\n```/testnet ```\n```/testnet --help```'; case '/testnet --help': - return 'This tool was created to very simplify validator monitoring with cosmos based node, with .env COSMOS_* variables that you can customize.\n\nFor use ```/testnet ```, where `````` is a number followed by "m" (for minutes) or "h" (for hours).\n\nFor example :\n```/testnet cosmost```\n```/testnet cosmost 1m```\n```/testnet cosmost 12h```\n```/testnet cosmost stop```'; + return 'This tool was created to very simplify validator monitoring with cosmos based node, with .env COSMOS_* variables that you can customize.\n\nFor use ```/testnet ```, where `````` is a number followed by "m" (for minutes) or "h" (for hours).\n\nFor example :\n```/testnet cosmost```\n```/testnet cosmost 1m```\n```/testnet cosmost 12h```\n```/testnet cosmost --stop```'; case '/ping': return '```/ping ```\n```/ping --help```'; diff --git a/src/useLoopReq.js b/src/useLoopReq.js index 5afa88e..1d51950 100644 --- a/src/useLoopReq.js +++ b/src/useLoopReq.js @@ -1,5 +1,5 @@ const useMainnet = require('./useMainnet'); -// const useTestnet = require('./useTestnet'); +const useTestnet = require('./useTestnet'); let networks = {}; @@ -23,7 +23,7 @@ const stopLoop = async (networkInputs, chat) => { }; module.exports = { - startLoop: async (client, networkInputs, intervalString, chat) => { + startMainnetLoop: async (client, networkInputs, intervalString, chat) => { const stopIndex = networkInputs.indexOf('--stop'); if (stopIndex !== -1) { networkInputs.splice(stopIndex, 1); @@ -34,7 +34,7 @@ module.exports = { const intervalNumber = parseInt(intervalString.slice(0, -1)); const intervalUnit = intervalString.slice(-1); if (isNaN(intervalNumber) || (intervalUnit !== 'm' && intervalUnit !== 'h')) { - chat.sendMessage('Invalid command format. Use ```/status ```, where `````` is a number followed by "m" (for minutes) or "h" (for hours).'); + chat.sendMessage('Invalid command format. Use ```/mainnet ```, where `````` is a number followed by "m" (for minutes) or "h" (for hours).'); return; } const intervalMillis = intervalUnit === 'm' ? intervalNumber * 60 * 1000 : intervalNumber * 60 * 60 * 1000; @@ -62,7 +62,7 @@ module.exports = { intervalId: setInterval(updateStatus, intervalMillis) }; }); - + const messageIntervalId = setInterval(async () => { let statusMessage = ''; for (const networkInput of networkInputs) { @@ -99,7 +99,86 @@ module.exports = { } }); - chat.sendMessage(`Started sending ${networkInputs.join(', ')} status updates every ${intervalNumber} ${intervalUnitWord}.`); + chat.sendMessage(`Started sending mainnet ${networkInputs.join(', ')} status updates every ${intervalNumber} ${intervalUnitWord}.`); + }, + + startTestnetLoop: async (client, networkInputs, intervalString, chat) => { + const stopIndex = networkInputs.indexOf('--stop'); + if (stopIndex !== -1) { + networkInputs.splice(stopIndex, 1); + stopLoop(networkInputs, chat); + return; + } + + const intervalNumber = parseInt(intervalString.slice(0, -1)); + const intervalUnit = intervalString.slice(-1); + if (isNaN(intervalNumber) || (intervalUnit !== 'm' && intervalUnit !== 'h')) { + chat.sendMessage('Invalid command format. Use ```/testnet ```, where `````` is a number followed by "m" (for minutes) or "h" (for hours).'); + return; + } + const intervalMillis = intervalUnit === 'm' ? intervalNumber * 60 * 1000 : intervalNumber * 60 * 60 * 1000; + + let intervalUnitWord; + if (intervalUnit === 'm') { + intervalUnitWord = intervalNumber > 1 ? 'minutes' : 'minute'; + } else if (intervalUnit === 'h') { + intervalUnitWord = intervalNumber > 1 ? 'hours' : 'hour'; + } + + networkInputs.forEach(networkInput => { + if (networks[networkInput] && networks[networkInput].intervalId) { + clearInterval(networks[networkInput].intervalId); + } + + const updateStatus = async () => { + const statusTestnet = await useTestnet(networkInput); + networks[networkInput].lastStatusTestnet = statusTestnet; + }; + + updateStatus(); + + networks[networkInput] = { + intervalId: setInterval(updateStatus, intervalMillis) + }; + }); + + const messageIntervalId = setInterval(async () => { + let statusMessage = ''; + for (const networkInput of networkInputs) { + if (networks[networkInput] && networks[networkInput].lastStatusTestnet) { + statusMessage += networks[networkInput].lastStatusTestnet + `\n------------------------------------------------------------------\n`; + } + } + + networkInputs.forEach(async networkInput => { + 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; + } + }); + + if (statusMessage.trim() !== '') { + const newStatusMessage = await chat.sendMessage(statusMessage.trim()); + + networkInputs.forEach(networkInput => { + if (networks[networkInput]) { + networks[networkInput].lastStatusMessage = newStatusMessage; + } + }); + } + }, intervalMillis); + + networkInputs.forEach(networkInput => { + if (networks[networkInput]) { + networks[networkInput].messageIntervalId = messageIntervalId; + } + }); + + chat.sendMessage(`Started sending testnet ${networkInputs.join(', ')} status updates every ${intervalNumber} ${intervalUnitWord}.`); }, stopLoop