2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-09-05 16:45:18 +00:00
Files
the-guard-bot/handlers/middlewares/removeChannelForwards.js

65 lines
1.6 KiB
JavaScript
Raw Normal View History

'use strict';
const R = require('ramda');
2021-05-07 22:46:11 +02:00
const { Telegraf: { optional, passThru } } = require('telegraf');
2020-06-29 15:07:12 +02:00
const { permit } = require('../../stores/user');
const { html, lrm } = require('../../utils/html');
2020-03-09 23:27:19 +01:00
const { excludeLinks = [] } = require('../../utils/config').config;
if (excludeLinks === false || excludeLinks === '*') {
module.exports = passThru();
return;
}
const isChannelForward = R.pathEq(
[ 'message', 'forward_from_chat', 'type' ],
2020-05-13 22:59:23 +02:00
'channel',
);
const fromAdmin = R.pathEq([ 'from', 'status' ], 'admin');
const inGroup = ctx => ctx.chat?.type.endsWith('group');
2019-01-24 19:53:10 +01:00
const capturingGroups = R.tail;
const toUsername = R.compose(
capturingGroups,
2020-05-13 22:59:23 +02:00
R.match(/^(?:@|(?:https?:\/\/)?(?:t\.me|telegram\.(?:me|dog))\/)(\w+)/i),
);
const customWhitelist = R.pipe(
R.chain(toUsername),
R.map(R.toLower),
R.constructN(1, Set),
)(excludeLinks);
const isWhitelisted = username => customWhitelist.has(username.toLowerCase());
const fromWhitelisted = ctx =>
isWhitelisted(ctx.message.forward_from_chat.username || '');
const pred = R.allPass([
2019-01-24 19:53:10 +01:00
inGroup,
isChannelForward,
R.complement(fromAdmin),
R.complement(fromWhitelisted),
]);
2020-03-10 22:10:48 +01:00
/** @param { import('../../typings/context').ExtendedContext } ctx */
2020-06-29 15:07:12 +02:00
const handler = async (ctx, next) => {
if (await permit.revoke(ctx.from)) {
await ctx.replyWithHTML(html`${lrm}${ctx.from.first_name} used 🎟 permit!`);
return next();
}
2020-05-13 22:59:23 +02:00
ctx.deleteMessage().catch(() => null);
return ctx.warn({
admin: ctx.botInfo,
reason: 'Channel forward',
userToWarn: ctx.from,
mode: 'auto',
});
};
module.exports = optional(pred, handler);