2020-05-14 23:18:25 +02:00
|
|
|
|
// @ts-check
|
2017-09-21 23:47:26 +04:30
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
// Utils
|
2020-06-15 14:45:55 +02:00
|
|
|
|
const { html, lrm } = require('../../utils/html');
|
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');
|
2020-05-13 22:00:49 +02:00
|
|
|
|
const { pMap } = require('../../utils/promise');
|
2017-09-21 23:47:26 +04:30
|
|
|
|
|
|
|
|
|
// DB
|
2017-11-17 18:53:51 +01:00
|
|
|
|
const { getUser, nowarns } = require('../../stores/user');
|
|
|
|
|
const { listGroups } = require('../../stores/group');
|
2017-09-21 23:47:26 +04:30
|
|
|
|
|
2017-11-19 13:08:11 +01:00
|
|
|
|
// This handler is very similiar to commands/unban.
|
|
|
|
|
// When adding a feature here, please consider adding it there too.
|
|
|
|
|
|
2020-03-10 22:10:48 +01:00
|
|
|
|
/** @param { import('../../typings/context').ExtendedContext } ctx */
|
2020-05-14 23:18:25 +02:00
|
|
|
|
const nowarnsHandler = async (ctx) => {
|
|
|
|
|
if (ctx.from?.status !== 'admin') return null;
|
2017-09-25 12:40:04 +03:30
|
|
|
|
|
2020-05-14 23:18:25 +02:00
|
|
|
|
const { targets } = parse(ctx.message);
|
2017-09-25 12:40:04 +03:30
|
|
|
|
|
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 pardon.</b>',
|
2018-11-20 11:47:30 +05:30
|
|
|
|
).then(scheduleDeletion());
|
2017-09-21 23:47:26 +04:30
|
|
|
|
}
|
|
|
|
|
|
2019-01-28 21:46:29 +01:00
|
|
|
|
const userToUnwarn = await getUser(strip(targets[0]));
|
|
|
|
|
|
|
|
|
|
if (!userToUnwarn) {
|
2020-05-14 23:18:25 +02:00
|
|
|
|
return ctx.replyWithHTML(
|
2019-01-28 21:46:29 +01:00
|
|
|
|
'❓ <b>User unknown.</b>',
|
|
|
|
|
).then(scheduleDeletion());
|
|
|
|
|
}
|
2017-09-21 23:47:26 +04:30
|
|
|
|
|
2019-01-28 21:46:29 +01:00
|
|
|
|
const { warns } = userToUnwarn;
|
2017-11-17 18:53:51 +01:00
|
|
|
|
|
|
|
|
|
if (warns.length === 0) {
|
2020-05-14 23:18:25 +02:00
|
|
|
|
return ctx.replyWithHTML(
|
2020-05-13 15:11:43 +02:00
|
|
|
|
html`ℹ️ ${link(userToUnwarn)} <b>already has no warnings.</b>`,
|
2017-10-31 23:08:22 +01:00
|
|
|
|
);
|
2017-09-24 22:17:27 +03:30
|
|
|
|
}
|
2017-11-17 18:53:51 +01:00
|
|
|
|
|
2019-01-28 21:46:29 +01:00
|
|
|
|
if (userToUnwarn.status === 'banned') {
|
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, userToUnwarn.id));
|
2017-11-17 18:53:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-13 22:05:22 +02:00
|
|
|
|
await nowarns(userToUnwarn);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2019-01-28 21:46:29 +01:00
|
|
|
|
if (userToUnwarn.status === 'banned') {
|
2022-05-31 17:17:45 +02:00
|
|
|
|
ctx.telegram.sendMessage(
|
2017-11-19 13:08:11 +01:00
|
|
|
|
userToUnwarn.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-11-19 13:08:11 +01: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)
|
|
|
|
|
}
|
|
|
|
|
|
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>pardoned</b> ${link(userToUnwarn)}
|
2020-05-13 15:11:43 +02:00
|
|
|
|
for all of their warnings.
|
|
|
|
|
`);
|
2017-09-21 23:47:26 +04:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = nowarnsHandler;
|