2017-09-28 22:59:35 +02:00
|
|
|
|
'use strict';
|
|
|
|
|
|
2017-10-08 13:19:11 +03:30
|
|
|
|
const { managesGroup, removeGroup } = require('../../stores/group');
|
2017-09-28 22:59:35 +02:00
|
|
|
|
|
2017-10-08 13:19:11 +03:30
|
|
|
|
const leaveCommandHandler = async ctx => {
|
|
|
|
|
const { chat, message, telegram, state, replyWithHTML } = ctx;
|
2017-10-06 16:34:23 +03:30
|
|
|
|
const { isMaster } = state;
|
2017-10-06 16:40:49 +03:30
|
|
|
|
if (!isMaster) return null;
|
|
|
|
|
|
2017-10-08 13:19:11 +03:30
|
|
|
|
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>'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-06 16:34:23 +03:30
|
|
|
|
await removeGroup(chat);
|
|
|
|
|
return telegram.leaveChat(chat.id);
|
2017-09-28 22:59:35 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = leaveCommandHandler;
|