2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-10-01 11:20:53 +00:00
Files
the-guard-bot/handlers/middlewares/removeLinks.js

77 lines
2.1 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');
const { listGroups } = require('../../stores/group');
2017-09-21 13:57:49 +04:30
2017-09-23 21:52:58 +03:30
const removeLinks = async ({ message, chat, reply }, next) => {
const groups = await listGroups();
const groupLinks = [
...groups.map(group => group.link
? group.link.split('/joinchat/')[1]
: ''),
...excludedGroups
];
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) ||
groupLinks.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-24 14:28:18 +03:30
const reason = 'Channel forward/link';
2017-09-21 23:47:26 +04:30
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(
2017-09-24 14:28:18 +03:30
`⚠️ ${link(userToWarn)} <b>got warned!</b> (${warnCount}/3)` +
`\n\nReason: ${reason}`,
2017-09-21 13:57:49 +04:30
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(
2017-09-24 14:28:18 +03:30
`🚫 ${link(userToWarn)} <b>got banned</b>! (${warnCount}/3)` +
'\n\nReason: Reached max number of warnings',
2017-09-21 13:57:49 +04:30
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;