2017-10-04 20:55:50 +03:30
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// DB
|
|
|
|
const { getCommand } = require('../../stores/command');
|
|
|
|
|
|
|
|
const runCustomCmdHandler = async (ctx, next) => {
|
2017-10-06 16:34:23 +03:30
|
|
|
const { message, state } = ctx;
|
|
|
|
const { isAdmin, isMaster } = state;
|
2017-10-06 21:49:33 +03:30
|
|
|
const isCommand = /^!\w+/.test(message.text);
|
|
|
|
if (!isCommand) {
|
2017-10-04 20:55:50 +03:30
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2017-10-06 21:49:33 +03:30
|
|
|
const commandName = message.text.split(' ')[0].replace('!', '');
|
2017-10-04 20:55:50 +03:30
|
|
|
const command = await getCommand({ isActive: true, name: commandName });
|
|
|
|
|
|
|
|
if (!command) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
const { caption, content, role, type } = command;
|
|
|
|
const replyTo = message.reply_to_message
|
|
|
|
? { reply_to_message_id: message.reply_to_message.message_id }
|
|
|
|
: {};
|
|
|
|
const options = Object.assign(replyTo, caption ? { caption } : {});
|
|
|
|
if (
|
|
|
|
role === 'Master' &&
|
2017-10-06 16:34:23 +03:30
|
|
|
!isMaster ||
|
2017-10-04 20:55:50 +03:30
|
|
|
role === 'Admins' &&
|
2017-10-06 16:34:23 +03:30
|
|
|
!isAdmin
|
2017-10-04 20:55:50 +03:30
|
|
|
) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'text') {
|
|
|
|
ctx.replyWithHTML(content, options);
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
if (type === 'photo') {
|
|
|
|
ctx.replyWithPhoto(content, options);
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
if (type === 'video') {
|
|
|
|
ctx.replyWithVideo(content, replyTo);
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
if (type === 'document') {
|
|
|
|
ctx.replyWithDocument(content, replyTo);
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
if (type === 'audio') {
|
|
|
|
ctx.replyWithAudio(content, replyTo);
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
return next();
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = runCustomCmdHandler;
|