'use strict'; // DB const { addCommand, getCommand } = require('../../stores/command'); // Bot const { Markup } = require('telegraf'); const { replyOptions } = require('../../bot/options'); 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) => { const { isAdmin, user } = state; const { id } = user; if (chat.type !== 'private') return null; if (!isAdmin) { return reply( 'ℹ️ Sorry, only admins access this command.', replyOptions ); } const [ , commandName ] = message.text.split(' '); const isValidName = commandName && commandName.match(/^(?:[!])?(\w+)$/); if (!isValidName) { reply( 'Send a valid command.\n\nExample:\n' + '/addcommand rules', 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( 'ℹ️ This command already exists.\n\n' + '/commands - to see the list of commands.\n' + '/addcommand - to add a command.\n' + '/removecommand <name>' + ' - 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(); }; module.exports = addCommandHandler;