2017-09-21 13:57:49 +04:30
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
// Utils
|
2017-10-31 22:20:49 +01:00
|
|
|
|
const { link, scheduleDeletion } = require('../../utils/tg');
|
2017-09-23 21:40:58 +03:30
|
|
|
|
const { logError } = require('../../utils/log');
|
2017-09-21 13:57:49 +04:30
|
|
|
|
|
2017-09-21 23:47:26 +04:30
|
|
|
|
// Config
|
2017-11-01 21:34:38 +01:00
|
|
|
|
const {
|
|
|
|
|
numberOfWarnsToBan,
|
|
|
|
|
warnsInlineKeyboard,
|
|
|
|
|
} = require('../../config.json');
|
|
|
|
|
const reply_markup = { inline_keyboard: warnsInlineKeyboard };
|
2017-09-21 23:47:26 +04:30
|
|
|
|
|
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-27 12:34:26 +03:30
|
|
|
|
const { isAdmin, ban, getWarns, warn } = require('../../stores/user');
|
2017-09-21 13:57:49 +04:30
|
|
|
|
|
2017-10-06 16:34:23 +03:30
|
|
|
|
const warnHandler = async ({ message, chat, reply, me, state }) => {
|
|
|
|
|
const { user } = state;
|
2017-10-06 16:40:49 +03:30
|
|
|
|
if (!state.isAdmin) 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) {
|
2017-10-31 23:08:22 +01:00
|
|
|
|
return reply(
|
|
|
|
|
'ℹ️ <b>Reply to a message or mentoin a user.</b>',
|
|
|
|
|
replyOptions
|
|
|
|
|
).then(scheduleDeletion);
|
2017-09-21 13:57:49 +04:30
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 20:22:03 +02:00
|
|
|
|
if (message.chat.type === 'private' || userToWarn.username === me) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 13:57:49 +04:30
|
|
|
|
const reason = message.text.split(' ').slice(1).join(' ').trim();
|
|
|
|
|
|
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-10-10 22:20:50 +03:30
|
|
|
|
if (reason.length === 0) {
|
2017-10-31 22:20:49 +01:00
|
|
|
|
return reply('ℹ️ <b>Need a reason to warn.</b>', replyOptions)
|
|
|
|
|
.then(scheduleDeletion);
|
2017-10-10 22:20:50 +03:30
|
|
|
|
}
|
|
|
|
|
|
2017-09-27 12:34:26 +03:30
|
|
|
|
await warn(userToWarn, reason);
|
|
|
|
|
const warnCount = await getWarns(userToWarn);
|
2017-11-01 15:24:57 +01:00
|
|
|
|
const promises = [];
|
2017-09-21 13:57:49 +04:30
|
|
|
|
|
2017-09-25 12:40:04 +03:30
|
|
|
|
if (message.reply_to_message) {
|
|
|
|
|
promises.push(bot.telegram.deleteMessage(
|
|
|
|
|
chat.id,
|
2017-10-31 23:08:22 +01:00
|
|
|
|
message.reply_to_message.message_id
|
|
|
|
|
));
|
2017-09-25 12:40:04 +03:30
|
|
|
|
}
|
|
|
|
|
|
2017-10-31 19:08:17 +01:00
|
|
|
|
try {
|
|
|
|
|
await reply(
|
|
|
|
|
`⚠️ ${link(user)} <b>warned</b> ${link(userToWarn)} <b>for:</b>` +
|
|
|
|
|
`\n\n${reason} (${warnCount.length}/${numberOfWarnsToBan})`,
|
2017-11-01 21:34:38 +01:00
|
|
|
|
{ parse_mode: 'HTML', reply_markup }
|
2017-10-31 23:08:22 +01:00
|
|
|
|
);
|
2017-10-31 19:08:17 +01:00
|
|
|
|
} catch (e) {
|
|
|
|
|
// we don't expect an error
|
|
|
|
|
// but we do wish to continue if one happens
|
|
|
|
|
// to ban people who reach max number of warnings
|
|
|
|
|
logError(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (warnCount.length >= numberOfWarnsToBan) {
|
2017-09-21 13:57:49 +04:30
|
|
|
|
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-10-06 16:34:23 +03:30
|
|
|
|
`🚫 ${link(user)} <b>banned</b> ${link(userToWarn)} ` +
|
2017-09-24 23:24:34 +03:30
|
|
|
|
'<b>for:</b>\n\nReached max number of warnings ' +
|
2017-10-31 19:08:17 +01:00
|
|
|
|
`(${warnCount.length}/${numberOfWarnsToBan})`,
|
2017-10-31 23:08:22 +01:00
|
|
|
|
replyOptions
|
|
|
|
|
));
|
2017-09-21 13:57:49 +04:30
|
|
|
|
}
|
|
|
|
|
|
2017-09-27 23:12:12 +03:30
|
|
|
|
return Promise.all(promises).catch(logError);
|
2017-09-21 13:57:49 +04:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = warnHandler;
|