diff --git a/config.example.json b/config.example.json index 86afca1..f625d58 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. + "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 - "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. } 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/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/commands/removeCommand.js b/handlers/commands/removeCommand.js index 635a07c..0eda575 100644 --- a/handlers/commands/removeCommand.js +++ b/handlers/commands/removeCommand.js @@ -29,14 +29,15 @@ 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); } await removeCommand({ name: commandName }); return reply( - `✅ /${commandName} ` + + `✅ !${commandName} ` + 'has been removed successfully.', 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 = [ 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/addUser.js b/handlers/messages/addUser.js index 1eadd76..1f13d6e 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,11 @@ 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 && + user.username.toLowerCase() === + String(master).replace('@', '').toLowerCase()), user: newUser, }; diff --git a/handlers/messages/removeLinks.js b/handlers/messages/removeLinks.js index 04b9db8..a411090 100644 --- a/handlers/messages/removeLinks.js +++ b/handlers/messages/removeLinks.js @@ -16,28 +16,31 @@ 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) => { 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) - ]; + 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])) ) { @@ -45,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 { @@ -59,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)); } 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(); diff --git a/handlers/middlewares/addedToGroup.js b/handlers/middlewares/addedToGroup.js index 485bf57..5a4a631 100644 --- a/handlers/middlewares/addedToGroup.js +++ b/handlers/middlewares/addedToGroup.js @@ -5,15 +5,19 @@ 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 && + 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 {