2017-09-22 15:52:27 +03:30
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
// Utils
|
2017-10-31 22:20:49 +01:00
|
|
|
|
const { link, scheduleDeletion } = require('../../utils/tg');
|
2017-09-23 21:49:23 +03:30
|
|
|
|
const { logError } = require('../../utils/log');
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
|
|
|
|
// Bot
|
2017-09-23 21:40:58 +03:30
|
|
|
|
const { replyOptions } = require('../../bot/options');
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
|
|
|
|
// DB
|
2017-09-25 16:17:18 +03:30
|
|
|
|
const { listGroups } = require('../../stores/group');
|
2017-10-06 16:34:23 +03:30
|
|
|
|
const { isBanned, unban } = require('../../stores/user');
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2017-09-24 22:42:39 +02:00
|
|
|
|
const noop = Function.prototype;
|
|
|
|
|
|
2017-10-06 16:34:23 +03:30
|
|
|
|
const unbanHandler = async ({ message, reply, telegram, state }) => {
|
|
|
|
|
const { isAdmin, user } = state;
|
2017-10-06 16:40:49 +03:30
|
|
|
|
if (!isAdmin) return null;
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2017-09-25 12:40:04 +03:30
|
|
|
|
const userToUnban = message.reply_to_message
|
|
|
|
|
? message.reply_to_message.from
|
|
|
|
|
: message.commandMention
|
|
|
|
|
? message.commandMention
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
if (!userToUnban) {
|
2017-10-31 23:08:22 +01:00
|
|
|
|
return reply(
|
|
|
|
|
'ℹ️ <b>Reply to a message or mention a user.</b>',
|
|
|
|
|
replyOptions
|
2018-11-20 11:47:30 +05:30
|
|
|
|
).then(scheduleDeletion());
|
2017-09-22 15:52:27 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!await isBanned(userToUnban)) {
|
2017-09-24 14:28:18 +03:30
|
|
|
|
return reply('ℹ️ <b>User is not banned.</b>', replyOptions);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
}
|
|
|
|
|
|
2017-09-23 14:35:45 +02:00
|
|
|
|
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) {
|
2017-09-27 23:12:12 +03:30
|
|
|
|
logError(err);
|
2017-09-23 21:49:23 +03:30
|
|
|
|
}
|
2017-09-23 14:35:45 +02:00
|
|
|
|
|
2017-09-23 21:49:23 +03:30
|
|
|
|
try {
|
|
|
|
|
await unban(userToUnban);
|
|
|
|
|
} catch (err) {
|
2017-09-27 23:12:12 +03:30
|
|
|
|
logError(err);
|
2017-09-23 21:49:23 +03:30
|
|
|
|
}
|
2017-09-22 15:52:27 +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)
|
|
|
|
|
|
2017-11-22 21:44:17 +03:30
|
|
|
|
if (userToUnban.first_name === '') {
|
2017-11-23 00:20:21 +03:30
|
|
|
|
return reply(`♻️ ${link(user)} <b>unbanned an user ` +
|
2017-11-22 21:44:17 +03:30
|
|
|
|
`with id</b> <code>${userToUnban.id}</code>.`, replyOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-10-06 16:34:23 +03:30
|
|
|
|
return reply(`♻️ ${link(user)} <b>unbanned</b> ` +
|
2017-09-24 23:24:34 +03:30
|
|
|
|
`${link(userToUnban)}.`, replyOptions);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = unbanHandler;
|