2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-25 03:17:09 +00:00

64 lines
1.8 KiB
JavaScript
Raw Normal View History

2017-09-21 13:57:49 +04:30
'use strict';
// Utils
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
const bot = require('../../bot');
const { replyOptions } = require('../../bot/options');
2017-09-21 13:57:49 +04:30
// DB
const { warn } = require('../../stores/warn');
const { ban } = require('../../stores/ban');
const { isAdmin } = require('../../stores/admin');
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) {
2017-09-24 14:28:18 +03:30
return reply(' <b>Reply to a message.</b>', replyOptions);
2017-09-21 13:57:49 +04:30
}
const messageToWarn = message.reply_to_message;
const userToWarn = messageToWarn.from;
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
}
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
}
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)
];
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 14:28:18 +03:30
`⚠️ ${link(userToWarn)} <b>got 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(
2017-09-24 14:28:18 +03:30
`🚫 ${link(userToWarn)} <b>got banned!</b> (${warnCount}/3)\n\n` +
2017-09-21 13:57:49 +04:30
'Reason: Reached max number of warnings',
replyOptions));
}
return Promise.all(promises).catch(logError(process.env.DEBUG));
};
module.exports = warnHandler;