mirror of
https://github.com/thedevs-network/the-guard-bot
synced 2025-08-30 21:55:17 +00:00
/leave now works with id and name too. closes #20
This commit is contained in:
@@ -1,11 +1,34 @@
|
|||||||
'use strict';
|
'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;
|
const { isMaster } = state;
|
||||||
if (!isMaster) return null;
|
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);
|
await removeGroup(chat);
|
||||||
return telegram.leaveChat(chat.id);
|
return telegram.leaveChat(chat.id);
|
||||||
};
|
};
|
||||||
|
@@ -7,7 +7,7 @@ const bot = require('../../bot');
|
|||||||
const { managesGroup } = require('../../stores/group');
|
const { managesGroup } = require('../../stores/group');
|
||||||
|
|
||||||
const linkHandler = async ({ chat, replyWithHTML }) => {
|
const linkHandler = async ({ chat, replyWithHTML }) => {
|
||||||
const group = await managesGroup(chat);
|
const group = await managesGroup({ id: chat.id });
|
||||||
|
|
||||||
const { message_id } = await replyWithHTML(
|
const { message_id } = await replyWithHTML(
|
||||||
'ℹ️ <b>Group\'s link:</b>\n\n' +
|
'ℹ️ <b>Group\'s link:</b>\n\n' +
|
||||||
|
@@ -15,7 +15,7 @@ const addedToGroupHandler = async (ctx, next) => {
|
|||||||
user.username === ctx.me);
|
user.username === ctx.me);
|
||||||
if (wasAdded && ctx.from.id === masterID) {
|
if (wasAdded && ctx.from.id === masterID) {
|
||||||
await admin(ctx.from);
|
await admin(ctx.from);
|
||||||
if (!await managesGroup(ctx.chat)) {
|
if (!await managesGroup({ id: ctx.chat.id })) {
|
||||||
try {
|
try {
|
||||||
const link = await telegram.exportChatInviteLink(ctx.chat.id);
|
const link = await telegram.exportChatInviteLink(ctx.chat.id);
|
||||||
ctx.chat.link = link ? link : '';
|
ctx.chat.link = link ? link : '';
|
||||||
|
@@ -47,7 +47,7 @@ const randomChoice = arr => arr[Math.floor(Math.random() * arr.length)];
|
|||||||
* @returns {Promise.<*>} - returns next object
|
* @returns {Promise.<*>} - returns next object
|
||||||
*/
|
*/
|
||||||
const leaveUnmanagedHandler = async (ctx, next) => {
|
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();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -19,7 +19,7 @@ const listGroups = () =>
|
|||||||
Group.find({});
|
Group.find({});
|
||||||
|
|
||||||
const managesGroup = group =>
|
const managesGroup = group =>
|
||||||
Group.findOne({ id: group.id });
|
Group.findOne(group);
|
||||||
|
|
||||||
const removeGroup = ({ id }) =>
|
const removeGroup = ({ id }) =>
|
||||||
Group.remove({ id });
|
Group.remove({ id });
|
||||||
|
Reference in New Issue
Block a user