From 699df6e1c562312d5ac05bb18b94e94a80b2bd41 Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Tue, 10 Oct 2017 21:42:39 +0330 Subject: [PATCH 01/11] small fix for removecommand message --- handlers/commands/removeCommand.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handlers/commands/removeCommand.js b/handlers/commands/removeCommand.js index 635a07c..816f96c 100644 --- a/handlers/commands/removeCommand.js +++ b/handlers/commands/removeCommand.js @@ -36,7 +36,7 @@ const removeCommandHandler = async ({ chat, message, reply, state }) => { await removeCommand({ name: commandName }); return reply( - `✅ /${commandName} ` + + `✅ !${commandName} ` + 'has been removed successfully.', replyOptions); }; From 183b901f2675156e8247d6944e6b0b90037507c4 Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Tue, 10 Oct 2017 21:54:59 +0330 Subject: [PATCH 02/11] custom commands role is not case sensitive anymore --- handlers/commands/removeCommand.js | 3 ++- handlers/messages/addCustomCmd.js | 5 +++-- handlers/messages/runCustomCmd.js | 7 ++++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/handlers/commands/removeCommand.js b/handlers/commands/removeCommand.js index 816f96c..0eda575 100644 --- a/handlers/commands/removeCommand.js +++ b/handlers/commands/removeCommand.js @@ -29,7 +29,8 @@ const removeCommandHandler = async ({ chat, message, reply, state }) => { replyOptions); } - if (command.role === 'Master' && !isMaster) { + const role = command.role.toLowerCase(); + if (role === 'master' && !isMaster) { return reply('ℹ️ Sorry, only master can remove this command.', replyOptions); } diff --git a/handlers/messages/addCustomCmd.js b/handlers/messages/addCustomCmd.js index 6bd767f..cea5b5c 100644 --- a/handlers/messages/addCustomCmd.js +++ b/handlers/messages/addCustomCmd.js @@ -66,7 +66,8 @@ const addCustomCmdHandler = async ({ chat, message, reply, state }, next) => { } if (command.state === 'role') { - if (text !== 'Master' && text !== 'Admins' && text !== 'Everyone') { + const role = text.toLowerCase(); + if (role !== 'master' && role !== 'admins' && role !== 'everyone') { reply('Please send a valid role.', Markup.keyboard([ [ 'Master', 'Admins', 'Everyone' ] ]) @@ -75,7 +76,7 @@ const addCustomCmdHandler = async ({ chat, message, reply, state }, next) => { .extra()); return next(); } - await updateCommand({ id, role: text, state: 'content' }); + await updateCommand({ id, role, state: 'content' }); reply( 'Send the content you wish to be shown when the command is used.' + '.\n\nSupported contents:\n- Text (HTML)\n- Photo' + diff --git a/handlers/messages/runCustomCmd.js b/handlers/messages/runCustomCmd.js index 13e58f0..03da4fd 100644 --- a/handlers/messages/runCustomCmd.js +++ b/handlers/messages/runCustomCmd.js @@ -18,15 +18,16 @@ const runCustomCmdHandler = async (ctx, next) => { return next(); } - const { caption, content, role, type } = command; + 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 } : {}); if ( - role === 'Master' && + role === 'master' && !isMaster || - role === 'Admins' && + role === 'admins' && !isAdmin ) { return next(); From 9ed3a54319294596726bd42839bc8770e31e28ce Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Tue, 10 Oct 2017 22:13:59 +0330 Subject: [PATCH 03/11] masterID can be a username now --- handlers/messages/addUser.js | 7 +++++-- handlers/middlewares/addedToGroup.js | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/handlers/messages/addUser.js b/handlers/messages/addUser.js index 1eadd76..ef8e063 100644 --- a/handlers/messages/addUser.js +++ b/handlers/messages/addUser.js @@ -1,7 +1,7 @@ 'use strict'; // Config -const { masterID } = require('../../config.json'); +const { master } = require('../../config.json'); // DB const { addUser, isUser } = require('../../stores/user'); @@ -27,7 +27,10 @@ const addUserHandler = async (ctx, next) => { ctx.state = { isAdmin: user && user.status === 'admin', - isMaster: user && user.id === masterID, + isMaster: user && + user.id === Number(master) || + user.username.toLowerCase() === + String(master).replace('@', '').toLowerCase(), user: newUser, }; diff --git a/handlers/middlewares/addedToGroup.js b/handlers/middlewares/addedToGroup.js index 485bf57..a530c30 100644 --- a/handlers/middlewares/addedToGroup.js +++ b/handlers/middlewares/addedToGroup.js @@ -5,15 +5,18 @@ const { replyOptions } = require('../../bot/options'); const { admin } = require('../../stores/user'); const { addGroup, managesGroup } = require('../../stores/group'); -const { masterID } = require('../../config.json'); +const { master } = require('../../config.json'); const addedToGroupHandler = async (ctx, next) => { const msg = ctx.message; const { telegram } = ctx; + const isMaster = ctx.from.id === Number(master) || + ctx.from.username.toLowerCase() === + String(master).replace('@', '').toLowerCase(); const wasAdded = msg.new_chat_members.some(user => user.username === ctx.me); - if (wasAdded && ctx.from.id === masterID) { + if (wasAdded && isMaster) { await admin(ctx.from); if (!await managesGroup({ id: ctx.chat.id })) { try { From d487a6fc75b609ab295014e928c1cb557575a827 Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Tue, 10 Oct 2017 22:20:50 +0330 Subject: [PATCH 04/11] ban/warn command on admins doesn't delete the message now --- handlers/commands/ban.js | 8 ++++---- handlers/commands/warn.js | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/handlers/commands/ban.js b/handlers/commands/ban.js index 57d53c6..8040ba7 100644 --- a/handlers/commands/ban.js +++ b/handlers/commands/ban.js @@ -31,6 +31,10 @@ const banHandler = async ({ chat, message, reply, telegram, me, state }) => { return null; } + if (await isAdmin(userToBan)) { + return reply('ℹ️ Can\'t ban other admins.', replyOptions); + } + if (reason.length === 0) { return reply('ℹ️ Need a reason to ban.', replyOptions); } @@ -41,10 +45,6 @@ const banHandler = async ({ chat, message, reply, telegram, me, state }) => { message.reply_to_message.message_id); } - if (await isAdmin(userToBan)) { - return reply('ℹ️ Can\'t ban other admins.', replyOptions); - } - if (await isBanned(userToBan)) { return reply(`🚫 ${link(userToBan)} is already banned.`, replyOptions); diff --git a/handlers/commands/warn.js b/handlers/commands/warn.js index 91af0a5..c85b3b3 100644 --- a/handlers/commands/warn.js +++ b/handlers/commands/warn.js @@ -35,14 +35,14 @@ const warnHandler = async ({ message, chat, reply, me, state }) => { const reason = message.text.split(' ').slice(1).join(' ').trim(); - if (reason.length === 0) { - return reply('ℹ️ Need a reason to warn.', replyOptions); - } - if (await isAdmin(userToWarn)) { return reply('ℹ️ Can\'t warn other admins.', replyOptions); } + if (reason.length === 0) { + return reply('ℹ️ Need a reason to warn.', replyOptions); + } + await warn(userToWarn, reason); const warnCount = await getWarns(userToWarn); const promises = [ From c818df273abe694bb3cb2cf33f4321a5d1ea9ebb Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Wed, 11 Oct 2017 00:02:14 +0330 Subject: [PATCH 05/11] added the ability to disable warning channel/groups forward/link --- handlers/messages/removeLinks.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/handlers/messages/removeLinks.js b/handlers/messages/removeLinks.js index 04b9db8..a063bc0 100644 --- a/handlers/messages/removeLinks.js +++ b/handlers/messages/removeLinks.js @@ -22,22 +22,26 @@ const { listGroups } = require('../../stores/group'); const removeLinks = async ({ message, chat, reply, state }, next) => { const { isAdmin, user } = state; const groups = await listGroups(); - const groupLinks = [ - ...groups.map(group => group.link - ? group.link.split('/joinchat/')[1] - : ''), - ...excludedGroups.map(group => - group.includes('/joinchat/') - ? group.split('/joinchat/')[1] - : group) - ]; + console.log(); + const groupLinks = excludedGroups !== '*' && + [ + ...groups.map(group => group.link + ? group.link.split('/joinchat/')[1] + : ''), + ...excludedGroups.map(group => + group.includes('/joinchat/') + ? group.split('/joinchat/')[1] + : group) + ]; if ( message.forward_from_chat && message.forward_from_chat.type !== 'private' && + excludedChannels !== '*' && !excludedChannels.includes(message.forward_from_chat.username) || message.text && (message.text.includes('t.me') || message.text.includes('telegram.me')) && + excludedGroups !== '*' && !(excludedChannels.includes(message.text) || groupLinks.includes(message.text.split('/joinchat/')[1])) ) { From 7904e1db6cb3c621cf03f4c9360effb1f1c1e6a6 Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Wed, 11 Oct 2017 00:02:45 +0330 Subject: [PATCH 06/11] removed console logs XD --- handlers/commands/leave.js | 1 - handlers/messages/removeLinks.js | 1 - 2 files changed, 2 deletions(-) diff --git a/handlers/commands/leave.js b/handlers/commands/leave.js index 8fd54c3..bbcd1c2 100644 --- a/handlers/commands/leave.js +++ b/handlers/commands/leave.js @@ -13,7 +13,6 @@ const leaveCommandHandler = async ctx => { const group = /^-?\d+/.test(groupName) ? { id: Number(groupName) } : { title: groupName }; - console.log(group); const isGroup = await managesGroup(group); if (!isGroup) { return replyWithHTML( diff --git a/handlers/messages/removeLinks.js b/handlers/messages/removeLinks.js index a063bc0..88e8f48 100644 --- a/handlers/messages/removeLinks.js +++ b/handlers/messages/removeLinks.js @@ -22,7 +22,6 @@ const { listGroups } = require('../../stores/group'); const removeLinks = async ({ message, chat, reply, state }, next) => { const { isAdmin, user } = state; const groups = await listGroups(); - console.log(); const groupLinks = excludedGroups !== '*' && [ ...groups.map(group => group.link From 89a8b5875f643bdaa6aa60eae26b0d1a892ccc73 Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Wed, 11 Oct 2017 00:16:34 +0330 Subject: [PATCH 07/11] fixed channel forwarding warn count bug --- handlers/messages/removeLinks.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/handlers/messages/removeLinks.js b/handlers/messages/removeLinks.js index 88e8f48..a411090 100644 --- a/handlers/messages/removeLinks.js +++ b/handlers/messages/removeLinks.js @@ -16,7 +16,7 @@ const bot = require('../../bot'); const { replyOptions } = require('../../bot/options'); // DB -const { ban, warn } = require('../../stores/user'); +const { ban, warn, getWarns } = require('../../stores/user'); const { listGroups } = require('../../stores/group'); const removeLinks = async ({ message, chat, reply, state }, next) => { @@ -48,13 +48,14 @@ const removeLinks = async ({ message, chat, reply, state }, next) => { return next(); } const reason = 'Channel forward/link'; - const warnCount = await warn(user, reason); + await warn(user, reason); + const warnCount = await getWarns(user); const promises = [ bot.telegram.deleteMessage(chat.id, message.message_id) ]; - if (warnCount < numberOfWarnsToBan) { + if (warnCount.length < numberOfWarnsToBan) { promises.push(reply( - `⚠️ ${link(user)} got warned! (${warnCount}/3)` + + `⚠️ ${link(user)} got warned! (${warnCount.length}/3)` + `\n\nReason: ${reason}`, replyOptions)); } else { @@ -62,7 +63,7 @@ const removeLinks = async ({ message, chat, reply, state }, next) => { promises.push(ban(user, 'Reached max number of warnings')); promises.push(reply( - `🚫 ${link(user)} got banned! (${warnCount}/3)` + + `🚫 ${link(user)} got banned! (${warnCount.length}/3)` + '\n\nReason: Reached max number of warnings', replyOptions)); } From 38b48a48b12c849c63f958d35751c5596da687bc Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Wed, 11 Oct 2017 00:25:30 +0330 Subject: [PATCH 08/11] updated config comments --- config.example.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config.example.json b/config.example.json index 86afca1..6e7a8a0 100644 --- a/config.example.json +++ b/config.example.json @@ -1,8 +1,8 @@ { - "masterID": 123456789, // enter the master admin ID here. It must be number. - "token": "", // enter the bot token here. + "masterID": 123456789, // master admin ID as a number or username as astring. + "token": "", // bot token. "numberOfWarnsToBan": 3, // Number of warns that will get someone banned. "groupsInlineKeyboard": [], // [[inlineButton]] -> inline keyboard to be added to reply to /groups - "excludedChannels": [], // [String] -> list of channels usernames to be excluded from warnings - "excludedGroups": [] // [String] -> list of groups links to be excluded from warnings + "excludedChannels": [], // [String] -> list of channels usernames to be excluded from warnings, use "*" to disable this feature. + "excludedGroups": [] // [String] -> list of groups links to be excluded from warnings, use "*" to disable this feature. } From 752d8396247c74f0e8995fa54d35f55bf5f02613 Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Wed, 11 Oct 2017 00:26:53 +0330 Subject: [PATCH 09/11] master id name fix in example --- config.example.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.example.json b/config.example.json index 6e7a8a0..f625d58 100644 --- a/config.example.json +++ b/config.example.json @@ -1,5 +1,5 @@ { - "masterID": 123456789, // master admin ID as a number or username as astring. + "master": 123456789, // master admin ID as a number or username as astring. "token": "", // bot token. "numberOfWarnsToBan": 3, // Number of warns that will get someone banned. "groupsInlineKeyboard": [], // [[inlineButton]] -> inline keyboard to be added to reply to /groups From b8cba2595f7099afb3e261836af80ce3bccf90eb Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Wed, 11 Oct 2017 21:34:05 +0330 Subject: [PATCH 10/11] check if username exists before using methods --- handlers/messages/addUser.js | 1 + handlers/middlewares/addedToGroup.js | 1 + 2 files changed, 2 insertions(+) diff --git a/handlers/messages/addUser.js b/handlers/messages/addUser.js index ef8e063..07331fd 100644 --- a/handlers/messages/addUser.js +++ b/handlers/messages/addUser.js @@ -29,6 +29,7 @@ const addUserHandler = async (ctx, next) => { isAdmin: user && user.status === 'admin', isMaster: user && user.id === Number(master) || + user.username && user.username.toLowerCase() === String(master).replace('@', '').toLowerCase(), user: newUser, diff --git a/handlers/middlewares/addedToGroup.js b/handlers/middlewares/addedToGroup.js index a530c30..5a4a631 100644 --- a/handlers/middlewares/addedToGroup.js +++ b/handlers/middlewares/addedToGroup.js @@ -11,6 +11,7 @@ const addedToGroupHandler = async (ctx, next) => { const msg = ctx.message; const { telegram } = ctx; const isMaster = ctx.from.id === Number(master) || + ctx.from.username && ctx.from.username.toLowerCase() === String(master).replace('@', '').toLowerCase(); From 846375f96ce97f2df764b61575b319f72c805a15 Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Wed, 11 Oct 2017 21:45:33 +0330 Subject: [PATCH 11/11] [bug fix] check if user exists --- handlers/messages/addUser.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/handlers/messages/addUser.js b/handlers/messages/addUser.js index 07331fd..1f13d6e 100644 --- a/handlers/messages/addUser.js +++ b/handlers/messages/addUser.js @@ -28,10 +28,10 @@ const addUserHandler = async (ctx, next) => { ctx.state = { isAdmin: user && user.status === 'admin', isMaster: user && - user.id === Number(master) || + (user.id === Number(master) || user.username && user.username.toLowerCase() === - String(master).replace('@', '').toLowerCase(), + String(master).replace('@', '').toLowerCase()), user: newUser, };