2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-09-07 09:35:24 +00:00
Files
the-guard-bot/handlers/messages/removeLinks.js

77 lines
2.0 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');
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
} = require('../../config.json');
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 { ban, warn } = require('../../stores/user');
const { listGroups } = require('../../stores/group');
2017-09-21 13:57:49 +04:30
const removeLinks = async ({ message, chat, reply, state }, next) => {
const { isAdmin, user } = state;
const groups = await listGroups();
const groupLinks = [
...groups.map(group => group.link
? group.link.split('/joinchat/')[1]
: ''),
...excludedGroups.map(group =>
group.includes('/joinchat/')
? group.split('/joinchat/')[1]
: group)
];
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
) {
if (isAdmin) {
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';
const warnCount = await warn(user, 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(user)} <b>got warned!</b> (${warnCount}/3)` +
2017-09-24 14:28:18 +03:30
`\n\nReason: ${reason}`,
2017-09-21 13:57:49 +04:30
replyOptions));
} else {
promises.push(bot.telegram.kickChatMember(chat.id, user.id));
promises.push(ban(user,
2017-09-21 13:57:49 +04:30
'Reached max number of warnings'));
promises.push(reply(
`🚫 ${link(user)} <b>got banned</b>! (${warnCount}/3)` +
2017-09-24 14:28:18 +03:30
'\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);
2017-09-23 21:52:58 +03:30
} catch (err) {
logError(err);
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
return next();
2017-09-21 13:57:49 +04:30
};
2017-09-23 21:52:58 +03:30
module.exports = removeLinks;