diff --git a/src/server/components/UserDatabase.js b/src/server/components/UserDatabase.js index 48fcdb1..79cf194 100644 --- a/src/server/components/UserDatabase.js +++ b/src/server/components/UserDatabase.js @@ -1,4 +1,6 @@ +const { Collection } = require("@discordjs/collection"); const { ObjectId } = require("mongodb"); + const { AbstractUserDatabase } = require("../interfaces/"); const { User } = require("../structures"); @@ -10,7 +12,7 @@ class UserDatabase extends AbstractUserDatabase { this.db = db; this.collectionName = userColl; this.logger = server.createLogger(this); - this.cache = new Map(); + this.cache = new Collection(); this.collection = null; } @@ -62,10 +64,13 @@ class UserDatabase extends AbstractUserDatabase { * @memberof UserDatabase */ async userFromDiscord (profile) { - const data = await this.collection.findOne({ 'discord.id': profile.id }); - let user = this.cache.get(data._id); + + let user = this.cache.find((u) => u.externalProfiles.discord?.id === profile.id); + if (user) return Promise.resolve(user); - if (!data) { + const data = await this.collection.findOne({ 'discord.id': profile.id }); + if (data) user = this._createUser(data); + else { this.logger.info(`Creating new user from Discord profile: ${profile.username} (${profile.id})`); user = this._createUser({ type: 'user', name: profile.username }); user.addExternalProfile('discord', profile);