mirror of
https://github.com/thedevs-network/the-guard-bot
synced 2025-08-30 05:37:59 +00:00
removed custom command's adding step and make it inline
This commit is contained in:
parent
ff5eca49e7
commit
73833c9740
@ -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>',
|
||||
replyOptions
|
||||
);
|
||||
|
||||
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><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();
|
||||
};
|
||||
|
||||
module.exports = addCommandHandler;
|
||||
|
@ -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
|
||||
);
|
||||
}
|
||||
|
@ -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><name></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') {
|
||||
|
@ -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 }
|
||||
);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user