2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-26 11:57:09 +00:00

60 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-09-21 13:57:49 +04:30
'use strict';
// Utils
const { link } = require('../utils/tg');
const { logError } = require('../utils/log');
// Bot
const bot = require('../bot');
const { replyOptions } = require('../bot/options');
// DB
const { warn } = require('../stores/warn');
const { ban } = require('../stores/bans');
const { isAdmin } = require('../stores/admins');
2017-09-21 13:57:49 +04:30
const warnHandler = async ({ message, chat, reply }) => {
if (!await isAdmin(message.from)) {
2017-09-21 13:57:49 +04:30
return null;
}
if (!message.reply_to_message) {
return reply('Reply to a message');
}
const messageToWarn = message.reply_to_message;
const userToWarn = messageToWarn.from;
const reason = message.text.split(' ').slice(1).join(' ').trim();
if (reason.length === 0) {
return reply('Need a reason to warn');
2017-09-21 13:57:49 +04:30
}
if (await isAdmin(userToWarn)) {
2017-09-21 13:57:49 +04:30
return reply('Can\'t warn other admin');
}
const warnCount = await warn(userToWarn, reason);
2017-09-21 13:57:49 +04:30
const promises = [
bot.telegram.deleteMessage(chat.id, messageToWarn.message_id),
bot.telegram.deleteMessage(chat.id, message.message_id)
];
if (warnCount < 3) {
promises.push(reply(
`${link(userToWarn)} <b>warned!</b> (${warnCount}/3)\n\n` +
2017-09-21 13:57:49 +04:30
`Reason: ${reason}`,
replyOptions));
} else {
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(userToWarn)} <b>banned</b>! (${warnCount}/3)\n` +
'Reason: Reached max number of warnings',
replyOptions));
}
return Promise.all(promises).catch(logError(process.env.DEBUG));
};
module.exports = warnHandler;