This commit is contained in:
Erik 2022-11-09 17:05:38 +02:00
parent c7a06f2ca6
commit 3ff0483a84
Signed by: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB

View File

@ -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);