db module
Persistent key-value storage for script state. Use for counters, streaks, flags, and lightweight caches.
Data is retained between restarts unless removed by command, reset, or cleanup policy.
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)returns 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 then mutate then set.
Examples
javascript
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
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");}