From 73833c9740f0aee704f77c0045c71494648d2fbe Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Mon, 20 Nov 2017 14:32:34 +0330 Subject: [PATCH] removed custom command's adding step and make it inline --- handlers/commands/addCommand.js | 53 +++++++++++++++++++++++++----- handlers/commands/removeCommand.js | 4 +-- handlers/messages/addCustomCmd.js | 36 -------------------- stores/command.js | 4 +-- 4 files changed, 49 insertions(+), 48 deletions(-) diff --git a/handlers/commands/addCommand.js b/handlers/commands/addCommand.js index 9d9a62d..cef0b59 100644 --- a/handlers/commands/addCommand.js +++ b/handlers/commands/addCommand.js @@ -1,13 +1,19 @@ 'use strict'; // DB -const { addCommand } = require('../../stores/command'); +const { addCommand, getCommand } = require('../../stores/command'); // Bot +const { Markup } = require('telegraf'); const { replyOptions } = require('../../bot/options'); -const addCommandHandler = async ({ chat, reply, state }) => { +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) { @@ -16,12 +22,43 @@ const addCommandHandler = async ({ chat, reply, state }) => { replyOptions ); } - await addCommand({ id: user.id }); - return reply( - 'Enter a name for the command without forward slash "/".' + - '\n\nFor example: rules', - 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; diff --git a/handlers/commands/removeCommand.js b/handlers/commands/removeCommand.js index 7f98f14..54adaf6 100644 --- a/handlers/commands/removeCommand.js +++ b/handlers/commands/removeCommand.js @@ -20,8 +20,8 @@ const removeCommandHandler = async ({ chat, message, reply, state }) => { const [ , commandName ] = text.split(' '); if (!commandName) { return reply( - 'Enter a command name to remove.\n\n' + - 'For example:\n/removecommand rules', + 'Send a valid command.\n\nExample:\n' + + '/removecommand rules', replyOptions ); } diff --git a/handlers/messages/addCustomCmd.js b/handlers/messages/addCustomCmd.js index 1dece4f..68cd58b 100644 --- a/handlers/messages/addCustomCmd.js +++ b/handlers/messages/addCustomCmd.js @@ -12,10 +12,6 @@ const { updateCommand } = require('../../stores/command'); -const preserved = [ 'admin', 'unadmin', 'leave', 'warn', 'unwarn', 'nowarns', - 'getwarns', 'ban', 'unban', 'report', 'staff', 'link', 'groups', 'commands', - 'addcommand', 'removecommand' ]; - const addCustomCmdHandler = async ({ chat, message, reply, state }, next) => { const { text, photo, document, video, audio } = message; const { isAdmin, user } = state; @@ -34,38 +30,6 @@ const addCustomCmdHandler = async ({ chat, message, reply, state }, next) => { return next(); } - if (command.state === 'add') { - if (!/^(?=\D)\w+$/.test(text)) { - reply('Please send a valid command.'); - return next(); - } - if (preserved.includes(text.toLowerCase())) { - reply('❗️Sorry you can\'t use this name, it\'s preserved.\n\n' + - 'Try another one.'); - return next(); - } - - if (await getCommand({ isActive: true, name: text.toLowerCase() })) { - 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 updateCommand({ id, name: text.toLowerCase(), state: 'role' }); - reply('Who can use this command?', Markup.keyboard([ - [ 'Master', 'Admins', 'Everyone' ] - ]) - .oneTime() - .resize() - .extra()); - return next(); - } - if (command.state === 'role') { const role = text.toLowerCase(); if (role !== 'master' && role !== 'admins' && role !== 'everyone') { diff --git a/stores/command.js b/stores/command.js index 7a5ac4e..13117b4 100644 --- a/stores/command.js +++ b/stores/command.js @@ -14,8 +14,8 @@ Command.ensureIndex({ const addCommand = command => Command.update( - { id: command.id, isActive: false }, - { id: command.id, isActive: false, state: 'add', }, + { id: command.id }, + Object.assign({}, command, { isActive: false }), { upsert: true } );