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

76 lines
1.7 KiB
JavaScript
Raw Normal View History

'use strict';
// Utils
const { displayUser, link, scheduleDeletion } = require('../../utils/tg');
2017-09-23 21:49:23 +03:30
const { logError } = require('../../utils/log');
const { parse, strip } = require('../../utils/parse');
// Bot
const { replyOptions } = require('../../bot/options');
// DB
2017-09-25 16:17:18 +03:30
const { listGroups } = require('../../stores/group');
const { getUser, unban } = require('../../stores/user');
2017-09-24 22:42:39 +02:00
const noop = Function.prototype;
2020-03-10 22:10:48 +01:00
/** @param { import('../../typings/context').ExtendedContext } ctx */
const unbanHandler = async ({ from, message, reply, telegram }) => {
if (!from || from.status !== 'admin') return null;
const { targets } = parse(message);
if (targets.length !== 1) {
return reply(
' <b>Specify one user to unban.</b>',
replyOptions
).then(scheduleDeletion());
}
const userToUnban = await getUser(strip(targets[0]));
2017-09-25 12:40:04 +03:30
if (!userToUnban) {
return reply(
'❓ <b>User unknown.</b>',
replyOptions
).then(scheduleDeletion());
}
if (userToUnban.status !== 'banned') {
2017-09-24 14:28:18 +03:30
return reply(' <b>User is not banned.</b>', replyOptions);
}
const groups = await listGroups();
const unbans = groups.map(group =>
telegram.unbanChatMember(group.id, userToUnban.id));
2017-09-23 21:49:23 +03:30
try {
await Promise.all(unbans);
} catch (err) {
logError(err);
2017-09-23 21:49:23 +03:30
}
2017-09-23 21:49:23 +03:30
try {
await unban(userToUnban);
} catch (err) {
logError(err);
2017-09-23 21:49:23 +03:30
}
2017-09-24 22:42:39 +02:00
telegram.sendMessage(
userToUnban.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)
return reply(`♻️ ${link(from)} <b>unbanned</b> ` +
`${displayUser(userToUnban)}.`, replyOptions);
};
module.exports = unbanHandler;