2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-28 20:57:52 +00:00
the-guard-bot/handlers/regex/runCustomCmd.js
2018-02-02 20:37:23 +01:00

51 lines
1.1 KiB
JavaScript

'use strict';
const { hears } = require('telegraf');
const R = require('ramda');
// 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)}`;
const runCustomCmdHandler = async (ctx, next) => {
const { message, state } = ctx;
const { isAdmin, isMaster } = state;
const commandName = ctx.match[1].toLowerCase();
const command = await getCommand({ isActive: true, name: commandName });
if (!command) {
return next();
}
const { caption, content, type } = command;
const role = command.role.toLowerCase();
if (
role === 'master' &&
!isMaster ||
role === 'admins' &&
!isAdmin
) {
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);
};
module.exports = hears(/^!(\w+)/, runCustomCmdHandler);