2017-09-22 15:52:27 +03:30
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
// Utils
|
2017-09-23 21:40:58 +03:30
|
|
|
|
const { link } = 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-23 21:40:58 +03:30
|
|
|
|
const { listGroups } = require('../../stores/groups');
|
|
|
|
|
const { isBanned, unban } = require('../../stores/ban');
|
|
|
|
|
const admins = require('../../stores/admin');
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2017-09-23 14:35:45 +02:00
|
|
|
|
const unbanHandler = async ({ message, reply, telegram }) => {
|
2017-09-22 15:52:27 +03:30
|
|
|
|
if (!await admins.isAdmin(message.from)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!message.reply_to_message) {
|
2017-09-24 14:28:18 +03:30
|
|
|
|
return reply('ℹ️ <b>Reply to a message.</b>', replyOptions);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const userToUnban = message.reply_to_message.from;
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
logError(process.env.DEBUG)(err);
|
|
|
|
|
}
|
2017-09-23 14:35:45 +02:00
|
|
|
|
|
2017-09-23 21:49:23 +03:30
|
|
|
|
try {
|
|
|
|
|
await unban(userToUnban);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logError(process.env.DEBUG)(err);
|
|
|
|
|
}
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2017-09-24 14:28:18 +03:30
|
|
|
|
return reply(`♻️ ${link(userToUnban)} <b>is unbanned.</b>`,
|
|
|
|
|
replyOptions);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = unbanHandler;
|