2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-30 21:55:17 +00:00

removed custom command's adding step and make it inline

This commit is contained in:
Pouria Ezzati
2017-11-20 14:32:34 +03:30
parent ff5eca49e7
commit 73833c9740
4 changed files with 49 additions and 48 deletions

View File

@@ -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: <b>rules</b>',
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' +
'/addcommand - to add a command.\n' +
'/removecommand <code>&lt;name&gt;</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();
};
module.exports = addCommandHandler;

View File

@@ -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 <b>rules</b>',
'<b>Send a valid command.</b>\n\nExample:\n' +
'<code>/removecommand rules</code>',
replyOptions
);
}

View File

@@ -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(
' <b>This command already exists.</b>\n\n' +
'/commands - to see the list of commands.\n' +
'/addcommand - to add a command.\n' +
'/removecommand <code>&lt;name&gt;</code>' +
' - 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') {

View File

@@ -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 }
);