mirror of
https://github.com/thedevs-network/the-guard-bot
synced 2025-08-31 14:15:25 +00:00
@@ -11,27 +11,26 @@ 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 { id } = user;
|
||||
const addCommandHandler = async (ctx, next) => {
|
||||
const { chat, message, reply } = ctx;
|
||||
const { id } = ctx.from;
|
||||
if (chat.type !== 'private') return null;
|
||||
|
||||
if (!isAdmin) {
|
||||
if (ctx.from.status !== 'admin') {
|
||||
return reply(
|
||||
'ℹ️ <b>Sorry, only admins access this command.</b>',
|
||||
replyOptions
|
||||
);
|
||||
}
|
||||
|
||||
const [ , commandName ] = message.text.split(' ');
|
||||
const [ slashCommand, commandName ] = message.text.split(' ');
|
||||
const isValidName = commandName && commandName.match(/^(?:[!])?(\w+)$/);
|
||||
if (!isValidName) {
|
||||
reply(
|
||||
return reply(
|
||||
'<b>Send a valid command.</b>\n\nExample:\n' +
|
||||
'<code>/addcommand rules</code>',
|
||||
replyOptions
|
||||
);
|
||||
return next();
|
||||
}
|
||||
const newCommand = isValidName[1].toLowerCase();
|
||||
if (preserved.includes(newCommand)) {
|
||||
@@ -40,25 +39,30 @@ const addCommandHandler = async ({ chat, message, reply, state }, next) => {
|
||||
return next();
|
||||
}
|
||||
|
||||
if (await getCommand({ isActive: true, name: newCommand })) {
|
||||
reply(
|
||||
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.',
|
||||
replyOptions
|
||||
Markup.keyboard([ [ `/replaceCommand ${newCommand}` ] ])
|
||||
.oneTime()
|
||||
.resize()
|
||||
.extra()
|
||||
);
|
||||
return next();
|
||||
}
|
||||
await addCommand({ id, name: newCommand, state: 'role' });
|
||||
reply('Who can use this command?', Markup.keyboard([
|
||||
return reply('Who can use this command?', Markup.keyboard([
|
||||
[ 'Master', 'Admins', 'Everyone' ]
|
||||
])
|
||||
.oneTime()
|
||||
.resize()
|
||||
.extra());
|
||||
return next();
|
||||
};
|
||||
|
||||
module.exports = addCommandHandler;
|
||||
|
@@ -53,7 +53,11 @@ composer.command('staff', deleteMessage, staffHandler);
|
||||
composer.command('link', deleteMessage, linkHandler);
|
||||
composer.command('groups', deleteMessage, groupsHandler);
|
||||
composer.command('commands', deleteMessage, commandReferenceHandler);
|
||||
composer.command('addcommand', deleteMessage, addCommandHandler);
|
||||
composer.command(
|
||||
[ 'addcommand', 'replaceCommand' ],
|
||||
deleteMessage,
|
||||
addCommandHandler
|
||||
);
|
||||
composer.command('removecommand', deleteMessage, removeCommandHandler);
|
||||
composer.command([ 'start', 'help' ], deleteMessage, helpHandler);
|
||||
|
||||
|
@@ -12,10 +12,10 @@ const {
|
||||
updateCommand
|
||||
} = require('../../stores/command');
|
||||
|
||||
const addCustomCmdHandler = async ({ chat, message, reply, state }, next) => {
|
||||
const addCustomCmdHandler = async ({ chat, message, reply, from }, next) => {
|
||||
const { text, photo, document, video, audio } = message;
|
||||
const { isAdmin, user } = state;
|
||||
const { id } = user;
|
||||
const { id } = from;
|
||||
const isAdmin = from.status === 'admin';
|
||||
|
||||
if (text && /^\/\w+/.test(text)) {
|
||||
await removeCommand({ id, isActive: false });
|
||||
@@ -42,13 +42,12 @@ const addCustomCmdHandler = async ({ chat, message, reply, state }, next) => {
|
||||
return next();
|
||||
}
|
||||
await updateCommand({ id, role, state: 'content' });
|
||||
reply(
|
||||
return 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
|
||||
);
|
||||
return next();
|
||||
}
|
||||
|
||||
if (command.state === 'content') {
|
||||
@@ -74,14 +73,8 @@ const addCustomCmdHandler = async ({ chat, message, reply, state }, next) => {
|
||||
if (message.caption) {
|
||||
newCommand.caption = message.caption;
|
||||
}
|
||||
await Promise.all([
|
||||
updateCommand(Object.assign(
|
||||
{},
|
||||
newCommand,
|
||||
{ id, isActive: true, state: null }
|
||||
)),
|
||||
]);
|
||||
reply(
|
||||
await updateCommand({ ...newCommand, id, isActive: true, state: null });
|
||||
return reply(
|
||||
'✅ <b>New command has been created successfully.</b>\n\n' +
|
||||
'Custom commands work with ! instead of /.\n\n' +
|
||||
'For example: <code>!rules</code>\n\n' +
|
||||
@@ -91,7 +84,6 @@ const addCustomCmdHandler = async ({ chat, message, reply, state }, next) => {
|
||||
'/removecomand <code><name></code> - to remove a command.',
|
||||
replyOptions
|
||||
);
|
||||
return next();
|
||||
}
|
||||
return next();
|
||||
};
|
||||
|
@@ -1,20 +1,25 @@
|
||||
'use strict';
|
||||
|
||||
const { hears } = require('telegraf');
|
||||
const R = require('ramda');
|
||||
|
||||
// DB
|
||||
const { getCommand } = require('../../stores/command');
|
||||
|
||||
const capitalize = R.replace(/^./, R.toUpper);
|
||||
|
||||
const getRepliedToId = R.path([ 'reply_to_message', 'message_id' ]);
|
||||
|
||||
const typeToMethod = type =>
|
||||
type === 'text'
|
||||
? 'replyWithHTML'
|
||||
: `replyWith${capitalize(type)}`;
|
||||
|
||||
const runCustomCmdHandler = async (ctx, next) => {
|
||||
const { message, state } = ctx;
|
||||
const { isAdmin, isMaster } = state;
|
||||
const isCommand = /^!\w+/.test(message.text);
|
||||
if (!isCommand) {
|
||||
return next();
|
||||
}
|
||||
|
||||
const commandName = message.text
|
||||
.split(' ')[0]
|
||||
.replace('!', '')
|
||||
.toLowerCase();
|
||||
const commandName = ctx.match[1].toLowerCase();
|
||||
const command = await getCommand({ isActive: true, name: commandName });
|
||||
|
||||
if (!command) {
|
||||
@@ -23,14 +28,6 @@ const runCustomCmdHandler = async (ctx, next) => {
|
||||
|
||||
const { caption, content, type } = command;
|
||||
const role = command.role.toLowerCase();
|
||||
const replyTo = message.reply_to_message
|
||||
? { reply_to_message_id: message.reply_to_message.message_id }
|
||||
: {};
|
||||
const options = Object.assign(
|
||||
replyTo,
|
||||
caption ? { caption } : {},
|
||||
{ disable_web_page_preview: true },
|
||||
);
|
||||
if (
|
||||
role === 'master' &&
|
||||
!isMaster ||
|
||||
@@ -40,27 +37,14 @@ const runCustomCmdHandler = async (ctx, next) => {
|
||||
return next();
|
||||
}
|
||||
|
||||
if (type === 'text') {
|
||||
ctx.replyWithHTML(content, options);
|
||||
return next();
|
||||
}
|
||||
if (type === 'photo') {
|
||||
ctx.replyWithPhoto(content, options);
|
||||
return next();
|
||||
}
|
||||
if (type === 'video') {
|
||||
ctx.replyWithVideo(content, replyTo);
|
||||
return next();
|
||||
}
|
||||
if (type === 'document') {
|
||||
ctx.replyWithDocument(content, replyTo);
|
||||
return next();
|
||||
}
|
||||
if (type === 'audio') {
|
||||
ctx.replyWithAudio(content, replyTo);
|
||||
return next();
|
||||
}
|
||||
return next();
|
||||
const reply_to_message_id = getRepliedToId(message);
|
||||
const options = {
|
||||
caption,
|
||||
disable_web_page_preview: true,
|
||||
reply_to_message_id,
|
||||
};
|
||||
|
||||
return ctx[typeToMethod(type)](content, options);
|
||||
};
|
||||
|
||||
module.exports = runCustomCmdHandler;
|
||||
module.exports = hears(/^!(\w+)/, runCustomCmdHandler);
|
||||
|
@@ -14,7 +14,7 @@ Command.ensureIndex({
|
||||
|
||||
const addCommand = command =>
|
||||
Command.update(
|
||||
{ id: command.id, isActive: false },
|
||||
{ name: command.name },
|
||||
Object.assign({}, command, { isActive: false }),
|
||||
{ upsert: true }
|
||||
);
|
||||
|
Reference in New Issue
Block a user