2020-05-08 21:31:02 +02:00
|
|
|
|
// @ts-check
|
2017-09-21 13:57:49 +04:30
|
|
|
|
'use strict';
|
|
|
|
|
|
2018-04-18 21:47:35 +02:00
|
|
|
|
const { last } = require('ramda');
|
2019-05-31 13:36:37 +02:00
|
|
|
|
const XRegExp = require('xregexp');
|
2018-04-18 21:47:35 +02:00
|
|
|
|
|
2017-09-21 13:57:49 +04:30
|
|
|
|
// Utils
|
2020-06-15 14:45:55 +02:00
|
|
|
|
const { html, lrm } = require('../../utils/html');
|
2020-05-13 15:11:43 +02:00
|
|
|
|
const { link, scheduleDeletion } = require('../../utils/tg');
|
2020-05-08 21:31:02 +02:00
|
|
|
|
const { isWarnNotExpired } = require('../../utils/config');
|
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 13:57:49 +04:30
|
|
|
|
|
2017-11-17 18:02:03 +01:00
|
|
|
|
// Config
|
2020-03-09 23:27:19 +01:00
|
|
|
|
const { numberOfWarnsToBan } = require('../../utils/config').config;
|
2017-11-17 18:02:03 +01:00
|
|
|
|
|
2017-09-21 13:57:49 +04:30
|
|
|
|
// DB
|
2017-10-28 20:24:13 +02:00
|
|
|
|
const { listGroups } = require('../../stores/group');
|
2017-11-17 17:42:24 +01:00
|
|
|
|
const { getUser, unwarn } = require('../../stores/user');
|
2017-09-21 13:57:49 +04:30
|
|
|
|
|
2019-05-31 13:36:37 +02:00
|
|
|
|
const dateRegex = XRegExp.tag('nix')`^
|
|
|
|
|
\d{4} # year
|
|
|
|
|
-\d{2} # month
|
|
|
|
|
(-\d{2} # day
|
|
|
|
|
([T\s]\d{2} # hour
|
|
|
|
|
(:\d{2} # min
|
|
|
|
|
(:\d{2} # sec
|
|
|
|
|
(.\d{3}Z? # ms
|
|
|
|
|
)?)?)?)?)?
|
|
|
|
|
$`;
|
|
|
|
|
|
2020-03-10 22:10:48 +01:00
|
|
|
|
/** @param { import('../../typings/context').ExtendedContext } ctx */
|
2020-05-14 23:18:25 +02:00
|
|
|
|
const unwarnHandler = async (ctx) => {
|
|
|
|
|
if (ctx.from?.status !== 'admin') return null;
|
2017-09-21 13:57:49 +04:30
|
|
|
|
|
2020-05-14 23:18:25 +02:00
|
|
|
|
const { reason, 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 unwarn.</b>',
|
2018-11-20 11:47:30 +05:30
|
|
|
|
).then(scheduleDeletion());
|
2017-09-25 12:40:04 +03:30
|
|
|
|
}
|
2017-09-21 13:57:49 +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-27 12:34:26 +03:30
|
|
|
|
|
2020-05-08 21:31:02 +02:00
|
|
|
|
const allWarns = userToUnwarn.warns.filter(isWarnNotExpired(new Date()));
|
2017-11-17 17:42:24 +01:00
|
|
|
|
|
|
|
|
|
if (allWarns.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-27 12:34:26 +03:30
|
|
|
|
}
|
|
|
|
|
|
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 17:42:24 +01:00
|
|
|
|
}
|
2017-10-28 20:24:13 +02:00
|
|
|
|
|
2019-05-31 13:36:37 +02:00
|
|
|
|
let lastWarn;
|
|
|
|
|
if (!reason) {
|
|
|
|
|
lastWarn = last(allWarns);
|
|
|
|
|
} else if (dateRegex.test(reason)) {
|
|
|
|
|
const normalized = reason.replace(' ', 'T').toUpperCase();
|
|
|
|
|
lastWarn = allWarns.find(({ date }) =>
|
|
|
|
|
date && date.toISOString().startsWith(normalized));
|
|
|
|
|
} else {
|
2020-05-14 23:18:25 +02:00
|
|
|
|
return ctx.replyWithHTML(
|
2019-05-31 13:36:37 +02:00
|
|
|
|
'⚠ <b>Invalid date</b>',
|
|
|
|
|
).then(scheduleDeletion());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!lastWarn) {
|
2020-05-14 23:18:25 +02:00
|
|
|
|
return ctx.replyWithHTML(
|
2019-05-31 13:36:37 +02:00
|
|
|
|
'❓ <b>404: Warn not found</b>',
|
|
|
|
|
).then(scheduleDeletion());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await unwarn(userToUnwarn, lastWarn);
|
2017-09-21 13:57:49 +04: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-08 21:31:02 +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-13 15:11:43 +02:00
|
|
|
|
const count = html`<b>${allWarns.length}</b>/${numberOfWarnsToBan}`;
|
|
|
|
|
|
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)} for
|
|
|
|
|
${count}: ${lrm}${lastWarn.reason || lastWarn}
|
2020-05-13 15:11:43 +02:00
|
|
|
|
`);
|
2017-09-21 13:57:49 +04:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = unwarnHandler;
|