2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-22 09:57:50 +00:00

Add middlewares/antiflood

This commit is contained in:
Wojciech Pawlik 2020-06-06 10:51:46 +02:00
parent 433854e154
commit a11e35d3a7
No known key found for this signature in database
GPG Key ID: 7D763FCB7B36ADB5
5 changed files with 31 additions and 3 deletions

View File

@ -8,9 +8,7 @@ const { config } = require('../utils/config');
/** @type { import('telegraf/typings').Telegraf<ExtendedContext> } */
const bot = new Telegraf(config.token);
if (process.env.NODE_ENV === 'development') {
bot.polling.offset = -1;
}
bot.polling.offset = -1;
module.exports = bot;

View File

@ -0,0 +1,19 @@
import ms = require("millisecond");
import rateLimit = require("telegraf-ratelimit");
import type { ExtendedContext } from "../../typings/context";
function keyGenerator(ctx: ExtendedContext) {
if (ctx.updateType !== "message") return null;
if (ctx.from?.status !== "member") return null;
if (!ctx.chat?.type.endsWith("group")) return null;
return `${ctx.chat.id}:${ctx.from.id}`;
}
const permissions = { can_send_messages: false };
async function onLimitExceeded(ctx: ExtendedContext) {
const until_date = (Date.now() + ms("2m")) / 1000;
await ctx.restrictChatMember(ctx.from!.id, { permissions, until_date });
}
export = rateLimit({ keyGenerator, onLimitExceeded });

View File

@ -14,6 +14,7 @@ const { deleteJoinsAfter = '2 minutes' } = require('../../utils/config').config;
const addedToGroupHandler = require('./addedToGroup');
const antibotHandler = require('./antibot');
const antifloodHandler = require('./antiflood');
const checkLinksHandler = require('./checkLinks');
const commandButtons = require('./commandButtons');
const kickBannedHandler = require('./kickBanned');
@ -45,6 +46,7 @@ composer.on(
deleteAfter(deleteJoinsAfter),
presenceLogHandler,
);
composer.use(antifloodHandler);
composer.action(
/^\/del -chat_id=(-\d+) -msg_id=(\d+) Report handled/,
reportHandled,

8
package-lock.json generated
View File

@ -1450,6 +1450,14 @@
}
}
},
"telegraf-ratelimit": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/telegraf-ratelimit/-/telegraf-ratelimit-2.0.0.tgz",
"integrity": "sha512-OUVJRz+pVDpkOfwvFuv4x5YxcPIl6puPN3HRdicN3NDnT+jQeMzKDwVAkvloUtyH1Ruo1wH4nfB5tZbtJAJ4pQ==",
"requires": {
"debug": "^3.1.0"
}
},
"telegram-typings": {
"version": "3.6.1",
"resolved": "https://registry.npmjs.org/telegram-typings/-/telegram-typings-3.6.1.tgz",

View File

@ -40,6 +40,7 @@
"spamwatch": "^0.2.0",
"string-replace-async": "^1.2.1",
"telegraf": "^3.38.0",
"telegraf-ratelimit": "^2.0.0",
"ts-node": "^8.9.1",
"typescript": "^3.8.3",
"xregexp": "^4.2.0"