2017-09-22 15:52:27 +03:30
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
// Utils
|
2017-09-23 21:52: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:52:58 +03:30
|
|
|
|
const bot = require('../../bot');
|
|
|
|
|
const { replyOptions } = require('../../bot/options');
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
|
|
|
|
// DB
|
2017-09-23 21:52:58 +03:30
|
|
|
|
const { listGroups } = require('../../stores/groups');
|
|
|
|
|
const { isBanned, ban } = require('../../stores/ban');
|
|
|
|
|
const { isAdmin } = require('../../stores/admin');
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2017-09-23 19:08:17 +02:00
|
|
|
|
const banHandler = async ({ chat, message, reply, telegram }) => {
|
2017-09-22 15:52:27 +03:30
|
|
|
|
if (!await isAdmin(message.from)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const userToBan = message.reply_to_message.from;
|
|
|
|
|
const reason = message.text.split(' ').slice(1).join(' ').trim();
|
|
|
|
|
|
|
|
|
|
if (reason.length === 0) {
|
2017-09-24 14:28:18 +03:30
|
|
|
|
return reply('ℹ️ <b>Need a reason to ban.</b>', replyOptions);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bot.telegram.deleteMessage(chat.id, message.reply_to_message.message_id);
|
|
|
|
|
|
|
|
|
|
if (await isAdmin(userToBan)) {
|
2017-09-24 14:28:18 +03:30
|
|
|
|
return reply('ℹ️ <b>Can\'t ban other admins.</b>', replyOptions);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
}
|
2017-09-23 19:08:17 +02:00
|
|
|
|
|
2017-09-23 15:39:12 +03:30
|
|
|
|
if (await isBanned(userToBan)) {
|
2017-09-24 14:28:18 +03:30
|
|
|
|
return reply(`🚫 ${link(userToBan)} <b>is already banned.</b>`,
|
|
|
|
|
replyOptions);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
}
|
|
|
|
|
|
2017-09-23 21:49:23 +03:30
|
|
|
|
try {
|
|
|
|
|
await ban(userToBan, reason);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logError(process.env.DEBUG)(err);
|
|
|
|
|
}
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2017-09-23 19:08:17 +02:00
|
|
|
|
const groups = await listGroups();
|
|
|
|
|
|
|
|
|
|
const bans = groups.map(group =>
|
|
|
|
|
telegram.kickChatMember(group.id, userToBan.id));
|
|
|
|
|
|
2017-09-23 21:49:23 +03:30
|
|
|
|
try {
|
|
|
|
|
await Promise.all(bans);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logError(process.env.DEBUG)(err);
|
|
|
|
|
}
|
2017-09-23 19:08:17 +02:00
|
|
|
|
|
2017-09-24 14:28:18 +03:30
|
|
|
|
return reply(`🚫 ${link(userToBan)} <b>got banned</b>.\n\n` +
|
2017-09-22 15:52:27 +03:30
|
|
|
|
`Reason: ${reason}`, replyOptions);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = banHandler;
|