2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-23 10:28:09 +00:00
2021-05-07 22:46:11 +02:00

67 lines
1.6 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
// Utils
const { html } = require('../../utils/html');
const { isMaster } = require('../../utils/config');
const { link, scheduleDeletion } = require('../../utils/tg');
const { parse, strip } = require('../../utils/cmd');
// Bot
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);
}
};
/** @param { import('../../typings/context').ExtendedContext } ctx */
const unAdminHandler = async (ctx) => {
if (!isMaster(ctx.from)) return null;
const { targets } = parse(ctx.message);
if (targets.length !== 1) {
return ctx.replyWithHTML(
' <b>Specify one user to unadmin.</b>',
).then(scheduleDeletion());
}
const userToUnadmin = await getUser(strip(targets[0]));
if (!userToUnadmin) {
return ctx.replyWithHTML(
'❓ <b>User unknown.</b>',
).then(scheduleDeletion());
}
if (userToUnadmin.status !== 'admin') {
return ctx.replyWithHTML(
html` ${link(userToUnadmin)} <b>is not admin.</b>`,
);
}
await tgUnadmin(userToUnadmin);
await unadmin(userToUnadmin);
return ctx.replyWithHTML(
html`❗️ ${link(userToUnadmin)} <b>is no longer admin.</b>`,
);
};
module.exports = unAdminHandler;