diff --git a/app.js b/app.js index 6c758d2..4ad7511 100644 --- a/app.js +++ b/app.js @@ -10,7 +10,7 @@ const Telegraf = require('telegraf'); const config = require('./config.js'); // Utils -const { path } = require('./util/index.js'); +const { path, getText, extractCommand } = require('./util/index.js'); // Lib const validator = require('./lib/validator.js'); @@ -65,8 +65,8 @@ bot.command('ls', bot.command('attach', ctx => { - const text = path(['update', 'message', 'text'], ctx); - const sessionIndex = parseInt(text.replace('/attach ', '').trim()); + const text = getText(ctx); + const sessionIndex = parseInt(extractCommand('attach')(text)); if(Number.isNaN(sessionIndex) || !sessions[sessionIndex]) return responder.fail('Session not found. /ls for list of sessions')(ctx); sessions.currentSession = sessions[sessionIndex]; @@ -76,8 +76,8 @@ bot.command('attach', bot.command('detach', ctx => { - const text = path(['update', 'message', 'text'], ctx); - const sessionIndex = parseInt(text.replace('/detach ', '').trim()); + const text = getText(ctx); + const sessionIndex = parseInt(extractCommand('detach')(text)); const currentSession = text.trim() === '/detach' ? sessions.currentSession : sessions[sessionIndex]; @@ -90,8 +90,8 @@ bot.command('detach', bot.command('kill', ctx => { - const text = path(['update', 'message', 'text'], ctx); - const sessionIndex = parseInt(text.replace('/kill ', '').trim()); + const text = getText(ctx); + const sessionIndex = parseInt(extractCommand('kill')(text)); if(Number.isNaN(sessionIndex) || !sessions[sessionIndex]) return responder.fail('Session not found. /ls for list of sessions.')(ctx); const disconnect = sessions[sessionIndex]; diff --git a/util/index.js b/util/index.js index 06aea6b..d8466b8 100644 --- a/util/index.js +++ b/util/index.js @@ -1,5 +1,11 @@ const path = (path, obj) => path.reduce((result, segment) => result && result[segment], obj); +const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args))); +const getText = ctx => path(['update', 'message', 'text'], ctx) || ''; +const extractCommand = cmd => text => text.replace(`/${cmd}`, '').trim(); module.exports = { path, + compose, + getText, + extractCommand, };