/** * * @param {string} id Member ID * @param {string} guild Guild ID * @returns {Promise} */ async fetchMessage(id, guild) { const cached = this.cacheManager.getMessageFromCache(guild, id); if (cached) { return new Message(cached); } const result = await Message.findOne({ id, guild }); if (!result) { return new Message({ id, guild }).save(); } this.cacheManager.saveToMessageCache(guild, result); return result; } /** * * @param {string} id The Guild ID * @param {{}|{}[]} data The data given my mongoose & MongoDB * @param {?boolean} find Whether to mark it as find */ saveToMessageCache(id, data, find = false) { const temp = []; if (Array.isArray(data)) temp.concat(data); else temp.push(data); this.debug(`Guild ${id} updated to Message cache for ${temp.length} results.`); const obj = this.messageCache.get(id) || { data: '[]', timeout: null }; obj.find = find; obj.data = JSON.parse(obj.data); if (Array.isArray(data)) obj.data.concat(data); else obj.data.push(data); obj.data = [...new Set(obj.data)]; obj.data = JSON.stringify(obj.data); if (obj.timeout) clearTimeout(obj.timeout); obj.timeout = setTimeout(() => { this.messageCache.delete(id); }, this.timeout); this.messageCache.set(id, obj); } /** * * @param {string} id Guild ID * @param {string} userId User ID * @returns {Object} */ getMessageFromCache(id, userId) { this.debug(`Guild ${id} trying to fetch User ${userId} from Message Cache`); const obj = this.messageCache.get(id) || { data: '[]', timeout: null }; const arr = JSON.parse(obj.data); return arr.find(data => data.id === userId); }