2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-09-08 18:15:19 +00:00
Files
the-guard-bot/handlers/middlewares/removeLinks.js

70 lines
1.9 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 {
2017-09-21 23:47:26 +04:30
excludedChannels,
excludedGroups,
numberOfWarnsToBan
2017-09-21 23:47:26 +04:30
} = 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
2017-09-23 21:52:58 +03:30
const removeLinks = async ({ message, chat, reply }, next) => {
2017-09-21 13:57:49 +04:30
if (
message.forward_from_chat &&
2017-09-23 21:52:58 +03:30
message.forward_from_chat.type !== 'private' &&
!excludedChannels.includes(message.forward_from_chat.username) ||
2017-09-21 23:47:26 +04:30
message.text &&
2017-09-23 21:52:58 +03:30
(message.text.includes('t.me') ||
2017-09-21 23:47:26 +04:30
message.text.includes('telegram.me')) &&
2017-09-23 21:52:58 +03:30
!(excludedChannels.includes(message.text) ||
2017-09-21 23:47:26 +04:30
excludedGroups.includes(message.text.split('/joinchat/')[1]))
2017-09-21 13:57:49 +04:30
) {
const userToWarn = message.from;
2017-09-21 23:47:26 +04:30
if (await isAdmin(userToWarn)) {
2017-09-23 21:52:58 +03:30
return next();
2017-09-21 13:57:49 +04:30
}
2017-09-21 23:47:26 +04:30
const reason = 'Sent a message which was forwarded ' +
'from other channels or included their link';
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-21 23:47:26 +04:30
if (warnCount < numberOfWarnsToBan) {
2017-09-21 13:57:49 +04:30
promises.push(reply(
`${link(userToWarn)} warned! (${warnCount}/3)\n` +
`Reason: ${reason}`,
replyOptions));
} else {
promises.push(bot.telegram.kickChatMember(chat.id, userToWarn.id));
2017-09-21 23:47:26 +04:30
promises.push(ban(userToWarn,
2017-09-21 13:57:49 +04:30
'Reached max number of warnings'));
promises.push(reply(
`${link(userToWarn)} <b>banned</b>! (${warnCount}/3)\n` +
'Reason: Reached max number of warnings',
replyOptions));
}
2017-09-23 21:52:58 +03:30
try {
await Promise.all(promises).catch(logError(process.env.DEBUG));
} catch (err) {
logError(process.env.DEBUG)(err);
}
return next();
2017-09-21 13:57:49 +04:30
}
2017-09-23 21:52:58 +03:30
return next();
2017-09-21 13:57:49 +04:30
};
2017-09-23 21:52:58 +03:30
module.exports = removeLinks;