db module
In-memory key-value store for quick state. Use for counters, streaks, flags, and lightweight caches.
Scope is per-bot runtime; no persistence beyond restarts.
Overview
The db module is a simple hash map. Keys and values should stay primitive or plain objects/arrays. Avoid large payloads; use embeds or HTTP for richer content.
No TTL or transactions. Reads and writes are synchronous and cheap.
API
db.get(key)→ value orundefined.db.set(key, value)→ stores value.db.delete(key)→ removes entry.
Patterns
- Prefix keys with entity type:
user:${ctx.user.id}:streak. - Keep values small (numbers, strings, short arrays/objects).
- Write after read to avoid racey increments: read → mutate → set.
Examples
javascript
const db = use("db");
function main() { const key = "user:" + ctx.user.id + ":xp"; const xp = (db.get(key) || 0) + 15; db.set(key, xp); ctx.reply("XP: " + xp);}javascript
const db = use("db");
function main() { const key = "guild:" + ctx.guild.id + ":feature:welcome"; const enabled = db.get(key); ctx.reply(enabled ? "Welcome messages on" : "Welcome messages off");}