2017-10-04 20:55:50 +03:30
|
|
|
'use strict';
|
|
|
|
|
2023-03-08 14:08:57 +01:00
|
|
|
const {
|
|
|
|
Telegraf: { hears },
|
|
|
|
} = require('telegraf');
|
2018-02-02 15:47:09 +01:00
|
|
|
const R = require('ramda');
|
|
|
|
|
2017-10-04 20:55:50 +03:30
|
|
|
// DB
|
|
|
|
const { getCommand } = require('../../stores/command');
|
|
|
|
|
2019-04-20 18:05:01 +02:00
|
|
|
const { scheduleDeletion } = require('../../utils/tg');
|
|
|
|
|
2020-02-19 08:11:32 +01:00
|
|
|
const { isMaster } = require('../../utils/config');
|
|
|
|
|
2020-03-09 23:27:19 +01:00
|
|
|
const { config } = require('../../utils/config');
|
2019-04-20 18:05:01 +02:00
|
|
|
|
|
|
|
const deleteCustom = config.deleteCustom || { longerThan: Infinity };
|
|
|
|
|
2018-02-02 15:47:09 +01:00
|
|
|
const capitalize = R.replace(/^./, R.toUpper);
|
|
|
|
|
2023-03-08 14:08:57 +01:00
|
|
|
const getRepliedToId = R.path(['reply_to_message', 'message_id']);
|
2018-02-02 15:47:09 +01:00
|
|
|
|
2023-03-08 14:08:57 +01:00
|
|
|
const typeToMethod = (type) =>
|
|
|
|
type === 'text' ? 'replyWithHTML' : `replyWith${capitalize(type)}`;
|
2018-02-02 15:47:09 +01:00
|
|
|
|
2020-02-19 08:11:32 +01:00
|
|
|
const autoDelete = ({ content, type }) => {
|
|
|
|
switch (type) {
|
2023-03-08 14:08:57 +01:00
|
|
|
case 'text':
|
|
|
|
return content.length > deleteCustom.longerThan;
|
|
|
|
case 'copy':
|
|
|
|
return (content.text || '').length > deleteCustom.longerThan;
|
|
|
|
default:
|
|
|
|
return false;
|
2020-02-19 08:11:32 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const hasRole = (role, from) => {
|
|
|
|
switch (role.toLowerCase()) {
|
2023-03-08 14:08:57 +01:00
|
|
|
case 'master':
|
|
|
|
return isMaster(from);
|
|
|
|
case 'admins':
|
|
|
|
return from && from.status === 'admin';
|
|
|
|
default:
|
|
|
|
return true;
|
2020-02-19 08:11:32 +01:00
|
|
|
}
|
|
|
|
};
|
2017-10-04 20:55:50 +03:30
|
|
|
|
2020-03-10 22:10:48 +01:00
|
|
|
/** @param { import('../../typings/context').ExtendedContext } ctx */
|
2020-02-19 08:11:32 +01:00
|
|
|
const runCustomCmdHandler = async (ctx, next) => {
|
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 });
|
|
|
|
|
2020-02-19 08:11:32 +01:00
|
|
|
if (!command || !hasRole(command.role, ctx.from)) {
|
2017-10-04 20:55:50 +03:30
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2017-10-10 21:54:59 +03:30
|
|
|
const { caption, content, type } = command;
|
2017-10-04 20:55:50 +03:30
|
|
|
|
2018-02-02 15:47:09 +01:00
|
|
|
const options = {
|
2023-03-08 14:08:57 +01:00
|
|
|
...(caption && { caption }),
|
2018-02-02 15:47:09 +01:00
|
|
|
disable_web_page_preview: true,
|
2020-02-19 08:11:32 +01:00
|
|
|
reply_to_message_id: getRepliedToId(ctx.message),
|
2018-02-02 15:47:09 +01:00
|
|
|
};
|
|
|
|
|
2023-03-08 14:08:57 +01:00
|
|
|
return ctx[typeToMethod(type)](content, options).then(({ message_id }) =>
|
|
|
|
scheduleDeletion(autoDelete(command) && deleteCustom.after)({
|
|
|
|
chat: ctx.chat,
|
|
|
|
message_id,
|
|
|
|
})
|
|
|
|
);
|
2017-10-04 20:55:50 +03:30
|
|
|
};
|
|
|
|
|
2019-04-20 11:40:03 +02:00
|
|
|
module.exports = hears(/^! ?(\w+)/, runCustomCmdHandler);
|