diff --git a/index.js b/index.js index 9e3aee2..e6715e1 100755 --- a/index.js +++ b/index.js @@ -4,8 +4,7 @@ require('dotenv').config(); const chalk = require('chalk'); -const { useMainnet, useTestnet, usePing, useCommand, startWhatsapp } = require('./src'); -const { startMainnetLoop, startTestnetLoop, stopLoop } = require('./src/useLoopReq'); +const { useCommand, startWhatsapp } = require('./src'); const start = async () => { const client = await startWhatsapp(); @@ -31,101 +30,11 @@ client.on('message', async msg => { const chat = await msg.getChat(); - const reply = useCommand(msg); + const reply = await useCommand(msg, client, chat); if (reply) { msg.reply(reply); } - if (msg.body.startsWith('/mainnet ')) { - const commandParts = msg.body.split(' '); - if (commandParts.length >= 3) { - const networks = commandParts.slice(1, -1); - const intervalString = commandParts[commandParts.length - 1]; - const errors = []; - - if (intervalString === '--stop') { - stopLoop(networks, chat); - } else { - for (const network of networks) { - try { - await useMainnet(network); - } catch (error) { - console.error(error); - errors.push(error.message); - } - } - if (errors.length > 0) { - msg.reply(errors.join('\n')); - } else { - startMainnetLoop(client, networks, intervalString, chat); - } - } - - } else if (commandParts.length === 2) { - const network = commandParts[1]; - if (network !== '--help') { - try { - msg.reply(await useMainnet(network)); - } catch (error) { - console.error(error); - msg.reply(error.message); - } - } - } - } - - if (msg.body.startsWith('/testnet ')) { - const commandParts = msg.body.split(' '); - if (commandParts.length >= 3) { - const networks = commandParts.slice(1, -1); - const intervalString = commandParts[commandParts.length - 1]; - const errors = []; - - if (intervalString === '--stop') { - stopLoop(networks, chat); - } else { - for (const network of networks) { - try { - await useTestnet(network); - } catch (error) { - console.error(error); - errors.push(error.message); - } - } - if (errors.length > 0) { - msg.reply(errors.join('\n')); - } else { - startTestnetLoop(client, networks, intervalString, chat); - } - } - - } else if (commandParts.length === 2) { - const network = commandParts[1]; - if (network !== '--help') { - try { - msg.reply(await useTestnet(network)); - } catch (error) { - console.error(error); - msg.reply(error.message); - } - } - } - } - - if (msg.body.startsWith('/ping ')) { - const url = msg.body.replace('/ping ', ''); - if (url !== '--help') { - usePing(url) - .then(response => { - msg.reply(response); - }) - .catch(error => { - console.error(error); - msg.reply(error.message); - }); - } - } - }); client.initialize(); diff --git a/src/index.js b/src/index.js index 6b55959..b70b005 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ module.exports = { startWhatsapp: require('./startWhatsapp'), useCommand: require('./useCommand'), - // useHelpCommand: require('./useHelpCommand'), + useHelpCommand: require('./useHelpCommand'), usePing: require('./usePing'), useMainnet: require('./useMainnet'), useTestnet: require('./useTestnet'), diff --git a/src/useCommand.js b/src/useCommand.js index e0848ef..c34b287 100644 --- a/src/useCommand.js +++ b/src/useCommand.js @@ -1,6 +1,10 @@ +const useMainnet = require('./useMainnet'); +const useTestnet = require('./useTestnet'); +const usePing = require('./usePing'); +const { startMainnetLoop, startTestnetLoop, stopLoop } = require('./useLoopReq'); const useHelpCommand = require('./useHelpCommand'); -module.exports = function useCommand(msg) { +module.exports = async function useCommand(msg, client, chat) { switch (msg.body) { case '/mainnet': @@ -23,5 +27,93 @@ module.exports = function useCommand(msg) { case '/help': return useHelpCommand(); + + default: + if (msg.body.startsWith('/mainnet ')) { + const commandParts = msg.body.split(' '); + if (commandParts.length >= 3) { + const networks = commandParts.slice(1, -1); + const intervalString = commandParts[commandParts.length - 1]; + const errors = []; + + if (intervalString === '--stop') { + stopLoop(networks, chat); + } else { + for (const network of networks) { + try { + await useMainnet(network); + } catch (error) { + console.error(error); + errors.push(error.message); + } + } + if (errors.length > 0) { + msg.reply(errors.join('\n')); + } else { + startMainnetLoop(client, networks, intervalString, chat); + } + } + + } else if (commandParts.length === 2) { + const network = commandParts[1]; + if (network !== '--help') { + try { + msg.reply(await useMainnet(network)); + } catch (error) { + console.error(error); + msg.reply(error.message); + } + } + } + } else if ( + msg.body.startsWith('/testnet ')) { + const commandParts = msg.body.split(' '); + if (commandParts.length >= 3) { + const networks = commandParts.slice(1, -1); + const intervalString = commandParts[commandParts.length - 1]; + const errors = []; + + if (intervalString === '--stop') { + stopLoop(networks, chat); + } else { + for (const network of networks) { + try { + await useTestnet(network); + } catch (error) { + console.error(error); + errors.push(error.message); + } + } + if (errors.length > 0) { + msg.reply(errors.join('\n')); + } else { + startTestnetLoop(client, networks, intervalString, chat); + } + } + + } else if (commandParts.length === 2) { + const network = commandParts[1]; + if (network !== '--help') { + try { + msg.reply(await useTestnet(network)); + } catch (error) { + console.error(error); + msg.reply(error.message); + } + } + } + } else if (msg.body.startsWith('/ping ')) { + const url = msg.body.replace('/ping ', ''); + if (url !== '--help') { + try { + const response = await usePing(url); + msg.reply(response); + } catch (error) { + console.error(error); + msg.reply(error.message); + } + } + } + break; } }