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
|
|
|
|
const { replyOptions } = require('../../bot/options');
|
|
|
|
|
|
2018-05-20 12:07:12 +02:00
|
|
|
|
const preserved = require('../commands').handlers;
|
2017-11-20 14:32:34 +03:30
|
|
|
|
|
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;
|
|
|
|
|
const { id } = ctx.from;
|
2017-10-06 16:40:49 +03:30
|
|
|
|
if (chat.type !== 'private') return null;
|
|
|
|
|
|
2018-02-02 15:47:09 +01:00
|
|
|
|
if (ctx.from.status !== 'admin') {
|
2017-10-31 23:08:22 +01:00
|
|
|
|
return reply(
|
|
|
|
|
'ℹ️ <b>Sorry, only admins access this command.</b>',
|
|
|
|
|
replyOptions
|
|
|
|
|
);
|
2017-10-04 20:55:50 +03:30
|
|
|
|
}
|
2017-11-20 14:32:34 +03:30
|
|
|
|
|
2018-05-20 18:20:21 +02:00
|
|
|
|
const [ slashCommand, commandName = '' ] = message.text.split(' ');
|
2018-05-20 12:07:12 +02:00
|
|
|
|
const isValidName = /^!?(\w+)$/.exec(commandName);
|
2017-11-20 14:32:34 +03:30
|
|
|
|
if (!isValidName) {
|
2018-02-02 15:47:09 +01:00
|
|
|
|
return reply(
|
2017-11-20 14:32:34 +03:30
|
|
|
|
'<b>Send a valid command.</b>\n\nExample:\n' +
|
|
|
|
|
'<code>/addcommand rules</code>',
|
|
|
|
|
replyOptions
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
const newCommand = isValidName[1].toLowerCase();
|
2018-05-20 12:07:12 +02:00
|
|
|
|
if (preserved.has(newCommand)) {
|
|
|
|
|
return reply('❗️ Sorry you can\'t use this name, it\'s preserved.\n\n' +
|
2017-11-20 14:32:34 +03:30
|
|
|
|
'Try another one.');
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 15:47:09 +01:00
|
|
|
|
const replaceCmd = slashCommand.toLowerCase() === '/replacecommand';
|
|
|
|
|
|
|
|
|
|
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' +
|
|
|
|
|
'/commands - to see the list of commands.\n' +
|
2017-11-25 14:59:41 +03:30
|
|
|
|
'/addcommand <code><name></code> - to add a command.\n' +
|
2017-11-20 14:32:34 +03:30
|
|
|
|
'/removecommand <code><name></code>' +
|
|
|
|
|
' - to remove a command.',
|
2018-02-02 15:47:09 +01:00
|
|
|
|
Markup.keyboard([ [ `/replaceCommand ${newCommand}` ] ])
|
|
|
|
|
.oneTime()
|
|
|
|
|
.resize()
|
|
|
|
|
.extra()
|
2017-11-20 14:32:34 +03:30
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
await addCommand({ id, name: newCommand, state: 'role' });
|
2018-02-02 15:47:09 +01:00
|
|
|
|
return reply('Who can use this command?', Markup.keyboard([
|
2017-11-20 14:32:34 +03:30
|
|
|
|
[ 'Master', 'Admins', 'Everyone' ]
|
|
|
|
|
])
|
|
|
|
|
.oneTime()
|
|
|
|
|
.resize()
|
|
|
|
|
.extra());
|
2017-10-04 20:55:50 +03:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = addCommandHandler;
|