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

119 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-10-04 20:55:50 +03:30
'use strict';
// DB
const { addCommand, getCommand } = require('../../stores/command');
2017-10-04 20:55:50 +03:30
// Bot
const { Markup } = require('telegraf');
2017-10-04 20:55:50 +03:30
const Cmd = require('../../utils/cmd');
const { isMaster } = require('../../utils/config');
const { inlineKeyboard } = require('../../utils/tg');
const preserved = require('../commands').handlers;
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
}),
};
};
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-03-10 22:10:48 +01:00
/** @param { import('../../typings/context').ExtendedContext } ctx */
const addCommandHandler = async (ctx) => {
const { chat, message, reply } = ctx;
if (chat.type === 'channel') return null;
2019-01-24 19:53:10 +01:00
const { id } = ctx.from;
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-04 20:55:50 +03:30
}
const { flags, reason: commandName } = Cmd.parse(message);
if (flags.has('noop')) return null;
const isValidName = /^!?(\w+)$/.exec(commandName);
if (!isValidName) {
2020-05-13 22:38:59 +02:00
return ctx.replyWithHTML(
'<b>Send a valid command.</b>\n\nExample:\n' +
2023-03-08 14:08:57 +01:00
'<code>/addcommand rules</code>'
);
}
const newCommand = isValidName[1].toLowerCase();
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.'
);
}
const replaceCmd = flags.has('replace');
const content = message.reply_to_message;
const cmdExists = await getCommand({ isActive: true, name: newCommand });
if (!replaceCmd && cmdExists) {
return ctx.replyWithHTML(
' <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>&lt;name&gt;</code> - to add a command.\n' +
'/removecommand <code>&lt;name&gt;</code>' +
' - to remove a command.',
Markup.keyboard([[`/addcommand -replace ${newCommand}`]])
2020-06-29 13:49:41 +02:00
.selective()
.oneTime()
2023-03-08 14:08:57 +01:00
.resize()
);
}
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>'
);
}
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 }),
});
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-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;