From 5c1ffd5fd5237aa55f0b5774fe0303f8ef56c63e Mon Sep 17 00:00:00 2001 From: adithyagenie Date: Sun, 16 Jun 2024 00:33:06 +0530 Subject: [PATCH] Add throttler for bot Signed-off-by: adithyagenie --- package.json | 3 ++- pnpm-lock.yaml | 19 +++++++++++++++++++ src/bot/bot.ts | 3 +++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 49c8f5a..47b0d5d 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "fastify": "^4.27.0", "grammy": "^1.24.1", "leetcode-query": "^1.2.3", - "node-schedule": "^2.1.1" + "node-schedule": "^2.1.1", + "@grammyjs/transformer-throttler": "^1.2.1" }, "devDependencies": { "@grammyjs/types": "^3.8.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 15c49d4..2603480 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,9 @@ importers: '@grammyjs/parse-mode': specifier: ^1.10.0 version: 1.10.0(grammy@1.24.1) + '@grammyjs/transformer-throttler': + specifier: ^1.2.1 + version: 1.2.1(grammy@1.24.1) codeforces-api-ts: specifier: ^3.0.1 version: 3.0.1 @@ -75,6 +78,12 @@ packages: peerDependencies: grammy: ^1.20.1 + '@grammyjs/transformer-throttler@1.2.1': + resolution: {integrity: sha512-CpWB0F3rJdUiKsq7826QhQsxbZi4wqfz1ccKX+fr+AOC+o8K7ZvS+wqX0suSu1QCsyUq2MDpNiKhyL2ZOJUS4w==} + engines: {node: ^12.20.0 || >=14.13.1} + peerDependencies: + grammy: ^1.0.0 + '@grammyjs/types@3.8.0': resolution: {integrity: sha512-5r2rUJ/s939iQEt6j2gyl1owbWwKAYAg2gQH7FfWIP1swL2assZ97vRUl8cBU3UEp7es9c/VioWeQjKbV6g4eg==} @@ -126,6 +135,9 @@ packages: base64-js@1.5.1: resolution: {integrity: sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==} + bottleneck@2.19.5: + resolution: {integrity: sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw==} + buffer@6.0.3: resolution: {integrity: sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==} @@ -473,6 +485,11 @@ snapshots: dependencies: grammy: 1.24.1 + '@grammyjs/transformer-throttler@1.2.1(grammy@1.24.1)': + dependencies: + bottleneck: 2.19.5 + grammy: 1.24.1 + '@grammyjs/types@3.8.0': {} '@types/node-fetch@2.6.11': @@ -520,6 +537,8 @@ snapshots: base64-js@1.5.1: {} + bottleneck@2.19.5: {} + buffer@6.0.3: dependencies: base64-js: 1.5.1 diff --git a/src/bot/bot.ts b/src/bot/bot.ts index 2a89f04..b745ac0 100644 --- a/src/bot/bot.ts +++ b/src/bot/bot.ts @@ -1,12 +1,15 @@ import { Bot, Context } from "grammy"; import { parseMode, ParseModeFlavor } from "@grammyjs/parse-mode"; import { helpCommand, messageSink, setCommands, startCommand } from "./commands/miscCommands"; +import { apiThrottler } from "@grammyjs/transformer-throttler"; export const bot = new Bot>(`${process.env.BOT_TOKEN}`); export type myBot = Bot>; export async function botInit() { + const throttler = apiThrottler(); + bot.api.config.use(throttler); bot.api.config.use(parseMode("HTML")); setCommands(bot); bot.hears(/^\/start/, async(ctx) => await startCommand(ctx));