|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { Markup } = require('telegraf'); |
| 4 | + |
| 5 | +// Bot |
| 6 | +const { replyOptions } = require('../../bot/options'); |
| 7 | + |
| 8 | +// DB |
| 9 | +const { isAdmin } = require('../../stores/user'); |
| 10 | +const { |
| 11 | + getCommand, |
| 12 | + removeCommand, |
| 13 | + updateCommand |
| 14 | +} = require('../../stores/command'); |
| 15 | + |
| 16 | +const addCustomCmdHandler = async ({ chat, message, reply }, next) => { |
| 17 | + const { text, photo, document, video, audio } = message; |
| 18 | + const { id } = message.from; |
| 19 | + |
| 20 | + if (text && /^\/\w+/.test(text)) { |
| 21 | + await removeCommand({ id, isActive: false }); |
| 22 | + return next(); |
| 23 | + } |
| 24 | + |
| 25 | + const command = await getCommand({ id, isActive: false }); |
| 26 | + if (chat.type !== 'private' || |
| 27 | + !await isAdmin(message.from) || |
| 28 | + !command || |
| 29 | + !command.state) { |
| 30 | + return next(); |
| 31 | + } |
| 32 | + |
| 33 | + const { state } = command; |
| 34 | + if (state === 'add') { |
| 35 | + if (!/^(?=\D)\w+$/.test(text)) { |
| 36 | + reply('Please send a valid command.'); |
| 37 | + return next(); |
| 38 | + } |
| 39 | + if (await getCommand({ isActive: true, name: text })) { |
| 40 | + reply( |
| 41 | + 'ℹ️ <b>This command already exists.</b>\n\n' + |
| 42 | + '/commands - to see the list of commands.\n' + |
| 43 | + '/addcommand - to add a command.\n' + |
| 44 | + '/removecommand <code><name></code>' + |
| 45 | + ' - to remove a command.', |
| 46 | + replyOptions); |
| 47 | + return next(); |
| 48 | + } |
| 49 | + await updateCommand({ id, name: text, state: 'role' }); |
| 50 | + reply('Who can use this command?', Markup.keyboard([ |
| 51 | + [ 'Master', 'Admins', 'Everyone' ] |
| 52 | + ]) |
| 53 | + .oneTime() |
| 54 | + .resize() |
| 55 | + .extra()); |
| 56 | + return next(); |
| 57 | + } |
| 58 | + |
| 59 | + if (state === 'role') { |
| 60 | + if (text !== 'Master' && text !== 'Admins' && text !== 'Everyone') { |
| 61 | + reply('Please send a valid role.', Markup.keyboard([ |
| 62 | + [ 'Master', 'Admins', 'Everyone' ] |
| 63 | + ]) |
| 64 | + .oneTime() |
| 65 | + .resize() |
| 66 | + .extra()); |
| 67 | + return next(); |
| 68 | + } |
| 69 | + await updateCommand({ id, role: text, state: 'content' }); |
| 70 | + reply( |
| 71 | + 'Send the content you wish to be shown when the command is used.' + |
| 72 | + '.\n\nSupported contents:\n- <b>Text (HTML)</b>\n- <b>Photo</b>' + |
| 73 | + '\n- <b>Video</b>\n- <b>Document</b>\n- <b>Audio</b>', |
| 74 | + replyOptions); |
| 75 | + return next(); |
| 76 | + } |
| 77 | + |
| 78 | + if (state === 'content') { |
| 79 | + let newCommand; |
| 80 | + if (text) { |
| 81 | + newCommand = { content: text, type: 'text' }; |
| 82 | + } |
| 83 | + if (photo) { |
| 84 | + newCommand = { |
| 85 | + content: photo[photo.length - 1].file_id, |
| 86 | + type: 'photo' |
| 87 | + }; |
| 88 | + } |
| 89 | + if (document) { |
| 90 | + newCommand = { content: document.file_id, type: 'document' }; |
| 91 | + } |
| 92 | + if (video) { |
| 93 | + newCommand = { content: video.file_id, type: 'video' }; |
| 94 | + } |
| 95 | + if (audio) { |
| 96 | + newCommand = { content: audio.file_id, type: 'audio' }; |
| 97 | + } |
| 98 | + if (message.caption) { |
| 99 | + newCommand.caption = message.caption; |
| 100 | + } |
| 101 | + await Promise.all([ |
| 102 | + updateCommand(Object.assign( |
| 103 | + {}, |
| 104 | + newCommand, |
| 105 | + { id, isActive: true, state: null })), |
| 106 | + ]); |
| 107 | + reply( |
| 108 | + '✅ <b>New command has been created successfully.</b>\n\n' + |
| 109 | + 'This command can be used in groups now. ' + |
| 110 | + 'Custom commands can reply other messages too.\n\n' + |
| 111 | + '/commands - to see the list of commands.\n' + |
| 112 | + '/addcommand - to add a new command.\n' + |
| 113 | + '/removecomand <code><name></code> - to remove a command.', |
| 114 | + replyOptions); |
| 115 | + return next(); |
| 116 | + } |
| 117 | + return next(); |
| 118 | +}; |
| 119 | + |
| 120 | +module.exports = addCustomCmdHandler; |
0 commit comments