2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-25 19:37:14 +00:00

83 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-09-21 13:57:49 +04:30
'use strict';
const { last } = require('ramda');
2017-09-21 13:57:49 +04:30
// Utils
const { escapeHtml, link, scheduleDeletion } = require('../../utils/tg');
const { parse, strip } = require('../../utils/parse');
2017-09-21 13:57:49 +04:30
// Config
const { numberOfWarnsToBan } = require('../../config');
2017-09-21 13:57:49 +04:30
// Bot
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');
const { getUser, unwarn } = require('../../stores/user');
2017-09-21 13:57:49 +04:30
const noop = Function.prototype;
const unwarnHandler = async ({ from, message, reply, telegram }) => {
if (!from || from.status !== 'admin') return null;
2017-09-21 13:57:49 +04:30
const { targets } = parse(message);
2017-09-25 12:40:04 +03:30
if (targets.length !== 1) {
return reply(
' <b>Specify one user to unwarn.</b>',
replyOptions
).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) {
return reply(
'❓ <b>User unknown</b>',
replyOptions
).then(scheduleDeletion());
}
const allWarns = userToUnwarn.warns;
if (allWarns.length === 0) {
return reply(
` ${link(userToUnwarn)} <b>already has no warnings.</b>`,
replyOptions
);
}
if (userToUnwarn.status === 'banned') {
const groups = await listGroups();
2017-10-28 20:24:13 +02:00
groups.forEach(group =>
telegram.unbanChatMember(group.id, userToUnwarn.id));
}
2017-10-28 20:24:13 +02:00
await unwarn(userToUnwarn);
2017-09-21 13:57:49 +04:30
if (userToUnwarn.status === 'banned') {
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)
}
const lastWarn = last(allWarns);
2017-09-21 13:57:49 +04:30
return reply(
`${link(from)} <b>pardoned</b> ${link(userToUnwarn)} ` +
`<b>for:</b>\n\n${escapeHtml(lastWarn.reason || lastWarn)}` +
` (${allWarns.length - 1}/${numberOfWarnsToBan})`,
replyOptions
);
2017-09-21 13:57:49 +04:30
};
module.exports = unwarnHandler;