2017-10-04 20:55:50 +03:30
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
// DB
|
2017-11-20 14:32:34 +03:30
|
|
|
|
const { addCommand, getCommand } = require('../../stores/command');
|
2017-10-04 20:55:50 +03:30
|
|
|
|
|
|
|
|
|
// Bot
|
2017-11-20 14:32:34 +03:30
|
|
|
|
const { Markup } = require('telegraf');
|
2017-10-04 20:55:50 +03:30
|
|
|
|
|
2020-02-19 08:11:32 +01:00
|
|
|
|
const Cmd = require('../../utils/cmd');
|
2018-07-26 14:27:09 +02:00
|
|
|
|
const { isMaster } = require('../../utils/config');
|
2020-02-19 08:11:32 +01:00
|
|
|
|
const { inlineKeyboard } = require('../../utils/tg');
|
2018-07-26 14:27:09 +02:00
|
|
|
|
|
2018-05-20 12:07:12 +02:00
|
|
|
|
const preserved = require('../commands').handlers;
|
2017-11-20 14:32:34 +03:30
|
|
|
|
|
2020-02-19 08:11:32 +01:00
|
|
|
|
const roleBtn = (btRole, { newCommand, currentRole }) => {
|
|
|
|
|
const noop = btRole.toLowerCase() === currentRole.toLowerCase();
|
|
|
|
|
return {
|
|
|
|
|
text: '✅ '.repeat(noop) + btRole,
|
|
|
|
|
callback_data: Cmd.stringify({
|
|
|
|
|
command: 'addcommand',
|
|
|
|
|
flags: {
|
|
|
|
|
noop,
|
|
|
|
|
role: btRole,
|
|
|
|
|
replace: 'soft',
|
|
|
|
|
},
|
|
|
|
|
reason: newCommand,
|
2020-04-24 15:24:24 +02:00
|
|
|
|
}),
|
2020-02-19 08:11:32 +01:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const roleKbRow = (cmdData) => [
|
|
|
|
|
roleBtn('Admins', cmdData),
|
|
|
|
|
roleBtn('Everyone', cmdData),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const normalizeRole = (role = '') => {
|
|
|
|
|
const lower = role.toLowerCase();
|
2023-03-08 14:08:57 +01:00
|
|
|
|
return lower === 'master' || lower === 'admins' ? lower : 'everyone';
|
2020-02-19 08:11:32 +01:00
|
|
|
|
};
|
|
|
|
|
|
2020-03-10 22:10:48 +01:00
|
|
|
|
/** @param { import('../../typings/context').ExtendedContext } ctx */
|
2018-05-20 12:07:12 +02:00
|
|
|
|
const addCommandHandler = async (ctx) => {
|
2018-02-02 15:47:09 +01:00
|
|
|
|
const { chat, message, reply } = ctx;
|
2020-02-19 08:11:32 +01:00
|
|
|
|
if (chat.type === 'channel') return null;
|
2019-01-24 19:53:10 +01:00
|
|
|
|
const { id } = ctx.from;
|
2017-10-06 16:40:49 +03:30
|
|
|
|
|
2018-02-02 15:47:09 +01:00
|
|
|
|
if (ctx.from.status !== 'admin') {
|
2020-05-13 22:38:59 +02:00
|
|
|
|
return ctx.replyWithHTML(
|
2023-03-08 14:08:57 +01:00
|
|
|
|
'ℹ️ <b>Sorry, only admins access this command.</b>'
|
2017-10-31 23:08:22 +01:00
|
|
|
|
);
|
2017-10-04 20:55:50 +03:30
|
|
|
|
}
|
2017-11-20 14:32:34 +03:30
|
|
|
|
|
2020-02-19 08:11:32 +01:00
|
|
|
|
const { flags, reason: commandName } = Cmd.parse(message);
|
|
|
|
|
if (flags.has('noop')) return null;
|
|
|
|
|
|
2018-05-20 12:07:12 +02:00
|
|
|
|
const isValidName = /^!?(\w+)$/.exec(commandName);
|
2017-11-20 14:32:34 +03:30
|
|
|
|
if (!isValidName) {
|
2020-05-13 22:38:59 +02:00
|
|
|
|
return ctx.replyWithHTML(
|
2017-11-20 14:32:34 +03:30
|
|
|
|
'<b>Send a valid command.</b>\n\nExample:\n' +
|
2023-03-08 14:08:57 +01:00
|
|
|
|
'<code>/addcommand rules</code>'
|
2017-11-20 14:32:34 +03:30
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
const newCommand = isValidName[1].toLowerCase();
|
2018-05-20 12:07:12 +02:00
|
|
|
|
if (preserved.has(newCommand)) {
|
2023-03-08 14:08:57 +01:00
|
|
|
|
return reply(
|
|
|
|
|
"❗️ Sorry you can't use this name, it's preserved.\n\n" +
|
|
|
|
|
'Try another one.'
|
|
|
|
|
);
|
2017-11-20 14:32:34 +03:30
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 08:11:32 +01:00
|
|
|
|
const replaceCmd = flags.has('replace');
|
|
|
|
|
const content = message.reply_to_message;
|
2018-02-02 15:47:09 +01:00
|
|
|
|
|
|
|
|
|
const cmdExists = await getCommand({ isActive: true, name: newCommand });
|
|
|
|
|
|
|
|
|
|
if (!replaceCmd && cmdExists) {
|
|
|
|
|
return ctx.replyWithHTML(
|
2017-11-20 14:32:34 +03:30
|
|
|
|
'ℹ️ <b>This command already exists.</b>\n\n' +
|
2023-03-08 14:08:57 +01:00
|
|
|
|
'/commands - to see the list of commands.\n' +
|
|
|
|
|
'/addcommand <code><name></code> - to add a command.\n' +
|
|
|
|
|
'/removecommand <code><name></code>' +
|
|
|
|
|
' - to remove a command.',
|
|
|
|
|
Markup.keyboard([[`/addcommand -replace ${newCommand}`]])
|
2020-06-29 13:49:41 +02:00
|
|
|
|
.selective()
|
2018-02-02 15:47:09 +01:00
|
|
|
|
.oneTime()
|
2023-03-08 14:08:57 +01:00
|
|
|
|
.resize()
|
2017-11-20 14:32:34 +03:30
|
|
|
|
);
|
|
|
|
|
}
|
2018-07-26 14:27:09 +02:00
|
|
|
|
if (cmdExists && cmdExists.role === 'master' && !isMaster(ctx.from)) {
|
2020-05-13 22:38:59 +02:00
|
|
|
|
return ctx.replyWithHTML(
|
2023-03-08 14:08:57 +01:00
|
|
|
|
'ℹ️ <b>Sorry, only master can replace this command.</b>'
|
2018-07-26 14:27:09 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
2020-02-19 08:11:32 +01:00
|
|
|
|
|
|
|
|
|
const softReplace = flags.get('replace') === 'soft';
|
|
|
|
|
if (content || softReplace) {
|
|
|
|
|
const role = normalizeRole(flags.get('role'));
|
|
|
|
|
await addCommand({
|
|
|
|
|
id,
|
|
|
|
|
role,
|
|
|
|
|
type: 'copy',
|
|
|
|
|
caption: null,
|
|
|
|
|
isActive: true,
|
|
|
|
|
name: newCommand,
|
2023-03-08 14:08:57 +01:00
|
|
|
|
...(softReplace || { content }),
|
2020-02-19 08:11:32 +01:00
|
|
|
|
});
|
|
|
|
|
return ctx.replyWithHTML(
|
2020-04-24 15:24:24 +02:00
|
|
|
|
`✅ <b>Successfully added <code>!${isValidName[1]}</code></b>.\n` +
|
2023-03-08 14:08:57 +01:00
|
|
|
|
'Who should be able to use it?',
|
|
|
|
|
inlineKeyboard(roleKbRow({ currentRole: role, newCommand }))
|
2020-02-19 08:11:32 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 15:24:24 +02:00
|
|
|
|
// eslint-disable-next-line max-len
|
2023-03-08 14:08:57 +01:00
|
|
|
|
return ctx.replyWithHTML("ℹ️ <b>Reply to a message you'd like to save</b>");
|
2017-10-04 20:55:50 +03:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = addCommandHandler;
|