mirror of
https://github.com/thedevs-network/the-guard-bot
synced 2025-08-30 05:37:59 +00:00
/leave now works with id and name too. closes #20
This commit is contained in:
parent
5314328ab6
commit
c42db48de1
@ -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);
|
||||
};
|
||||
|
@ -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' +
|
||||
|
@ -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 : '';
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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 });
|
||||
|
Loading…
x
Reference in New Issue
Block a user