2017-09-22 15:52:27 +03:30
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
// Utils
|
2017-10-31 22:20:49 +01:00
|
|
|
|
const { link, scheduleDeletion } = require('../../utils/tg');
|
2017-09-23 21:49:23 +03:30
|
|
|
|
const { logError } = require('../../utils/log');
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
|
|
|
|
// Bot
|
2017-09-23 21:40:58 +03:30
|
|
|
|
const { replyOptions } = require('../../bot/options');
|
2018-02-17 22:03:46 +01:00
|
|
|
|
const { telegram } = require('../../bot');
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
|
|
|
|
// DB
|
2017-09-27 00:11:20 +03:30
|
|
|
|
const { isAdmin, 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()) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2017-10-06 16:34:23 +03:30
|
|
|
|
const unAdminHandler = async ({ message, reply, state }) => {
|
|
|
|
|
const { isMaster } = state;
|
2017-10-06 16:40:49 +03:30
|
|
|
|
if (!isMaster) return null;
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2017-09-25 12:40:04 +03:30
|
|
|
|
const userToUnadmin = message.reply_to_message
|
|
|
|
|
? message.reply_to_message.from
|
|
|
|
|
: message.commandMention
|
|
|
|
|
? message.commandMention
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
if (!userToUnadmin) {
|
2017-10-31 23:08:22 +01:00
|
|
|
|
return reply(
|
|
|
|
|
'ℹ️ <b>Reply to a message or mention a user.</b>',
|
|
|
|
|
replyOptions
|
2018-11-20 11:47:30 +05:30
|
|
|
|
).then(scheduleDeletion());
|
2017-09-22 15:52:27 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!await isAdmin(userToUnadmin)) {
|
2017-10-31 23:08:22 +01:00
|
|
|
|
return reply(
|
|
|
|
|
`ℹ️ ${link(userToUnadmin)} <b>is not admin.</b>`,
|
|
|
|
|
replyOptions
|
|
|
|
|
);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
}
|
|
|
|
|
|
2018-02-17 22:03:46 +01:00
|
|
|
|
tgUnadmin(userToUnadmin);
|
|
|
|
|
|
2017-09-23 21:49:23 +03:30
|
|
|
|
try {
|
|
|
|
|
await unadmin(userToUnadmin);
|
|
|
|
|
} catch (err) {
|
2017-09-27 23:12:12 +03:30
|
|
|
|
logError(err);
|
2017-09-23 21:49:23 +03:30
|
|
|
|
}
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2017-10-31 23:08:22 +01:00
|
|
|
|
return reply(
|
|
|
|
|
`❗️ ${link(userToUnadmin)} <b>is no longer admin.</b>`,
|
|
|
|
|
replyOptions
|
|
|
|
|
);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = unAdminHandler;
|