2020-05-14 23:18:25 +02:00
|
|
|
|
// @ts-check
|
2017-09-22 15:52:27 +03:30
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
// Utils
|
2020-05-08 14:08:11 +02:00
|
|
|
|
const { displayUser, scheduleDeletion } = require('../../utils/tg');
|
2020-06-15 14:45:55 +02:00
|
|
|
|
const { html, lrm } = require('../../utils/html');
|
2020-05-13 15:11:43 +02:00
|
|
|
|
const { parse, strip } = require('../../utils/cmd');
|
2020-05-13 22:05:22 +02:00
|
|
|
|
const { pMap } = require('../../utils/promise');
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
|
|
|
|
// DB
|
2017-09-25 16:17:18 +03:30
|
|
|
|
const { listGroups } = require('../../stores/group');
|
2019-01-28 21:46:29 +01:00
|
|
|
|
const { getUser, unban } = require('../../stores/user');
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2020-03-10 22:10:48 +01:00
|
|
|
|
/** @param { import('../../typings/context').ExtendedContext } ctx */
|
2020-05-14 23:18:25 +02:00
|
|
|
|
const unbanHandler = async (ctx) => {
|
|
|
|
|
if (ctx.from?.status !== 'admin') return null;
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2020-05-14 23:18:25 +02:00
|
|
|
|
const { targets } = parse(ctx.message);
|
2019-01-28 21:46:29 +01:00
|
|
|
|
|
|
|
|
|
if (targets.length !== 1) {
|
2020-05-14 23:18:25 +02:00
|
|
|
|
return ctx.replyWithHTML(
|
2019-01-28 21:46:29 +01:00
|
|
|
|
'ℹ️ <b>Specify one user to unban.</b>',
|
|
|
|
|
).then(scheduleDeletion());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const userToUnban = await getUser(strip(targets[0]));
|
2017-09-25 12:40:04 +03:30
|
|
|
|
|
|
|
|
|
if (!userToUnban) {
|
2020-05-14 23:18:25 +02:00
|
|
|
|
return ctx.replyWithHTML(
|
2019-01-28 21:46:29 +01:00
|
|
|
|
'❓ <b>User unknown.</b>',
|
2018-11-20 11:47:30 +05:30
|
|
|
|
).then(scheduleDeletion());
|
2017-09-22 15:52:27 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-01-28 21:46:29 +01:00
|
|
|
|
if (userToUnban.status !== 'banned') {
|
2020-05-14 23:18:25 +02:00
|
|
|
|
return ctx.replyWithHTML('ℹ️ <b>User is not banned.</b>');
|
2017-09-22 15:52:27 +03:30
|
|
|
|
}
|
|
|
|
|
|
2020-05-25 22:49:53 +02:00
|
|
|
|
await pMap(await listGroups({ type: 'supergroup' }), (group) =>
|
2022-05-31 17:17:45 +02:00
|
|
|
|
ctx.telegram.unbanChatMember(group.id, userToUnban.id));
|
2017-09-23 14:35:45 +02:00
|
|
|
|
|
2020-05-13 22:05:22 +02:00
|
|
|
|
await unban(userToUnban);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2022-05-31 17:17:45 +02:00
|
|
|
|
ctx.telegram.sendMessage(
|
2017-09-24 22:42:39 +02:00
|
|
|
|
userToUnban.id,
|
2020-05-08 14:08:11 +02:00
|
|
|
|
'♻️ You were unbanned from all of the /groups!',
|
2020-05-14 23:18:25 +02:00
|
|
|
|
).catch(() => null);
|
2017-09-24 22:42:39 +02:00
|
|
|
|
// it's likely that the banned person haven't PMed the bot,
|
|
|
|
|
// which will cause the sendMessage to fail,
|
|
|
|
|
// hance .catch(noop)
|
|
|
|
|
// (it's an expected, non-critical failure)
|
|
|
|
|
|
2017-11-22 21:44:17 +03:30
|
|
|
|
|
2020-05-14 23:18:25 +02:00
|
|
|
|
return ctx.loggedReply(html`
|
2020-06-15 14:45:55 +02:00
|
|
|
|
♻️ ${lrm}${ctx.from.first_name} <b>unbanned</b> ${displayUser(userToUnban)}.
|
2020-05-13 15:11:43 +02:00
|
|
|
|
`);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = unbanHandler;
|