2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-09-01 06:35:23 +00:00

merging excluded groups and channels

This commit is contained in:
Pouria Ezzati
2017-11-18 13:11:12 +03:30
parent 64e1942a25
commit 25ff603556
2 changed files with 17 additions and 34 deletions

View File

@@ -6,6 +6,5 @@
"numberOfWarnsToBan": 3, "numberOfWarnsToBan": 3,
"groupsInlineKeyboard": [], "groupsInlineKeyboard": [],
"warnInlineKeyboard": [], "warnInlineKeyboard": [],
"excludedChannels": [], "excludeLinks": []
"excludedGroups": []
} }

View File

@@ -6,8 +6,7 @@ const { logError } = require('../../utils/log');
// Config // Config
const { const {
excludedChannels, excludeLinks,
excludedGroups,
numberOfWarnsToBan, numberOfWarnsToBan,
warnInlineKeyboard, warnInlineKeyboard,
} = require('../../config.json'); } = require('../../config.json');
@@ -26,28 +25,24 @@ const removeLinks = async ({ message, chat, reply, state }, next) => {
const { isAdmin, user } = state; const { isAdmin, user } = state;
const { entities, forward_from_chat, text } = message; const { entities, forward_from_chat, text } = message;
const managedGroups = await listGroups(); const managedGroups = await listGroups();
const shouldRemoveGroups = excludedGroups !== '*';
const shouldRemoveChannels = excludedChannels !== '*';
if ( if (
message.chat.type === 'private' || message.chat.type === 'private' ||
isAdmin || isAdmin ||
!shouldRemoveGroups && !excludeLinks) {
!shouldRemoveChannels) {
return next(); return next();
} }
// gather both managed groups and config excluded groups // gather both managed groups and config excluded links
const knownGroups = [ const knownLinks = [
...managedGroups.map(group => group.link ...managedGroups.map(group => group.link
? group.link ? group.link
: ''), : ''),
...excludedGroups ...excludeLinks
]; ];
// collect channels/supergroups usernames in the text // collect channels/supergroups usernames in the text
let isChannelAd = false; let isAd = false;
let isGroupAd = false;
const regexp = /(@\w+)|(((t.me)|(telegram.me))\/\w+(\/[A-Za-z0-9_-]+)?)/g; const regexp = /(@\w+)|(((t.me)|(telegram.me))\/\w+(\/[A-Za-z0-9_-]+)?)/g;
const usernames = const usernames =
text text
@@ -57,14 +52,14 @@ const removeLinks = async ({ message, chat, reply, state }, next) => {
await Promise.all(usernames await Promise.all(usernames
? usernames.map(async username => { ? usernames.map(async username => {
// skip if already detected an ad // skip if already detected an ad
if (isChannelAd || isGroupAd) return; if (isAd) return;
// detect add if it's an invite link // detect add if it's an invite link
if ( if (
username.includes('/joinchat/') && username.includes('/joinchat/') &&
!knownGroups.some(group => group.includes(username)) !knownLinks.some(knownLink => knownLink.includes(username))
) { ) {
isGroupAd = true; isAd = true;
return; return;
} }
@@ -75,24 +70,13 @@ const removeLinks = async ({ message, chat, reply, state }, next) => {
const { type } = await bot.telegram.getChat(username); const { type } = await bot.telegram.getChat(username);
if (!type) return; if (!type) return;
if ( if (
type === 'channel' && !excludeLinks
shouldRemoveChannels && .some(knownLink =>
!excludedChannels knownLink.includes(username.replace('@', '')))
.some(channel =>
channel.includes(username.replace('@', '')))
) { ) {
isChannelAd = true; isAd = true;
return; return;
} }
if (
type === 'supergroup' &&
shouldRemoveGroups &&
!knownGroups
.some(group =>
group.includes(username.replace('@', '')))
) {
isGroupAd = true;
}
} catch (err) { } catch (err) {
logError(err); logError(err);
} }
@@ -103,15 +87,15 @@ const removeLinks = async ({ message, chat, reply, state }, next) => {
// check if is forwarded from channel // check if is forwarded from channel
forward_from_chat && forward_from_chat &&
forward_from_chat.type !== 'private' && forward_from_chat.type !== 'private' &&
shouldRemoveChannels && excludeLinks &&
!excludedChannels.includes(forward_from_chat.username) || !excludeLinks.includes(forward_from_chat.username) ||
// check if text contains link/username of a channel or group // check if text contains link/username of a channel or group
text && text &&
(text.includes('t.me') || (text.includes('t.me') ||
text.includes('telegram.me') || text.includes('telegram.me') ||
entities && entities.some(entity => entity.type === 'mention')) && entities && entities.some(entity => entity.type === 'mention')) &&
(isChannelAd || isGroupAd) isAd
) { ) {
const reason = 'Forwarded or linked channels/groups'; const reason = 'Forwarded or linked channels/groups';
await warn(user, reason); await warn(user, reason);