Skip to content

Commit

Permalink
migrate: move the code with the useCommand element to its proper posi…
Browse files Browse the repository at this point in the history
…tion

Signed-off-by: Salman Wahib <[email protected]>
  • Loading branch information
sxlmnwb committed Aug 13, 2023
1 parent eda6eb4 commit aeb7c37
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 95 deletions.
95 changes: 2 additions & 93 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -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'),
Expand Down
94 changes: 93 additions & 1 deletion src/useCommand.js
Original file line number Diff line number Diff line change
@@ -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':
Expand All @@ -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;
}
}

0 comments on commit aeb7c37

Please sign in to comment.