@riddea/telegraf-rate-limiter

Simple message rate limiter for telegraf.

Usage no npm install needed!

<script type="module">
  import riddeaTelegrafRateLimiter from 'https://cdn.skypack.dev/@riddea/telegraf-rate-limiter';
</script>

README

telegraf-rate-limiter

Description

NPM Version Telegraf Version Downloads

Multilingual utilities for telegraf.

Installation

npm i @riddea/telegraf-rate-limiter

Use

import { RateLimiter } from "@riddea/telegraf-rate-limiter";
import { Telegraf } from "telegraf";
// Import RateLimiter package
import { RateLimiter } from "@riddea/telegraf-rate-limiter";

// Put your bot token from @BotFather
const bot = new Telegraf("");
// Set 1 message per 2 second
const rateLimiter = new RateLimiter(1, 2000);

bot.on("message", async (ctx) => {
  // Take user ID and check member have rate limit or no
  const limited = rateLimiter.take(ctx.from.id);

  // Send message if member has limit
  if (limited) return await ctx.reply("Hey! Wait 2 second before send new message!");

  // Doing other if member don't have limits
  await ctx.reply("Hello!");
});

bot.launch();

// Enable graceful stop
process.once("SIGINT", () => bot.stop("SIGINT"));
process.once("SIGTERM", () => bot.stop("SIGTERM"));

Example

Example

Contributing

This project opened for contribution! You can create a new Pull request with your code changes.

LICENSE

This code has MIT license. See the LICENSE file for getting more information. Ogirinal code take from discord.js-rate-limiter