2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-09-08 10:05:24 +00:00
Files
the-guard-bot/handlers/messages/addCustomCmd.js

100 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-10-04 20:55:50 +03:30
'use strict';
const { Markup } = require('telegraf');
// Bot
const { replyOptions } = require('../../bot/options');
// DB
const {
getCommand,
removeCommand,
updateCommand
} = require('../../stores/command');
const addCustomCmdHandler = async ({ chat, message, reply, state }, next) => {
2017-10-04 20:55:50 +03:30
const { text, photo, document, video, audio } = message;
const { isAdmin, user } = state;
const { id } = user;
2017-10-04 20:55:50 +03:30
if (text && /^\/\w+/.test(text)) {
await removeCommand({ id, isActive: false });
return next();
}
const command = await getCommand({ id, isActive: false });
if (chat.type !== 'private' ||
!isAdmin ||
2017-10-04 20:55:50 +03:30
!command ||
!command.state) {
return next();
}
if (command.state === 'role') {
const role = text.toLowerCase();
if (role !== 'master' && role !== 'admins' && role !== 'everyone') {
2017-10-04 20:55:50 +03:30
reply('Please send a valid role.', Markup.keyboard([
[ 'Master', 'Admins', 'Everyone' ]
])
.oneTime()
.resize()
.extra());
return next();
}
await updateCommand({ id, role, state: 'content' });
2017-10-04 20:55:50 +03:30
reply(
'Send the content you wish to be shown when the command is used.' +
'.\n\nSupported contents:\n- <b>Text (HTML)</b>\n- <b>Photo</b>' +
'\n- <b>Video</b>\n- <b>Document</b>\n- <b>Audio</b>',
replyOptions
);
2017-10-04 20:55:50 +03:30
return next();
}
if (command.state === 'content') {
2017-10-04 20:55:50 +03:30
let newCommand;
if (text) {
newCommand = { content: text, type: 'text' };
}
if (photo) {
newCommand = {
content: photo[photo.length - 1].file_id,
type: 'photo'
};
}
if (document) {
newCommand = { content: document.file_id, type: 'document' };
}
if (video) {
newCommand = { content: video.file_id, type: 'video' };
}
if (audio) {
newCommand = { content: audio.file_id, type: 'audio' };
}
if (message.caption) {
newCommand.caption = message.caption;
}
await Promise.all([
updateCommand(Object.assign(
{},
newCommand,
{ id, isActive: true, state: null }
)),
2017-10-04 20:55:50 +03:30
]);
reply(
'✅ <b>New command has been created successfully.</b>\n\n' +
2017-10-08 14:17:03 +03:30
'Custom commands work with ! instead of /.\n\n' +
'For example: <code>!rules</code>\n\n' +
2017-10-04 20:55:50 +03:30
'Custom commands can reply other messages too.\n\n' +
'/commands - to see the list of commands.\n' +
'/addcommand - to add a new command.\n' +
'/removecomand <code>&lt;name&gt;</code> - to remove a command.',
replyOptions
);
2017-10-04 20:55:50 +03:30
return next();
}
return next();
};
module.exports = addCustomCmdHandler;