2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-25 03:17:09 +00:00

107 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2020-05-08 21:31:02 +02:00
// @ts-check
2017-09-21 13:57:49 +04:30
'use strict';
const { last } = require('ramda');
const XRegExp = require('xregexp');
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
// Config
2020-03-09 23:27:19 +01:00
const { numberOfWarnsToBan } = require('../../utils/config').config;
2017-09-21 13:57:49 +04:30
// DB
2017-10-28 20:24:13 +02:00
const { listGroups } = require('../../stores/group');
const { getUser, unwarn } = require('../../stores/user');
2017-09-21 13:57:49 +04:30
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
if (targets.length !== 1) {
2020-05-14 23:18:25 +02:00
return ctx.replyWithHTML(
' <b>Specify one user to unwarn.</b>',
).then(scheduleDeletion());
2017-09-25 12:40:04 +03:30
}
2017-09-21 13:57:49 +04:30
const userToUnwarn = await getUser(strip(targets[0]));
if (!userToUnwarn) {
2020-05-14 23:18:25 +02:00
return ctx.replyWithHTML(
'❓ <b>User unknown</b>',
).then(scheduleDeletion());
}
2020-05-08 21:31:02 +02:00
const allWarns = userToUnwarn.warns.filter(isWarnNotExpired(new Date()));
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>`,
);
}
if (userToUnwarn.status === 'banned') {
await pMap(await listGroups({ type: 'supergroup' }), group =>
ctx.telegram.unbanChatMember(group.id, userToUnwarn.id));
}
2017-10-28 20:24:13 +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(
'⚠ <b>Invalid date</b>',
).then(scheduleDeletion());
}
if (!lastWarn) {
2020-05-14 23:18:25 +02:00
return ctx.replyWithHTML(
'❓ <b>404: Warn not found</b>',
).then(scheduleDeletion());
}
await unwarn(userToUnwarn, lastWarn);
2017-09-21 13:57:49 +04:30
if (userToUnwarn.status === 'banned') {
ctx.telegram.sendMessage(
userToUnwarn.id,
'♻️ You were unbanned from all of the /groups!',
2020-05-08 21:31:02 +02:00
).catch(() => null);
// 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;