2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-24 10:58:19 +00:00

84 lines
2.0 KiB
JavaScript
Raw Normal View History

2017-09-21 23:47:26 +04:30
'use strict';
// Utils
const { link, scheduleDeletion } = require('../../utils/tg');
2017-09-23 21:49:23 +03:30
const { logError } = require('../../utils/log');
const { parse, strip } = require('../../utils/parse');
2017-09-21 23:47:26 +04:30
// Bot
const { replyOptions } = require('../../bot/options');
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
const noop = Function.prototype;
// 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 */
const nowarnsHandler = async ({ from, message, reply, telegram }) => {
if (!from || from.status !== 'admin') return null;
2017-09-25 12:40:04 +03:30
const { targets } = parse(message);
2017-09-25 12:40:04 +03:30
if (targets.length !== 1) {
return reply(
' <b>Specify one user to pardon.</b>',
replyOptions
).then(scheduleDeletion());
2017-09-21 23:47:26 +04:30
}
const userToUnwarn = await getUser(strip(targets[0]));
if (!userToUnwarn) {
return reply(
'❓ <b>User unknown.</b>',
replyOptions
).then(scheduleDeletion());
}
2017-09-21 23:47:26 +04:30
const { warns } = userToUnwarn;
2017-11-17 18:53:51 +01:00
if (warns.length === 0) {
return reply(
` ${link(userToUnwarn)} <b>already has no warnings.</b>`,
replyOptions
);
}
2017-11-17 18:53:51 +01:00
if (userToUnwarn.status === 'banned') {
2017-11-17 18:53:51 +01:00
const groups = await listGroups();
groups.forEach(group =>
telegram.unbanChatMember(group.id, userToUnwarn.id));
}
2017-09-23 21:49:23 +03:30
try {
await nowarns(userToUnwarn);
} catch (err) {
logError(err);
2017-09-23 21:49:23 +03: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)
}
2017-09-21 23:47:26 +04:30
return reply(
`♻️ ${link(from)} <b>pardoned</b> ${link(userToUnwarn)} ` +
2017-09-24 23:24:34 +03:30
'<b>for all of their warnings.</b>',
replyOptions
);
2017-09-21 23:47:26 +04:30
};
module.exports = nowarnsHandler;