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');
|
|
|
|
|
|
2017-11-20 14:32:34 +03:30
|
|
|
|
const preserved = [ 'admin', 'unadmin', 'leave', 'warn', 'unwarn', 'nowarns',
|
|
|
|
|
'getwarns', 'ban', 'unban', 'report', 'staff', 'link', 'groups', 'commands',
|
|
|
|
|
'addcommand', 'removecommand' ];
|
|
|
|
|
|
|
|
|
|
const addCommandHandler = async ({ chat, message, reply, state }, next) => {
|
2017-10-06 16:34:23 +03:30
|
|
|
|
const { isAdmin, user } = state;
|
2017-11-20 14:32:34 +03:30
|
|
|
|
const { id } = user;
|
2017-10-06 16:40:49 +03:30
|
|
|
|
if (chat.type !== 'private') return null;
|
|
|
|
|
|
2017-10-06 16:34:23 +03:30
|
|
|
|
if (!isAdmin) {
|
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
|
|
|
|
|
|
|
|
|
const [ , commandName ] = message.text.split(' ');
|
|
|
|
|
const isValidName = commandName && commandName.match(/^(?:[!])?(\w+)$/);
|
|
|
|
|
if (!isValidName) {
|
|
|
|
|
reply(
|
|
|
|
|
'<b>Send a valid command.</b>\n\nExample:\n' +
|
|
|
|
|
'<code>/addcommand rules</code>',
|
|
|
|
|
replyOptions
|
|
|
|
|
);
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
const newCommand = isValidName[1].toLowerCase();
|
|
|
|
|
if (preserved.includes(newCommand)) {
|
|
|
|
|
reply('❗️ Sorry you can\'t use this name, it\'s preserved.\n\n' +
|
|
|
|
|
'Try another one.');
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (await getCommand({ isActive: true, name: newCommand })) {
|
|
|
|
|
reply(
|
|
|
|
|
'ℹ️ <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.',
|
|
|
|
|
replyOptions
|
|
|
|
|
);
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
await addCommand({ id, name: newCommand, state: 'role' });
|
|
|
|
|
reply('Who can use this command?', Markup.keyboard([
|
|
|
|
|
[ 'Master', 'Admins', 'Everyone' ]
|
|
|
|
|
])
|
|
|
|
|
.oneTime()
|
|
|
|
|
.resize()
|
|
|
|
|
.extra());
|
|
|
|
|
return next();
|
2017-10-04 20:55:50 +03:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = addCommandHandler;
|