import { Snowflake , User } from "discord.js" ; import { Document , model , Schema } from "mongoose" ; import DBWrapper from "./db-wrapper" ; export interface userSettings { timezone : string ; } export interface userInterface extends Document { userID : Snowflake ; username : string ; settings : userSettings ; createdAt : Date ; updatedAt : Date ; } const userSchema = new Schema < userInterface > ( { userID : { type : String , unique : true , } , username : String , settings : { timezone : { type : String , default : null } , } , } , { timestamps : true } ) ; export const userModel = model < userInterface > ( "User" , userSchema ) ; export class DBUser extends DBWrapper < User , userInterface > { protected async getOrCreate ( user ) { const savedUser = await userModel . findOne ( { userID : user . id } ) ; return savedUser ?? this . create ( user ) ; } async create ( user ) { const newUser = await new userModel ( { userID : user . id , username : user . username , settings : { timezone : null , } , } ) ; return this . save ( newUser ) ; } async update ( user , settings ) { const savedUser = await this . get ( user ) ; const userSettings = savedUser . settings ; for ( const key in settings ) { if ( userSettings [ key ] !== settings [ key ] ) userSettings [ key ] = settings [ key ] ; else continue ; } return userModel . findOneAndUpdate ( { userID : user . id } , { settings : userSettings , } , { new : true } ) ; } }