2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-24 10:58:19 +00:00

66 lines
1.8 KiB
JavaScript
Raw Normal View History

2017-10-04 20:55:50 +03:30
'use strict';
// DB
const { addCommand, getCommand } = require('../../stores/command');
2017-10-04 20:55:50 +03:30
// Bot
const { Markup } = require('telegraf');
2017-10-04 20:55:50 +03:30
const { replyOptions } = require('../../bot/options');
const preserved = require('../commands').handlers;
const addCommandHandler = async (ctx) => {
const { chat, message, reply } = ctx;
const { id } = ctx.from;
if (chat.type !== 'private') return null;
if (ctx.from.status !== 'admin') {
return reply(
' <b>Sorry, only admins access this command.</b>',
replyOptions
);
2017-10-04 20:55:50 +03:30
}
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' +
2017-11-25 14:59:41 +03:30
'/addcommand <code>&lt;name&gt;</code> - to add a command.\n' +
'/removecommand <code>&lt;name&gt;</code>' +
' - to remove a command.',
Markup.keyboard([ [ `/replaceCommand ${newCommand}` ] ])
.oneTime()
.resize()
.extra()
);
}
await addCommand({ id, name: newCommand, state: 'role' });
return reply('Who can use this command?', Markup.keyboard([
[ 'Master', 'Admins', 'Everyone' ]
])
.oneTime()
.resize()
.extra());
2017-10-04 20:55:50 +03:30
};
module.exports = addCommandHandler;