rashi-discord-bot-lib
Version:
🚀 Powerful Discord bot framework with built-in database, event handling, and utilities
42 lines • 1.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MongoCooldownManager = void 0;
const mongo_1 = require("../utils/mongo"); // lub '../database/mongo' w zależności od struktury
class MongoCooldownManager {
collectionName;
collection;
constructor(collectionName = 'cooldowns') {
this.collectionName = collectionName;
}
async connect() {
const db = (0, mongo_1.getDb)();
this.collection = db.collection(this.collectionName);
await this.collection.createIndex({ expiresAt: 1 }, { expireAfterSeconds: 0 });
}
key(userId, command) {
return `${userId}:${command}`;
}
async getRemaining(userId, command) {
const doc = await this.collection.findOne({ key: this.key(userId, command) });
if (!doc?.expiresAt)
return 0;
const remaining = doc.expiresAt.getTime() - Date.now();
return remaining > 0 ? Math.ceil(remaining / 1000) : 0;
}
async set(userId, command, seconds) {
const expiresAt = new Date(Date.now() + seconds * 1000);
await this.collection.updateOne({ key: this.key(userId, command) }, { $set: { expiresAt } }, { upsert: true });
}
async checkAndSet(userId, command, seconds) {
const remaining = await this.getRemaining(userId, command);
if (remaining > 0)
return remaining;
await this.set(userId, command, seconds);
return 0;
}
async clear(userId, command) {
await this.collection.deleteOne({ key: this.key(userId, command) });
}
}
exports.MongoCooldownManager = MongoCooldownManager;
//# sourceMappingURL=MongoCooldownManager.js.map