mirror of
https://github.com/thedevs-network/the-guard-bot
synced 2025-08-31 06:05:22 +00:00
removed custom command's adding step and make it inline
This commit is contained in:
@@ -1,13 +1,19 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// DB
|
// DB
|
||||||
const { addCommand } = require('../../stores/command');
|
const { addCommand, getCommand } = require('../../stores/command');
|
||||||
|
|
||||||
// Bot
|
// Bot
|
||||||
|
const { Markup } = require('telegraf');
|
||||||
const { replyOptions } = require('../../bot/options');
|
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 { isAdmin, user } = state;
|
||||||
|
const { id } = user;
|
||||||
if (chat.type !== 'private') return null;
|
if (chat.type !== 'private') return null;
|
||||||
|
|
||||||
if (!isAdmin) {
|
if (!isAdmin) {
|
||||||
@@ -16,12 +22,43 @@ const addCommandHandler = async ({ chat, reply, state }) => {
|
|||||||
replyOptions
|
replyOptions
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
await addCommand({ id: user.id });
|
|
||||||
return reply(
|
const [ , commandName ] = message.text.split(' ');
|
||||||
'Enter a name for the command without forward slash "/".' +
|
const isValidName = commandName && commandName.match(/^(?:[!])?(\w+)$/);
|
||||||
'\n\nFor example: <b>rules</b>',
|
if (!isValidName) {
|
||||||
|
reply(
|
||||||
|
'<b>Send a valid command.</b>\n\nExample:\n' +
|
||||||
|
'<code>/addcommand rules</code>',
|
||||||
replyOptions
|
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;
|
module.exports = addCommandHandler;
|
||||||
|
@@ -20,8 +20,8 @@ const removeCommandHandler = async ({ chat, message, reply, state }) => {
|
|||||||
const [ , commandName ] = text.split(' ');
|
const [ , commandName ] = text.split(' ');
|
||||||
if (!commandName) {
|
if (!commandName) {
|
||||||
return reply(
|
return reply(
|
||||||
'Enter a command name to remove.\n\n' +
|
'<b>Send a valid command.</b>\n\nExample:\n' +
|
||||||
'For example:\n/removecommand <b>rules</b>',
|
'<code>/removecommand rules</code>',
|
||||||
replyOptions
|
replyOptions
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@@ -12,10 +12,6 @@ const {
|
|||||||
updateCommand
|
updateCommand
|
||||||
} = require('../../stores/command');
|
} = 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 addCustomCmdHandler = async ({ chat, message, reply, state }, next) => {
|
||||||
const { text, photo, document, video, audio } = message;
|
const { text, photo, document, video, audio } = message;
|
||||||
const { isAdmin, user } = state;
|
const { isAdmin, user } = state;
|
||||||
@@ -34,38 +30,6 @@ const addCustomCmdHandler = async ({ chat, message, reply, state }, next) => {
|
|||||||
return 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') {
|
if (command.state === 'role') {
|
||||||
const role = text.toLowerCase();
|
const role = text.toLowerCase();
|
||||||
if (role !== 'master' && role !== 'admins' && role !== 'everyone') {
|
if (role !== 'master' && role !== 'admins' && role !== 'everyone') {
|
||||||
|
@@ -14,8 +14,8 @@ Command.ensureIndex({
|
|||||||
|
|
||||||
const addCommand = command =>
|
const addCommand = command =>
|
||||||
Command.update(
|
Command.update(
|
||||||
{ id: command.id, isActive: false },
|
{ id: command.id },
|
||||||
{ id: command.id, isActive: false, state: 'add', },
|
Object.assign({}, command, { isActive: false }),
|
||||||
{ upsert: true }
|
{ upsert: true }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user