mirror of
https://github.com/thedevs-network/the-guard-bot
synced 2025-08-27 12:27:11 +00:00
74 lines
2.0 KiB
JavaScript
74 lines
2.0 KiB
JavaScript
'use strict';
|
||
|
||
// DB
|
||
const { addCommand, getCommand } = require('../../stores/command');
|
||
|
||
// Bot
|
||
const { Markup } = require('telegraf');
|
||
const { replyOptions } = require('../../bot/options');
|
||
|
||
const { isMaster } = require('../../utils/config');
|
||
|
||
const preserved = require('../commands').handlers;
|
||
|
||
const addCommandHandler = async (ctx) => {
|
||
const { chat, message, reply } = ctx;
|
||
if (chat.type !== 'private') return null;
|
||
const { id } = ctx.from;
|
||
|
||
if (ctx.from.status !== 'admin') {
|
||
return reply(
|
||
'ℹ️ <b>Sorry, only admins access this command.</b>',
|
||
replyOptions
|
||
);
|
||
}
|
||
|
||
const [ slashCommand, commandName = '' ] = message.text.split(' ');
|
||
const isValidName = /^!?(\w+)$/.exec(commandName);
|
||
if (!isValidName) {
|
||
return reply(
|
||
'<b>Send a valid command.</b>\n\nExample:\n' +
|
||
'<code>/addcommand rules</code>',
|
||
replyOptions
|
||
);
|
||
}
|
||
const newCommand = isValidName[1].toLowerCase();
|
||
if (preserved.has(newCommand)) {
|
||
return reply('❗️ Sorry you can\'t use this name, it\'s preserved.\n\n' +
|
||
'Try another one.');
|
||
}
|
||
|
||
const replaceCmd = slashCommand.toLowerCase() === '/replacecommand';
|
||
|
||
const cmdExists = await getCommand({ isActive: true, name: newCommand });
|
||
|
||
if (!replaceCmd && cmdExists) {
|
||
return ctx.replyWithHTML(
|
||
'ℹ️ <b>This command already exists.</b>\n\n' +
|
||
'/commands - to see the list of commands.\n' +
|
||
'/addcommand <code><name></code> - to add a command.\n' +
|
||
'/removecommand <code><name></code>' +
|
||
' - to remove a command.',
|
||
Markup.keyboard([ [ `/replaceCommand ${newCommand}` ] ])
|
||
.oneTime()
|
||
.resize()
|
||
.extra()
|
||
);
|
||
}
|
||
if (cmdExists && cmdExists.role === 'master' && !isMaster(ctx.from)) {
|
||
return ctx.reply(
|
||
'ℹ️ <b>Sorry, only master can replace this command.</b>',
|
||
replyOptions
|
||
);
|
||
}
|
||
await addCommand({ id, name: newCommand, state: 'role' });
|
||
return reply('Who can use this command?', Markup.keyboard([
|
||
[ 'Master', 'Admins', 'Everyone' ]
|
||
])
|
||
.oneTime()
|
||
.resize()
|
||
.extra());
|
||
};
|
||
|
||
module.exports = addCommandHandler;
|