diff --git a/handlers/commands/index.js b/handlers/commands/index.js new file mode 100644 index 0000000..827829d --- /dev/null +++ b/handlers/commands/index.js @@ -0,0 +1,44 @@ +'use strict'; + +const { Composer } = require('telegraf'); + +const composer = new Composer(); + +const adminHandler = require('./admin'); +const unAdminHandler = require('./unadmin'); +const leaveCommandHandler = require('./leave'); +const warnHandler = require('./warn'); +const unwarnHandler = require('./unwarn'); +const nowarnsHandler = require('./nowarns'); +const getWarnsHandler = require('./getwarns'); +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'); + +composer.command('admin', adminHandler); +composer.command('unadmin', unAdminHandler); +composer.command('leave', leaveCommandHandler); +composer.command('warn', warnHandler); +composer.command('unwarn', unwarnHandler); +composer.command('nowarns', nowarnsHandler); +composer.command('getwarns', getWarnsHandler); +composer.command('ban', banHandler); +composer.command('unban', unbanHandler); +composer.command('report', reportHandler); +composer.hears(/^@admins?\s?/i, reportHandler); +composer.command('staff', staffHandler); +composer.command('link', linkHandler); +composer.command('groups', groupsHandler); +composer.command('commands', commandReferenceHandler); +composer.command('addcommand', addCommandHandler); +composer.command('removecommand', removeCommandHandler); +composer.command([ 'start', 'help' ], helpHandler); + +module.exports = composer; diff --git a/handlers/messages/index.js b/handlers/messages/index.js new file mode 100644 index 0000000..99c2709 --- /dev/null +++ b/handlers/messages/index.js @@ -0,0 +1,21 @@ +'use strict'; + +const { Composer } = require('telegraf'); + +const composer = new Composer(); + +const addUserHandler = require('./addUser'); +const removeLinksHandler = require('./removeLinks'); +const checkUsernameHandler = require('./checkUsername'); +const addCustomCmdHandler = require('./addCustomCmd'); +const runCustomCmdHandler = require('./runCustomCmd'); + +composer.on('message', + addUserHandler, + removeLinksHandler, + checkUsernameHandler, + addCustomCmdHandler, + runCustomCmdHandler +); + +module.exports = composer; diff --git a/handlers/middlewares/index.js b/handlers/middlewares/index.js new file mode 100644 index 0000000..73287f6 --- /dev/null +++ b/handlers/middlewares/index.js @@ -0,0 +1,27 @@ +'use strict'; + +const { Composer } = require('telegraf'); + +const composer = new Composer(); + +const { deleteAfter } = require('../../utils/tg'); +const delTimeout = 2 * 60 * 1000; + +const leaveUnmanagedHandler = require('./leaveUnmanaged'); +const removeCommandsHandler = require('./removeCommands'); +const kickBannedHandler = require('./kickBanned'); +const syncStatusHandler = require('./syncStatus'); +const antibotHandler = require('./antibot'); +const addedToGroupHandler = require('./addedToGroup'); + +composer.on('new_chat_members', addedToGroupHandler); +composer.use(leaveUnmanagedHandler); +composer.use(removeCommandsHandler); +composer.use(kickBannedHandler); +composer.on('new_chat_members', syncStatusHandler); +composer.on('new_chat_members', antibotHandler); +composer.on( + [ 'new_chat_members', 'left_chat_member' ], + deleteAfter(delTimeout)); + +module.exports = composer; diff --git a/index.js b/index.js index 2744c25..c32176b 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,6 @@ 'use strict'; // Utils -const { deleteAfter } = require('./utils/tg'); const { logError } = require('./utils/log'); /** @@ -14,96 +13,12 @@ bot.telegram.getMe().then((botInfo) => { bot.options.username = botInfo.username; }); - -/** - * @type {number} - * Time in milliseconds after which join and leave messages should be deleted - */ -const delTimeout = 2 * 60 * 1000; - -/** - * @type {string} - * Path of middlewares and commands - */ -const middleware = './handlers/middlewares'; -const message = './handlers/messages'; -const command = './handlers/commands'; - -/** - * @type {function} - * Middleware Handlers - */ -const leaveUnmanagedHandler = require(`${middleware}/leaveUnmanaged`); -const removeCommandsHandler = require(`${middleware}/removeCommands`); -const kickBannedHandler = require(`${middleware}/kickBanned`); -const syncStatusHandler = require(`${middleware}/syncStatus`); -const antibotHandler = require(`${middleware}/antibot`); -const addedToGroupHandler = require(`${middleware}/addedToGroup`); - -/** - * @type {function} - * Messages Handlers - */ -const addUserHandler = require(`${message}/addUser`); -const removeLinksHandler = require(`${message}/removeLinks`); -const checkUsernameHandler = require(`${message}/checkUsername`); -const addCustomCmdHandler = require(`${message}/addCustomCmd`); -const runCustomCmdHandler = require(`${message}/runCustomCmd`); - -/** - * @type {function} - * Commmands Handlers - */ -const adminHandler = require(`${command}/admin`); -const unAdminHandler = require(`${command}/unadmin`); -const leaveCommandHandler = require(`${command}/leave`); -const warnHandler = require(`${command}/warn`); -const unwarnHandler = require(`${command}/unwarn`); -const nowarnsHandler = require(`${command}/nowarns`); -const getWarnsHandler = require(`${command}/getwarns`); -const banHandler = require(`${command}/ban`); -const unbanHandler = require(`${command}/unban`); -const reportHandler = require(`${command}/report`); -const staffHandler = require(`${command}/staff`); -const linkHandler = require(`${command}/link`); -const groupsHandler = require(`${command}/groups`); -const commandReferenceHandler = require(`${command}/commands`); -const addCommandHandler = require(`${command}/addCommand`); -const removeCommandHandler = require(`${command}/removeCommand`); -const helpHandler = require(`${command}/help`); - -bot.on('new_chat_members', addedToGroupHandler); -bot.use(leaveUnmanagedHandler); -bot.use(removeCommandsHandler); -bot.use(kickBannedHandler); -bot.on('message', - addUserHandler, - removeLinksHandler, - checkUsernameHandler, - addCustomCmdHandler, - runCustomCmdHandler +bot.use( + require('./handlers/middlewares'), + require('./handlers/messages'), + require('./handlers/commands'), ); -bot.on('new_chat_members', syncStatusHandler); -bot.on('new_chat_members', antibotHandler); -bot.on([ 'new_chat_members', 'left_chat_member' ], deleteAfter(delTimeout)); -bot.command('admin', adminHandler); -bot.command('unadmin', unAdminHandler); -bot.command('leave', leaveCommandHandler); -bot.command('warn', warnHandler); -bot.command('unwarn', unwarnHandler); -bot.command('nowarns', nowarnsHandler); -bot.command('getwarns', getWarnsHandler); -bot.command('ban', banHandler); -bot.command('unban', unbanHandler); -bot.command('report', reportHandler); -bot.hears(/^@admins?\s?/i, reportHandler); -bot.command('staff', staffHandler); -bot.command('link', linkHandler); -bot.command('groups', groupsHandler); -bot.command('commands', commandReferenceHandler); -bot.command('addcommand', addCommandHandler); -bot.command('removecommand', removeCommandHandler); -bot.command([ 'start', 'help' ], helpHandler); + bot.catch(logError);