2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-25 11:27:20 +00:00

60 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2020-05-14 23:18:25 +02:00
// @ts-check
'use strict';
// Utils
const { displayUser, scheduleDeletion } = require('../../utils/tg');
2020-06-15 14:45:55 +02:00
const { html, lrm } = require('../../utils/html');
2020-05-13 15:11:43 +02:00
const { parse, strip } = require('../../utils/cmd');
2020-05-13 22:05:22 +02:00
const { pMap } = require('../../utils/promise');
// DB
2017-09-25 16:17:18 +03:30
const { listGroups } = require('../../stores/group');
const { getUser, unban } = require('../../stores/user');
2020-03-10 22:10:48 +01:00
/** @param { import('../../typings/context').ExtendedContext } ctx */
2020-05-14 23:18:25 +02:00
const unbanHandler = async (ctx) => {
if (ctx.from?.status !== 'admin') return null;
2020-05-14 23:18:25 +02:00
const { targets } = parse(ctx.message);
if (targets.length !== 1) {
2020-05-14 23:18:25 +02:00
return ctx.replyWithHTML(
' <b>Specify one user to unban.</b>',
).then(scheduleDeletion());
}
const userToUnban = await getUser(strip(targets[0]));
2017-09-25 12:40:04 +03:30
if (!userToUnban) {
2020-05-14 23:18:25 +02:00
return ctx.replyWithHTML(
'❓ <b>User unknown.</b>',
).then(scheduleDeletion());
}
if (userToUnban.status !== 'banned') {
2020-05-14 23:18:25 +02:00
return ctx.replyWithHTML(' <b>User is not banned.</b>');
}
await pMap(await listGroups({ type: 'supergroup' }), (group) =>
ctx.telegram.unbanChatMember(group.id, userToUnban.id));
2020-05-13 22:05:22 +02:00
await unban(userToUnban);
ctx.telegram.sendMessage(
2017-09-24 22:42:39 +02:00
userToUnban.id,
'♻️ You were unbanned from all of the /groups!',
2020-05-14 23:18:25 +02:00
).catch(() => null);
2017-09-24 22:42:39 +02:00
// 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>unbanned</b> ${displayUser(userToUnban)}.
2020-05-13 15:11:43 +02:00
`);
};
module.exports = unbanHandler;