Skip to content

CIRCE / DOCUMENTATION

Cooldown

Rate-limit a handler per user, channel or guild, with memory or SQLite storage.

Documentation menu
On this page
cooldown({
  durationMs: number,
  scope: "user" | "channel" | "guild",
  store?: CooldownStore,   // default MemoryCooldownStore
}): Guard;
import { cooldown } from "@sxnnyside/circe";

guards: [cooldown({ durationMs: 5_000, scope: "user" })];

Stores

Store Import Persists
MemoryCooldownStore @sxnnyside/circe No. Resets on restart. Supports using.
SqliteCooldownStore @sxnnyside/circe/sqlite Yes. Bun, Node.js 22.18+, Deno 2.2+. WAL on disk.
import { SqliteCooldownStore } from "@sxnnyside/circe/sqlite";

const store = new SqliteCooldownStore({ path: "./data/cooldowns.sqlite" });
cooldown({ durationMs: 5_000, scope: "user", store });

SqliteCooldownStore({ path?, sweepIntervalMs?, driver? }). BunSqliteCooldownStore in @sxnnyside/circe/bun-sqlite is an older alias.

Custom store

interface CooldownStore {
  check(key: string): number | Promise<number>;           // ms remaining, 0 if free
  hit(key: string, durationMs: number): void | Promise<void>;
  claim?(key: string, durationMs: number): number | Promise<number>; // atomic: 0 = taken, else ms left
}

With claim, the guard takes the cooldown atomically, safe across processes sharing the store (a Redis store, the SQLite file).