2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-30 13:47:54 +00:00

/leave now works with id and name too. closes #20

This commit is contained in:
Pouria Ezzati
2017-10-08 13:19:11 +03:30
parent 5314328ab6
commit c42db48de1
5 changed files with 29 additions and 6 deletions

View File

@@ -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(
' <b>Couldn\'t find a group with that ID/name.</b>'
);
}
await Promise.all([
removeGroup(isGroup),
telegram.leaveChat(isGroup.id)
]);
return replyWithHTML(
'✅ <b>I no longer manage that group.</b>'
);
}
await removeGroup(chat);
return telegram.leaveChat(chat.id);
};

View File

@@ -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(
' <b>Group\'s link:</b>\n\n' +

View File

@@ -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 : '';

View File

@@ -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();
}

View File

@@ -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 });