From c42db48de1d9ae32f3b5ca1d08903ccdc147da7f Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Sun, 8 Oct 2017 13:19:11 +0330 Subject: [PATCH] /leave now works with id and name too. closes #20 --- handlers/commands/leave.js | 27 ++++++++++++++++++++++++-- handlers/commands/link.js | 2 +- handlers/middlewares/addedToGroup.js | 2 +- handlers/middlewares/leaveUnmanaged.js | 2 +- stores/group.js | 2 +- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/handlers/commands/leave.js b/handlers/commands/leave.js index e20445f..8fd54c3 100644 --- a/handlers/commands/leave.js +++ b/handlers/commands/leave.js @@ -1,11 +1,34 @@ 'use strict'; -const { removeGroup } = require('../../stores/group'); +const { managesGroup, removeGroup } = require('../../stores/group'); -const leaveCommandHandler = async ({ chat, telegram, state }) => { +const leaveCommandHandler = async ctx => { + const { chat, message, telegram, state, replyWithHTML } = ctx; const { isMaster } = state; if (!isMaster) return null; + const groupName = message.text.split(' ').slice(1).join(' '); + + if (groupName) { + const group = /^-?\d+/.test(groupName) + ? { id: Number(groupName) } + : { title: groupName }; + console.log(group); + const isGroup = await managesGroup(group); + if (!isGroup) { + return replyWithHTML( + 'ℹ️ Couldn\'t find a group with that ID/name.' + ); + } + await Promise.all([ + removeGroup(isGroup), + telegram.leaveChat(isGroup.id) + ]); + return replyWithHTML( + '✅ I no longer manage that group.' + ); + } + await removeGroup(chat); return telegram.leaveChat(chat.id); }; diff --git a/handlers/commands/link.js b/handlers/commands/link.js index bbd201f..9310eec 100644 --- a/handlers/commands/link.js +++ b/handlers/commands/link.js @@ -7,7 +7,7 @@ const bot = require('../../bot'); const { managesGroup } = require('../../stores/group'); const linkHandler = async ({ chat, replyWithHTML }) => { - const group = await managesGroup(chat); + const group = await managesGroup({ id: chat.id }); const { message_id } = await replyWithHTML( 'ℹ️ Group\'s link:\n\n' + diff --git a/handlers/middlewares/addedToGroup.js b/handlers/middlewares/addedToGroup.js index 2c51f57..485bf57 100644 --- a/handlers/middlewares/addedToGroup.js +++ b/handlers/middlewares/addedToGroup.js @@ -15,7 +15,7 @@ const addedToGroupHandler = async (ctx, next) => { user.username === ctx.me); if (wasAdded && ctx.from.id === masterID) { await admin(ctx.from); - if (!await managesGroup(ctx.chat)) { + if (!await managesGroup({ id: ctx.chat.id })) { try { const link = await telegram.exportChatInviteLink(ctx.chat.id); ctx.chat.link = link ? link : ''; diff --git a/handlers/middlewares/leaveUnmanaged.js b/handlers/middlewares/leaveUnmanaged.js index f3edd1f..2997d98 100644 --- a/handlers/middlewares/leaveUnmanaged.js +++ b/handlers/middlewares/leaveUnmanaged.js @@ -47,7 +47,7 @@ const randomChoice = arr => arr[Math.floor(Math.random() * arr.length)]; * @returns {Promise.<*>} - returns next object */ const leaveUnmanagedHandler = async (ctx, next) => { - if (ctx.chat.type === 'private' || await managesGroup(ctx.chat)) { + if (ctx.chat.type === 'private' || await managesGroup({ id: ctx.chat.id })) { return next(); } diff --git a/stores/group.js b/stores/group.js index d458606..88f5ffb 100644 --- a/stores/group.js +++ b/stores/group.js @@ -19,7 +19,7 @@ const listGroups = () => Group.find({}); const managesGroup = group => - Group.findOne({ id: group.id }); + Group.findOne(group); const removeGroup = ({ id }) => Group.remove({ id });