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-22 15:52:27 +03:30
|
|
|
|
|
|
|
|
|
// Bot
|
2017-09-23 21:52:58 +03:30
|
|
|
|
const { replyOptions } = require('../../bot/options');
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
|
|
|
|
// DB
|
2018-02-05 19:17:52 +01:00
|
|
|
|
const { isAdmin, isBanned } = require('../../stores/user');
|
|
|
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
const ban = require('../../actions/ban');
|
|
|
|
|
|
|
|
|
|
const banHandler = async (ctx) => {
|
|
|
|
|
const { message, reply, me } = ctx;
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2017-09-25 12:40:04 +03:30
|
|
|
|
const userToBan = message.reply_to_message
|
2017-12-15 03:48:27 +05:00
|
|
|
|
? Object.assign({ username: '' }, message.reply_to_message.from)
|
2017-09-25 12:40:04 +03:30
|
|
|
|
: message.commandMention
|
2017-12-15 03:48:27 +05:00
|
|
|
|
? Object.assign({ username: '' }, message.commandMention)
|
2017-09-25 12:40:04 +03:30
|
|
|
|
: null;
|
2017-09-22 15:52:27 +03:30
|
|
|
|
const reason = message.text.split(' ').slice(1).join(' ').trim();
|
|
|
|
|
|
2018-02-05 19:17:52 +01:00
|
|
|
|
if (ctx.from.status !== 'admin') return null;
|
2017-11-02 00:18:59 +03:30
|
|
|
|
|
|
|
|
|
if (message.chat.type === 'private') {
|
|
|
|
|
return reply(
|
|
|
|
|
'ℹ️ <b>This command is only available in groups.</b>',
|
|
|
|
|
replyOptions
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-25 12:40:04 +03:30
|
|
|
|
if (!userToBan) {
|
2017-10-31 23:08:22 +01:00
|
|
|
|
return reply(
|
|
|
|
|
'ℹ️ <b>Reply to a message or mention a user.</b>',
|
|
|
|
|
replyOptions
|
|
|
|
|
).then(scheduleDeletion);
|
2017-09-25 12:40:04 +03:30
|
|
|
|
}
|
|
|
|
|
|
2017-12-15 03:48:27 +05:00
|
|
|
|
if (userToBan.username.toLowerCase() === me.toLowerCase()) return null;
|
2017-11-23 18:06:52 +03:30
|
|
|
|
|
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) {
|
2018-02-05 19:17:52 +01:00
|
|
|
|
ctx.deleteMessage(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-10-31 23:08:22 +01:00
|
|
|
|
return reply(
|
|
|
|
|
`🚫 ${link(userToBan)} <b>is already banned.</b>`,
|
|
|
|
|
replyOptions
|
|
|
|
|
);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
}
|
|
|
|
|
|
2018-02-05 19:17:52 +01:00
|
|
|
|
return ban({ admin: ctx.from, reason, userToBan }).then(ctx.replyWithHTML);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = banHandler;
|