2017-09-21 13:57:49 +04:30
|
|
|
|
'use strict';
|
|
|
|
|
|
2018-04-18 21:47:35 +02:00
|
|
|
|
const { last } = require('ramda');
|
|
|
|
|
|
2017-09-21 13:57:49 +04:30
|
|
|
|
// Utils
|
2019-04-06 20:58:56 +02:00
|
|
|
|
const { escapeHtml, link, scheduleDeletion } = require('../../utils/tg');
|
2019-01-28 21:46:29 +01:00
|
|
|
|
const { parse, strip } = require('../../utils/parse');
|
2017-09-21 13:57:49 +04:30
|
|
|
|
|
2017-11-17 18:02:03 +01:00
|
|
|
|
// Config
|
2017-11-24 12:31:47 +01:00
|
|
|
|
const { numberOfWarnsToBan } = require('../../config');
|
2017-11-17 18:02:03 +01:00
|
|
|
|
|
2017-09-21 13:57:49 +04:30
|
|
|
|
// Bot
|
2017-09-23 21:40:58 +03:30
|
|
|
|
const { replyOptions } = require('../../bot/options');
|
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
|
|
|
|
|
2017-11-19 13:08:11 +01:00
|
|
|
|
const noop = Function.prototype;
|
|
|
|
|
|
2019-01-28 21:46:29 +01:00
|
|
|
|
const unwarnHandler = async ({ from, message, reply, telegram }) => {
|
|
|
|
|
if (!from || from.status !== 'admin') return null;
|
2017-09-21 13:57:49 +04:30
|
|
|
|
|
2019-01-28 21:46:29 +01:00
|
|
|
|
const { targets } = parse(message);
|
2017-09-25 12:40:04 +03:30
|
|
|
|
|
2019-01-28 21:46:29 +01:00
|
|
|
|
if (targets.length !== 1) {
|
2017-10-31 23:08:22 +01:00
|
|
|
|
return reply(
|
2019-01-28 21:46:29 +01:00
|
|
|
|
'ℹ️ <b>Specify one user to unwarn.</b>',
|
2017-10-31 23:08:22 +01:00
|
|
|
|
replyOptions
|
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) {
|
|
|
|
|
return reply(
|
|
|
|
|
'❓ <b>User unknown</b>',
|
|
|
|
|
replyOptions
|
|
|
|
|
).then(scheduleDeletion());
|
|
|
|
|
}
|
2017-09-27 12:34:26 +03:30
|
|
|
|
|
2019-01-28 21:46:29 +01:00
|
|
|
|
const allWarns = userToUnwarn.warns;
|
2017-11-17 17:42:24 +01:00
|
|
|
|
|
|
|
|
|
if (allWarns.length === 0) {
|
2017-10-31 23:08:22 +01:00
|
|
|
|
return reply(
|
|
|
|
|
`ℹ️ ${link(userToUnwarn)} <b>already has no warnings.</b>`,
|
|
|
|
|
replyOptions
|
|
|
|
|
);
|
2017-09-27 12:34:26 +03:30
|
|
|
|
}
|
|
|
|
|
|
2019-01-28 21:46:29 +01:00
|
|
|
|
if (userToUnwarn.status === 'banned') {
|
2017-11-17 17:42:24 +01:00
|
|
|
|
const groups = await listGroups();
|
2017-10-28 20:24:13 +02:00
|
|
|
|
|
2017-11-17 17:42:24 +01:00
|
|
|
|
groups.forEach(group =>
|
|
|
|
|
telegram.unbanChatMember(group.id, userToUnwarn.id));
|
|
|
|
|
}
|
2017-10-28 20:24:13 +02:00
|
|
|
|
|
2017-09-27 12:34:26 +03:30
|
|
|
|
await unwarn(userToUnwarn);
|
2017-09-21 13:57:49 +04:30
|
|
|
|
|
2019-01-28 21:46:29 +01:00
|
|
|
|
if (userToUnwarn.status === 'banned') {
|
2017-11-19 13:08:11 +01:00
|
|
|
|
telegram.sendMessage(
|
|
|
|
|
userToUnwarn.id,
|
|
|
|
|
'♻️ You were unbanned from all of the /groups!'
|
|
|
|
|
).catch(noop);
|
|
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 21:47:35 +02:00
|
|
|
|
const lastWarn = last(allWarns);
|
|
|
|
|
|
2017-09-21 13:57:49 +04:30
|
|
|
|
return reply(
|
2019-01-28 21:46:29 +01:00
|
|
|
|
`❎ ${link(from)} <b>pardoned</b> ${link(userToUnwarn)} ` +
|
2019-04-06 20:58:56 +02:00
|
|
|
|
`<b>for:</b>\n\n${escapeHtml(lastWarn.reason || lastWarn)}` +
|
2017-11-17 18:02:03 +01:00
|
|
|
|
` (${allWarns.length - 1}/${numberOfWarnsToBan})`,
|
2017-10-31 23:08:22 +01:00
|
|
|
|
replyOptions
|
|
|
|
|
);
|
2017-09-21 13:57:49 +04:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = unwarnHandler;
|