2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-25 11:27:20 +00:00

92 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-09-21 13:57:49 +04:30
'use strict';
// Utils
const { link, scheduleDeletion } = 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,
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
const bot = require('../../bot');
const { replyOptions } = require('../../bot/options');
2017-09-21 13:57:49 +04:30
// DB
const { isAdmin, ban, getWarns, warn } = require('../../stores/user');
2017-09-21 13:57:49 +04:30
const warnHandler = async ({ message, chat, reply, me, state }) => {
const { user } = state;
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) {
return reply(
' <b>Reply to a message or mentoin a user.</b>',
replyOptions
).then(scheduleDeletion);
2017-09-21 13:57:49 +04:30
}
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();
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
}
if (reason.length === 0) {
return reply(' <b>Need a reason to warn.</b>', replyOptions)
.then(scheduleDeletion);
}
await warn(userToWarn, reason);
const warnCount = await getWarns(userToWarn);
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,
message.reply_to_message.message_id
));
2017-09-25 12:40:04 +03:30
}
try {
await reply(
`⚠️ ${link(user)} <b>warned</b> ${link(userToWarn)} <b>for:</b>` +
`\n\n${reason} (${warnCount.length}/${numberOfWarnsToBan})`,
{ parse_mode: 'HTML', reply_markup }
);
} 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));
promises.push(ban(userToWarn, 'Reached max number of warnings'));
2017-09-21 13:57:49 +04:30
promises.push(reply(
`🚫 ${link(user)} <b>banned</b> ${link(userToWarn)} ` +
2017-09-24 23:24:34 +03:30
'<b>for:</b>\n\nReached max number of warnings ' +
`(${warnCount.length}/${numberOfWarnsToBan})`,
replyOptions
));
2017-09-21 13:57:49 +04:30
}
return Promise.all(promises).catch(logError);
2017-09-21 13:57:49 +04:30
};
module.exports = warnHandler;