mirror of
https://github.com/franjsco/lscanbot
synced 2025-02-16 11:31:37 +01:00
refactoring promises
This commit is contained in:
parent
9c600000d3
commit
5824f3abc2
15
index.js
15
index.js
@ -24,15 +24,8 @@ bot.on('/help', (msg) => {
|
||||
bot.on('/scan', (msg) => {
|
||||
if (utils.isAuthorizedUser(msg.from.id)) {
|
||||
scanner.scanNetwork(msg.from.id)
|
||||
.then(() => {
|
||||
scanner.getDevicesNetwork(msg.from.id)
|
||||
.then((devices) => {
|
||||
bot.sendMessage(msg.from.id, utils.templateDevicesList(devices), { parseMode: 'Markdown' });
|
||||
})
|
||||
.catch((err) => {
|
||||
msg.reply.text('Error, check the error.log');
|
||||
logger.logError(err);
|
||||
});
|
||||
.then((devices) => {
|
||||
bot.sendMessage(msg.from.id, utils.templateDevicesList(devices), { parseMode: 'Markdown' } );
|
||||
})
|
||||
.catch((err) => {
|
||||
msg.reply.text('Error, check the error.log');
|
||||
@ -70,8 +63,8 @@ bot.on(/^\/remove (.+)$/, (msg, props) => {
|
||||
}
|
||||
});
|
||||
|
||||
bot.on('/getUID', (msg) => {
|
||||
msg.reply.text(`⚠️ UID: ${msg.from.id}`);
|
||||
bot.on('/me', (msg) => {
|
||||
msg.reply.text(`⚠️ My UID: ${msg.from.id}`);
|
||||
});
|
||||
|
||||
scanner.initScannerDB();
|
||||
|
44
scanner.js
44
scanner.js
@ -2,52 +2,31 @@ const arpScanner = require('arpscan/promise');
|
||||
const appConfig = require('./config.json');
|
||||
const db = require('./db');
|
||||
|
||||
function scannerResults(data) {
|
||||
return data;
|
||||
}
|
||||
|
||||
function returnDevices(data) {
|
||||
return data;
|
||||
}
|
||||
|
||||
function returnDevicesError(err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
function returnInventory(data) {
|
||||
return data;
|
||||
}
|
||||
|
||||
function returnInventoryError(err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
function scannerError(err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
function scanNetwork(userId) {
|
||||
db.initScan(userId);
|
||||
return (
|
||||
arpScanner(appConfig.scanner)
|
||||
.then(scannerResults)
|
||||
.then(data => db.addDevices(userId, data))
|
||||
.catch(scannerError)
|
||||
.then(data => {
|
||||
db.addDevices(userId, data)
|
||||
return userId
|
||||
})
|
||||
.then(getDevicesNetwork)
|
||||
.catch((err) => { return err})
|
||||
);
|
||||
}
|
||||
|
||||
function getDevicesNetwork(userId) {
|
||||
return (db.getDevices(userId)
|
||||
.then(returnDevices)
|
||||
.catch(returnDevicesError)
|
||||
.then(data => {return data})
|
||||
.catch(err => {return err})
|
||||
);
|
||||
}
|
||||
|
||||
function getInventoryNetwork() {
|
||||
return (
|
||||
db.getInventory()
|
||||
.then(returnInventory)
|
||||
.catch(returnInventoryError)
|
||||
.then(data => {return data})
|
||||
.catch(err => {return err})
|
||||
);
|
||||
}
|
||||
|
||||
@ -60,7 +39,7 @@ function addDeviceToTrack(data) {
|
||||
const devArg = {
|
||||
device: data[0],
|
||||
owner: data[1],
|
||||
mac: data[2],
|
||||
mac: data[2].toUpperCase(),
|
||||
};
|
||||
db.addDeviceToInventory(devArg);
|
||||
}
|
||||
@ -71,7 +50,6 @@ function removeDeviceToTrack(mac) {
|
||||
|
||||
module.exports.initScannerDB = initScannerDB;
|
||||
module.exports.scanNetwork = scanNetwork;
|
||||
module.exports.getDevicesNetwork = getDevicesNetwork;
|
||||
module.exports.getInventoryNetwork = getInventoryNetwork;
|
||||
module.exports.addDeviceToTrack = addDeviceToTrack;
|
||||
module.exports.removeDeviceToTrack = removeDeviceToTrack;
|
||||
|
Loading…
x
Reference in New Issue
Block a user