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:52:58 +03:30
|
|
|
|
const bot = require('../../bot');
|
|
|
|
|
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-09-27 12:34:26 +03:30
|
|
|
|
const { isAdmin, isBanned, ban } = require('../../stores/user');
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2017-10-06 16:34:23 +03:30
|
|
|
|
const banHandler = async ({ chat, message, reply, telegram, me, state }) => {
|
2017-10-06 16:40:49 +03:30
|
|
|
|
if (!state.isAdmin) return null;
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2017-09-25 12:40:04 +03:30
|
|
|
|
const userToBan = message.reply_to_message
|
|
|
|
|
? message.reply_to_message.from
|
|
|
|
|
: message.commandMention
|
|
|
|
|
? message.commandMention
|
|
|
|
|
: null;
|
2017-09-22 15:52:27 +03:30
|
|
|
|
const reason = message.text.split(' ').slice(1).join(' ').trim();
|
|
|
|
|
|
2017-09-25 12:40:04 +03:30
|
|
|
|
if (!userToBan) {
|
|
|
|
|
return reply('ℹ️ <b>Reply to a message or mention a user.</b>',
|
2017-10-31 22:20:49 +01:00
|
|
|
|
replyOptions).then(scheduleDeletion);
|
2017-09-25 12:40:04 +03:30
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 20:22:03 +02:00
|
|
|
|
if (message.chat.type === 'private' || userToBan.username === me) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 22:20:50 +03:30
|
|
|
|
if (await isAdmin(userToBan)) {
|
|
|
|
|
return reply('ℹ️ <b>Can\'t ban other admins.</b>', replyOptions);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:52:27 +03:30
|
|
|
|
if (reason.length === 0) {
|
2017-10-31 22:20:49 +01:00
|
|
|
|
return reply('ℹ️ <b>Need a reason to ban.</b>', replyOptions)
|
|
|
|
|
.then(scheduleDeletion);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
}
|
|
|
|
|
|
2017-09-25 12:40:04 +03:30
|
|
|
|
if (message.reply_to_message) {
|
|
|
|
|
bot.telegram.deleteMessage(
|
|
|
|
|
chat.id,
|
|
|
|
|
message.reply_to_message.message_id);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
}
|
|
|
|
|
|
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) {
|
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-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) {
|
2017-09-27 23:12:12 +03:30
|
|
|
|
logError(err);
|
2017-09-23 21:49:23 +03:30
|
|
|
|
}
|
2017-09-23 19:08:17 +02:00
|
|
|
|
|
2017-10-06 16:34:23 +03:30
|
|
|
|
return reply(`🚫 ${link(state.user)} <b>banned</b> ${link(userToBan)} ` +
|
2017-09-24 23:24:34 +03:30
|
|
|
|
`<b>for:</b>\n\n${reason}`, replyOptions);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = banHandler;
|