2017-10-04 20:55:50 +03:30
|
|
|
'use strict';
|
|
|
|
|
2018-02-02 15:47:09 +01:00
|
|
|
const { hears } = require('telegraf');
|
|
|
|
const R = require('ramda');
|
|
|
|
|
2017-10-04 20:55:50 +03:30
|
|
|
// DB
|
|
|
|
const { getCommand } = require('../../stores/command');
|
|
|
|
|
2018-02-02 15:47:09 +01:00
|
|
|
const capitalize = R.replace(/^./, R.toUpper);
|
|
|
|
|
|
|
|
const getRepliedToId = R.path([ 'reply_to_message', 'message_id' ]);
|
|
|
|
|
|
|
|
const typeToMethod = type =>
|
|
|
|
type === 'text'
|
|
|
|
? 'replyWithHTML'
|
|
|
|
: `replyWith${capitalize(type)}`;
|
|
|
|
|
2017-10-04 20:55:50 +03:30
|
|
|
const runCustomCmdHandler = async (ctx, next) => {
|
2017-10-06 16:34:23 +03:30
|
|
|
const { message, state } = ctx;
|
|
|
|
const { isAdmin, isMaster } = state;
|
2017-10-04 20:55:50 +03:30
|
|
|
|
2018-02-02 15:47:09 +01:00
|
|
|
const commandName = ctx.match[1].toLowerCase();
|
2017-10-04 20:55:50 +03:30
|
|
|
const command = await getCommand({ isActive: true, name: commandName });
|
|
|
|
|
|
|
|
if (!command) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2017-10-10 21:54:59 +03:30
|
|
|
const { caption, content, type } = command;
|
|
|
|
const role = command.role.toLowerCase();
|
2017-10-04 20:55:50 +03:30
|
|
|
if (
|
2017-10-10 21:54:59 +03:30
|
|
|
role === 'master' &&
|
2017-10-06 16:34:23 +03:30
|
|
|
!isMaster ||
|
2017-10-10 21:54:59 +03:30
|
|
|
role === 'admins' &&
|
2017-10-06 16:34:23 +03:30
|
|
|
!isAdmin
|
2017-10-04 20:55:50 +03:30
|
|
|
) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2018-02-02 15:47:09 +01:00
|
|
|
const reply_to_message_id = getRepliedToId(message);
|
|
|
|
const options = {
|
|
|
|
caption,
|
|
|
|
disable_web_page_preview: true,
|
|
|
|
reply_to_message_id,
|
|
|
|
};
|
|
|
|
|
|
|
|
return ctx[typeToMethod(type)](content, options);
|
2017-10-04 20:55:50 +03:30
|
|
|
};
|
|
|
|
|
2019-04-20 11:40:03 +02:00
|
|
|
module.exports = hears(/^! ?(\w+)/, runCustomCmdHandler);
|