Skip to content

Instantly share code, notes, and snippets.

@poeti8
Created April 25, 2021 10:56
Show Gist options
  • Save poeti8/966ccef35d61ad2735dc0120ce3e8760 to your computer and use it in GitHub Desktop.
Save poeti8/966ccef35d61ad2735dc0120ce3e8760 to your computer and use it in GitHub Desktop.
the-guard-anti-arabic-plugin
'use strict';
const C = require('telegraf/composer');
// Utils
const o = (f, g) => x => f(g(x));
const both = (a, b) =>
(...args) =>
a(...args) && b(...args);
// Telegraf utils
const text = ctx => (
(ctx.message && (ctx.message.entities ||
ctx.message.caption_entities)) ||
[]
).reduce(
(acc, entity) =>
entity.type === 'url'
? acc.slice(0, entity.offset) +
acc.slice(entity.offset + entity.length)
: acc,
(ctx.message && ctx.message.text) ||
(ctx.message && ctx.message.caption) ||
'');
const isNonAdmin = ctx =>
!ctx.state.isMaster && ctx.from.status !== 'admin';
// Text utils
const isSandland = c =>
c >= '\u0600' && c <= '\u06FF';
const sandlandAmount = s =>
s.split('').filter(isSandland).length / s.length;
const banAmount = len =>
(amount => amount <= 0 ? 1 : amount)
(4 / (len - 2) + 0.2);
// Main hook
const shouldBan = text =>
sandlandAmount(text) >= banAmount(text.length);
// Woop woop
module.exports = C.optional(
both(
o(shouldBan, text),
isNonAdmin),
ctx =>
Promise.all([
ctx.deleteMessage(),
ctx.ban({
admin: ctx.botInfo,
reason: 'Persian/Arabic text',
userToBan: ctx.from
})
]));
@Mrs-Feathers
Copy link

This plugin seems to be outdated. the error i'm getting seems to be:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './composer' is not defined by "exports" in /root/the-guard-bot/node_modules/telegraf/package.json
at new NodeError (node:internal/errors:387:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:439:9)
at packageExportsResolve (node:internal/modules/esm/resolve:718:3)
at resolveExports (node:internal/modules/cjs/loader:493:36)
at Function.Module._findPath (node:internal/modules/cjs/loader:533:31)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:942:27)
at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/root/the-guard-bot/node_modules/@cspotcode/source-map-support/source-map-support.js:811:30)
at Function.Module._load (node:internal/modules/cjs/loader:804:27)
at Module.require (node:internal/modules/cjs/loader:1022:19)
at require (node:internal/modules/cjs/helpers:102:18) {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'

@MKRhere
Copy link

MKRhere commented Aug 18, 2022

Fix:

const { Composer } = require("telegraf");

Telegraf no longer allows importing from internal paths that aren't explicitly exported.

@iTzVoko
Copy link

iTzVoko commented Aug 2, 2023

"Anti Arabic"? Racism?

@poeti8
Copy link
Author

poeti8 commented Aug 3, 2023

Yes

@Mrs-Feathers
Copy link

this is not intended as racism. it is used to stop bots, of which we get daily and use arabic characters posting scam links and such. we welcome everyone, but wish to keep bots out. our chat is english-only so everyone (my self being german) must keep to english as well and so its not limiting anyone from joining. only bots from spamming.

@iTzVoko
Copy link

iTzVoko commented Aug 7, 2023

this is not intended as racism. it is used to stop bots, of which we get daily and use arabic characters posting scam links and such.

Okay. But what about making an "Anti Hebrew", for example? Why not making anti-Chinese/Japanese letters also?
I don't want to make assumptions, but isn't that weird? i.e. Just the Arabic language is denied?

@MKRhere
Copy link

MKRhere commented Aug 7, 2023

It just happens that Telegram has a rampant problem with Persian spambots. Modern Persian uses Arabic alphabet, so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment