diff --git a/handlers/commands/index.js b/handlers/commands/index.js index 781a850..ed638ff 100644 --- a/handlers/commands/index.js +++ b/handlers/commands/index.js @@ -1,5 +1,7 @@ 'use strict'; +const requireDir = require('require-directory'); +const R = require('ramda'); const { Router } = require('telegraf'); const routingFn = require('./routingFn'); @@ -8,44 +10,8 @@ const router = new Router(routingFn); module.exports = router; -const adminHandler = require('./admin'); -const unAdminHandler = require('./unadmin'); -const leaveCommandHandler = require('./leave'); -const hideGroupHandler = require('./hideGroup'); -const showGroupHandler = require('./showGroup'); -const warnHandler = require('./warn'); -const unwarnHandler = require('./unwarn'); -const nowarnsHandler = require('./nowarns'); -const userHandler = require('./user'); -const banHandler = require('./ban'); -const unbanHandler = require('./unban'); -const reportHandler = require('./report'); -const staffHandler = require('./staff'); -const linkHandler = require('./link'); -const groupsHandler = require('./groups'); -const commandReferenceHandler = require('./commands'); -const addCommandHandler = require('./addCommand'); -const removeCommandHandler = require('./removeCommand'); -const helpHandler = require('./help'); +const exclude = (_, filename) => filename === 'routingFn.js'; +const rename = R.toLower; -router.on('admin', adminHandler); -router.on('unadmin', unAdminHandler); -router.on('leave', leaveCommandHandler); -router.on('hidegroup', hideGroupHandler); -router.on('showgroup', showGroupHandler); -router.on('warn', warnHandler); -router.on('unwarn', unwarnHandler); -router.on('nowarns', nowarnsHandler); -router.on('user', userHandler); -router.on('ban', banHandler); -router.on('unban', unbanHandler); -router.on('report', reportHandler); -router.on('staff', staffHandler); -router.on('link', linkHandler); -router.on('groups', groupsHandler); -router.on('commands', commandReferenceHandler); -router.on('addcommand', addCommandHandler); -router.on('replacecommand', addCommandHandler); -router.on('removecommand', removeCommandHandler); -router.on('start', helpHandler); -router.on('help', helpHandler); +const handlers = requireDir(module, { exclude, rename }); +router.handlers = new Map(Object.entries(handlers)); diff --git a/handlers/commands/replaceCommand.js b/handlers/commands/replaceCommand.js new file mode 100644 index 0000000..cd5c64c --- /dev/null +++ b/handlers/commands/replaceCommand.js @@ -0,0 +1,4 @@ +'use strict'; + +// "clone" the function (https://stackoverflow.com/a/6772648) +module.exports = require('./addCommand').bind(); diff --git a/handlers/commands/start.js b/handlers/commands/start.js new file mode 100644 index 0000000..ab6391f --- /dev/null +++ b/handlers/commands/start.js @@ -0,0 +1,4 @@ +'use strict'; + +// "clone" the function (https://stackoverflow.com/a/6772648) +module.exports = require('./help').bind(); diff --git a/package-lock.json b/package-lock.json index 8261eee..bd49273 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1155,6 +1155,11 @@ } } }, + "require-directory": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", + "integrity": "sha1-jGStX9MNqxyXbiNE/+f3kqam30I=" + }, "require-uncached": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/require-uncached/-/require-uncached-1.0.3.tgz", diff --git a/package.json b/package.json index ea3ec81..5873988 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "nedb-promise": "^2.0.1", "node-fetch": "^2.1.2", "ramda": "^0.25.0", + "require-directory": "^2.1.1", "telegraf": "^3.17.8", "xregexp": "^4.0.0" },