2017-09-22 15:52:27 +03:30
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
// Utils
|
2020-05-13 15:11:43 +02:00
|
|
|
|
const { html } = require('../../utils/html');
|
2019-01-28 21:46:29 +01:00
|
|
|
|
const { isMaster } = require('../../utils/config');
|
2017-10-31 22:20:49 +01:00
|
|
|
|
const { link, scheduleDeletion } = require('../../utils/tg');
|
2020-05-13 15:11:43 +02:00
|
|
|
|
const { parse, strip } = require('../../utils/cmd');
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
|
|
|
|
// Bot
|
2018-02-17 22:03:46 +01:00
|
|
|
|
const { telegram } = require('../../bot');
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
|
|
|
|
// DB
|
2019-01-28 21:46:29 +01:00
|
|
|
|
const { getUser, unadmin } = require('../../stores/user');
|
2018-02-17 22:03:46 +01:00
|
|
|
|
const { listGroups } = require('../../stores/group');
|
|
|
|
|
|
|
|
|
|
const noop = Function.prototype;
|
|
|
|
|
|
|
|
|
|
const tgUnadmin = async (userToUnadmin) => {
|
|
|
|
|
for (const group of await listGroups()) {
|
2023-03-08 14:08:57 +01:00
|
|
|
|
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);
|
2018-02-17 22:03:46 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2020-03-10 22:10:48 +01:00
|
|
|
|
/** @param { import('../../typings/context').ExtendedContext } ctx */
|
2021-05-07 22:46:11 +02:00
|
|
|
|
const unAdminHandler = async (ctx) => {
|
|
|
|
|
if (!isMaster(ctx.from)) return null;
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2021-05-07 22:46:11 +02:00
|
|
|
|
const { targets } = parse(ctx.message);
|
2019-01-28 21:46:29 +01:00
|
|
|
|
|
|
|
|
|
if (targets.length !== 1) {
|
2023-03-08 14:08:57 +01:00
|
|
|
|
return ctx
|
|
|
|
|
.replyWithHTML('ℹ️ <b>Specify one user to unadmin.</b>')
|
|
|
|
|
.then(scheduleDeletion());
|
2019-01-28 21:46:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const userToUnadmin = await getUser(strip(targets[0]));
|
2017-09-25 12:40:04 +03:30
|
|
|
|
|
|
|
|
|
if (!userToUnadmin) {
|
2023-03-08 14:08:57 +01:00
|
|
|
|
return ctx
|
|
|
|
|
.replyWithHTML('❓ <b>User unknown.</b>')
|
|
|
|
|
.then(scheduleDeletion());
|
2017-09-22 15:52:27 +03:30
|
|
|
|
}
|
|
|
|
|
|
2019-01-28 21:46:29 +01:00
|
|
|
|
if (userToUnadmin.status !== 'admin') {
|
2021-05-07 22:46:11 +02:00
|
|
|
|
return ctx.replyWithHTML(
|
2023-03-08 14:08:57 +01:00
|
|
|
|
html`ℹ️ ${link(userToUnadmin)} <b>is not admin.</b>`
|
2017-10-31 23:08:22 +01:00
|
|
|
|
);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
}
|
|
|
|
|
|
2020-05-13 15:11:43 +02:00
|
|
|
|
await tgUnadmin(userToUnadmin);
|
2018-02-17 22:03:46 +01:00
|
|
|
|
|
2020-05-13 22:05:22 +02:00
|
|
|
|
await unadmin(userToUnadmin);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2021-05-07 22:46:11 +02:00
|
|
|
|
return ctx.replyWithHTML(
|
2023-03-08 14:08:57 +01:00
|
|
|
|
html`❗️ ${link(userToUnadmin)} <b>is no longer admin.</b>`
|
2017-10-31 23:08:22 +01:00
|
|
|
|
);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = unAdminHandler;
|