From c5bae13d3f7fc360784db8057eff11f8bb883f9e Mon Sep 17 00:00:00 2001 From: GingerPlusPlus Date: Thu, 24 Jan 2019 19:53:10 +0100 Subject: [PATCH] Make some handlers channel-friendly --- handlers/commands/addCommand.js | 2 +- handlers/commands/commands.js | 4 ++-- handlers/commands/report.js | 1 + handlers/messages/addCustomCmd.js | 7 ++++--- handlers/middlewares/checkLinks.js | 2 +- handlers/middlewares/kickBanned.js | 2 +- handlers/middlewares/removeChannelForwards.js | 3 +++ handlers/middlewares/updateUserData.js | 2 ++ stores/user.js | 9 +++++++-- utils/config.js | 6 +++--- 10 files changed, 25 insertions(+), 13 deletions(-) diff --git a/handlers/commands/addCommand.js b/handlers/commands/addCommand.js index 5f25e00..3cd4d5d 100644 --- a/handlers/commands/addCommand.js +++ b/handlers/commands/addCommand.js @@ -13,8 +13,8 @@ const preserved = require('../commands').handlers; const addCommandHandler = async (ctx) => { const { chat, message, reply } = ctx; - const { id } = ctx.from; if (chat.type !== 'private') return null; + const { id } = ctx.from; if (ctx.from.status !== 'admin') { return reply( diff --git a/handlers/commands/commands.js b/handlers/commands/commands.js index 6dfa610..f3fc69c 100644 --- a/handlers/commands/commands.js +++ b/handlers/commands/commands.js @@ -69,11 +69,11 @@ const commandReferenceHandler = async (ctx) => { : ''; const customCommandsText = masterCommands.repeat(isMaster(ctx.from)) + - adminCommands.repeat(ctx.from.status === 'admin') + + adminCommands.repeat(ctx.from && ctx.from.status === 'admin') + userCommands + '\nCustom commands(prefix with !):\n' + masterCustomCommands.repeat(isMaster(ctx.from)) + - adminCustomCommands.repeat(ctx.from.status === 'admin') + + adminCustomCommands.repeat(ctx.from && ctx.from.status === 'admin') + userCustomCommands; return ctx.replyWithHTML(customCommandsText) diff --git a/handlers/commands/report.js b/handlers/commands/report.js index 8ee72b1..b1af7ed 100644 --- a/handlers/commands/report.js +++ b/handlers/commands/report.js @@ -9,6 +9,7 @@ const { replyOptions } = require('../../bot/options'); const { chats = {} } = require('../../config'); const reportHandler = async ctx => { + if (!ctx.chat.type.endsWith('group')) return null; const msg = ctx.message; if (!msg.reply_to_message) { return ctx.reply( diff --git a/handlers/messages/addCustomCmd.js b/handlers/messages/addCustomCmd.js index e77059f..bc07913 100644 --- a/handlers/messages/addCustomCmd.js +++ b/handlers/messages/addCustomCmd.js @@ -32,7 +32,9 @@ const createNewCommand = ctx => { }; const addCustomCmdHandler = async (ctx, next) => { - const { chat, message, reply, from } = ctx; + if (ctx.chat.type !== 'private') return next(); + + const { message, reply, from } = ctx; const { text } = message; const { id } = from; const isAdmin = from.status === 'admin'; @@ -43,8 +45,7 @@ const addCustomCmdHandler = async (ctx, next) => { } const command = await getCommand({ id, isActive: false }); - if (chat.type !== 'private' || - !isAdmin || + if (!isAdmin || !command || !command.state) { return next(); diff --git a/handlers/middlewares/checkLinks.js b/handlers/middlewares/checkLinks.js index cd99cd4..7bbdd29 100644 --- a/handlers/middlewares/checkLinks.js +++ b/handlers/middlewares/checkLinks.js @@ -155,7 +155,7 @@ const classifyList = (urls) => const matchTmeLinks = R.match(/\b(?:t\.me|telegram\.(?:me|dog))\/[\w-/]+/gi); const classifyCtx = (ctx) => { - if (ctx.chat.type === 'private') return Action.Nothing; + if (!ctx.chat.type.endsWith('group')) return Action.Nothing; const message = ctx.message || ctx.editedMessage; diff --git a/handlers/middlewares/kickBanned.js b/handlers/middlewares/kickBanned.js index bbe5fa7..ed612c9 100644 --- a/handlers/middlewares/kickBanned.js +++ b/handlers/middlewares/kickBanned.js @@ -1,7 +1,7 @@ 'use strict'; const kickBannedHandler = (ctx, next) => { - if (ctx.chat.type === 'private') { + if (!ctx.chat.type.endsWith('group')) { return next(); } if (ctx.from.status === 'banned') { diff --git a/handlers/middlewares/removeChannelForwards.js b/handlers/middlewares/removeChannelForwards.js index 8e18eb8..5be33c3 100644 --- a/handlers/middlewares/removeChannelForwards.js +++ b/handlers/middlewares/removeChannelForwards.js @@ -16,6 +16,8 @@ const isChannelForward = R.pathEq( ); const fromAdmin = R.pathEq([ 'from', 'status' ], 'admin'); +const inGroup = ctx => ctx.chat.type.endsWith('group'); + const capturingGroups = R.tail; const toUsername = R.compose( @@ -35,6 +37,7 @@ const fromWhitelisted = ctx => isWhitelisted(ctx.message.forward_from_chat.username || ''); const pred = R.allPass([ + inGroup, isChannelForward, R.complement(fromAdmin), R.complement(fromWhitelisted), diff --git a/handlers/middlewares/updateUserData.js b/handlers/middlewares/updateUserData.js index c8d5d21..cff4eb8 100644 --- a/handlers/middlewares/updateUserData.js +++ b/handlers/middlewares/updateUserData.js @@ -10,6 +10,8 @@ const updateUserDataHandler = async (ctx, next) => { updateUser(ctx.message.forward_from); } + if (!ctx.from) return next(); + const user = await updateUser(ctx.from); Object.defineProperty(ctx, 'from', { value: { ...user, ...ctx.from } }); diff --git a/stores/user.js b/stores/user.js index 839641a..6877c35 100644 --- a/stores/user.js +++ b/stores/user.js @@ -100,8 +100,13 @@ const getAdmins = () => const unadmin = ({ id }) => User.update({ id }, { $set: { status: 'member' } }); -const isAdmin = ({ id }) => - User.findOne({ id, status: 'admin' }); +const isAdmin = (user) => { + if (!user) return false; + + if (user.status) return user.status === 'admin'; + + return User.findOne({ id: user.id, status: 'admin' }); +}; const ban = ({ id }, ban_details) => { const ban_reason = ban_details.reason; diff --git a/utils/config.js b/utils/config.js index f1c4a02..b239e8f 100644 --- a/utils/config.js +++ b/utils/config.js @@ -11,9 +11,9 @@ if (!masterById && !masterByUsername) { config.master); } -const isMaster = masterById - ? user => user.id === Number(config.master) - : user => user.username && eq.username(user.username, config.master); +const isMaster = masterByUsername + ? user => user && user.username && eq.username(user.username, config.master) + : user => user && user.id === Number(config.master); module.exports = { isMaster,