2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-23 18:38:05 +00:00

71 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2020-05-14 23:18:25 +02:00
// @ts-check
2017-09-21 23:47:26 +04:30
'use strict';
// Utils
2020-06-15 14:45:55 +02:00
const { html, lrm } = require('../../utils/html');
const { link, scheduleDeletion } = require('../../utils/tg');
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 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
// 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 */
2020-05-14 23:18:25 +02:00
const nowarnsHandler = async (ctx) => {
if (ctx.from?.status !== 'admin') return null;
2017-09-25 12:40:04 +03:30
2020-05-14 23:18:25 +02:00
const { 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 pardon.</b>',
).then(scheduleDeletion());
2017-09-21 23:47:26 +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());
}
2017-09-21 23:47:26 +04:30
const { warns } = userToUnwarn;
2017-11-17 18:53:51 +01:00
if (warns.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-11-17 18:53:51 +01:00
if (userToUnwarn.status === 'banned') {
await pMap(await listGroups({ type: 'supergroup' }), (group) =>
ctx.telegram.unbanChatMember(group.id, userToUnwarn.id));
2017-11-17 18:53:51 +01:00
}
2020-05-13 22:05:22 +02:00
await nowarns(userToUnwarn);
if (userToUnwarn.status === 'banned') {
ctx.telegram.sendMessage(
userToUnwarn.id,
'♻️ You were unbanned from all of the /groups!',
2020-05-14 23:18:25 +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-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)}
2020-05-13 15:11:43 +02:00
for all of their warnings.
`);
2017-09-21 23:47:26 +04:30
};
module.exports = nowarnsHandler;