2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-23 18:38:05 +00:00
the-guard-bot/handlers/regex/runCustomCmd.js

51 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-10-04 20:55:50 +03:30
'use strict';
const { hears } = require('telegraf');
const R = require('ramda');
2017-10-04 20:55:50 +03:30
// DB
const { getCommand } = require('../../stores/command');
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) => {
const { message, state } = ctx;
const { isAdmin, isMaster } = state;
2017-10-04 20:55:50 +03:30
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();
}
const { caption, content, type } = command;
const role = command.role.toLowerCase();
2017-10-04 20:55:50 +03:30
if (
role === 'master' &&
!isMaster ||
role === 'admins' &&
!isAdmin
2017-10-04 20:55:50 +03:30
) {
return next();
}
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);