2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-24 19:07:17 +00:00

76 lines
1.7 KiB
JavaScript
Raw Normal View History

'use strict';
// Utils
const { isMaster } = require('../../utils/config');
const { link, scheduleDeletion } = require('../../utils/tg');
2017-09-23 21:49:23 +03:30
const { logError } = require('../../utils/log');
const { parse, strip } = require('../../utils/parse');
// Bot
const { replyOptions } = require('../../bot/options');
const { telegram } = require('../../bot');
// DB
const { getUser, unadmin } = require('../../stores/user');
const { listGroups } = require('../../stores/group');
const noop = Function.prototype;
const tgUnadmin = async (userToUnadmin) => {
for (const group of await listGroups()) {
telegram.promoteChatMember(group.id, userToUnadmin.id, {
can_change_info: false,
can_delete_messages: false,
can_invite_users: false,
can_pin_messages: false,
can_promote_members: false,
can_restrict_members: false,
}).catch(noop);
}
};
2020-03-10 22:10:48 +01:00
/** @param { import('../../typings/context').ExtendedContext } ctx */
const unAdminHandler = async ({ from, message, reply }) => {
if (!isMaster(from)) return null;
const { targets } = parse(message);
if (targets.length !== 1) {
return reply(
' <b>Specify one user to unadmin.</b>',
replyOptions
).then(scheduleDeletion());
}
const userToUnadmin = await getUser(strip(targets[0]));
2017-09-25 12:40:04 +03:30
if (!userToUnadmin) {
return reply(
'❓ <b>User unknown.</b>',
replyOptions
).then(scheduleDeletion());
}
if (userToUnadmin.status !== 'admin') {
return reply(
` ${link(userToUnadmin)} <b>is not admin.</b>`,
replyOptions
);
}
tgUnadmin(userToUnadmin);
2017-09-23 21:49:23 +03:30
try {
await unadmin(userToUnadmin);
} catch (err) {
logError(err);
2017-09-23 21:49:23 +03:30
}
return reply(
`❗️ ${link(userToUnadmin)} <b>is no longer admin.</b>`,
replyOptions
);
};
module.exports = unAdminHandler;