2017-09-21 13:57:49 +04:30
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
// Utils
|
2017-09-23 21:40:58 +03:30
|
|
|
|
const { loadJSON } = require('../../utils/json');
|
|
|
|
|
const { link } = require('../../utils/tg');
|
|
|
|
|
const { logError } = require('../../utils/log');
|
2017-09-21 13:57:49 +04:30
|
|
|
|
|
2017-09-21 23:47:26 +04:30
|
|
|
|
// Config
|
|
|
|
|
const { numberOfWarnsToBan } = loadJSON('config.json');
|
|
|
|
|
|
2017-09-21 13:57:49 +04:30
|
|
|
|
// Bot
|
2017-09-23 21:40:58 +03:30
|
|
|
|
const bot = require('../../bot');
|
|
|
|
|
const { replyOptions } = require('../../bot/options');
|
2017-09-21 13:57:49 +04:30
|
|
|
|
|
|
|
|
|
// DB
|
2017-09-23 21:40:58 +03:30
|
|
|
|
const { warn } = require('../../stores/warn');
|
2017-09-27 00:11:20 +03:30
|
|
|
|
const { ban } = require('../../stores/user');
|
|
|
|
|
const { isAdmin } = require('../../stores/user');
|
2017-09-21 13:57:49 +04:30
|
|
|
|
|
|
|
|
|
const warnHandler = async ({ message, chat, reply }) => {
|
2017-09-21 16:14:54 +04:30
|
|
|
|
if (!await isAdmin(message.from)) {
|
2017-09-21 13:57:49 +04:30
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-09-25 12:40:04 +03:30
|
|
|
|
|
|
|
|
|
const userToWarn = message.reply_to_message
|
|
|
|
|
? message.reply_to_message.from
|
|
|
|
|
: message.commandMention
|
|
|
|
|
? message.commandMention
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
if (!userToWarn) {
|
|
|
|
|
return reply('ℹ️ <b>Reply to a message or mentoin a user.</b>',
|
|
|
|
|
replyOptions);
|
2017-09-21 13:57:49 +04:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 warn.</b>', replyOptions);
|
2017-09-21 13:57:49 +04:30
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 16:14:54 +04:30
|
|
|
|
if (await isAdmin(userToWarn)) {
|
2017-09-24 14:28:18 +03:30
|
|
|
|
return reply('ℹ️ <b>Can\'t warn other admins.</b>', replyOptions);
|
2017-09-21 13:57:49 +04:30
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 16:14:54 +04:30
|
|
|
|
const warnCount = await warn(userToWarn, reason);
|
2017-09-21 13:57:49 +04:30
|
|
|
|
const promises = [
|
|
|
|
|
bot.telegram.deleteMessage(chat.id, message.message_id)
|
|
|
|
|
];
|
|
|
|
|
|
2017-09-25 12:40:04 +03:30
|
|
|
|
if (message.reply_to_message) {
|
|
|
|
|
promises.push(bot.telegram.deleteMessage(
|
|
|
|
|
chat.id,
|
|
|
|
|
message.reply_to_message.message_id));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 23:47:26 +04:30
|
|
|
|
if (warnCount < numberOfWarnsToBan) {
|
2017-09-21 13:57:49 +04:30
|
|
|
|
promises.push(reply(
|
2017-09-24 23:24:34 +03:30
|
|
|
|
`⚠️ ${link(message.from)} <b>warned</b> ${link(userToWarn)} ` +
|
|
|
|
|
`<b>for:</b>\n\n ${reason} (${warnCount}/3)`,
|
2017-09-21 13:57:49 +04:30
|
|
|
|
replyOptions));
|
|
|
|
|
} else {
|
|
|
|
|
promises.push(bot.telegram.kickChatMember(chat.id, userToWarn.id));
|
2017-09-21 16:14:54 +04:30
|
|
|
|
promises.push(ban(userToWarn, 'Reached max number of warnings'));
|
2017-09-21 13:57:49 +04:30
|
|
|
|
promises.push(reply(
|
2017-09-24 23:24:34 +03:30
|
|
|
|
`🚫 ${link(message.from)} <b>banned</b> ${link(userToWarn)} ` +
|
|
|
|
|
'<b>for:</b>\n\nReached max number of warnings ' +
|
|
|
|
|
`(${warnCount}/3)\n\n`,
|
2017-09-21 13:57:49 +04:30
|
|
|
|
replyOptions));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Promise.all(promises).catch(logError(process.env.DEBUG));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = warnHandler;
|