import { GuildMember , Snowflake } from "discord.js" ; import { Document , model , Schema } from "mongoose" ; import DBWrapper from "./db-wrapper" ; import { soundEffectInterface } from "./SoundEffect" ; export interface memberSettings { joinSoundEffect : Schema . Types . ObjectId ; leaveSoundEffect : Schema . Types . ObjectId ; } export interface voiceActivity { joinTime : Date ; leaveTime : Date ; voiceDuration : number ; streamStartTime : Date ; streamEndTime : Date ; streamDuration : number ; } export interface memberInterface extends Document { userID : Snowflake ; guildID : Snowflake ; username : string ; settings : memberSettings ; ban : { endTime : Date ; } ; mute : { endTime : Date ; } ; voiceActivity : voiceActivity ; createdAt : Date ; updatedAt : Date ; } const memberSchema = new Schema < memberInterface > ( { userID : String , guildID : String , username : String , settings : { joinSoundEffect : { type : Schema . Types . ObjectId , ref : "SoundEffect" , default : null , } , leaveSoundEffect : { type : Schema . Types . ObjectId , ref : "SoundEffect" , default : null , } , } , ban : { endTime : { type : Date , default : null } , } , mute : { endTime : { type : Date , default : null } , } , voiceActivity : { joinTime : { type : Date , default : null } , leaveTime : { type : Date , default : null } , voiceDuration : { type : Number , default : 0 } , streamStartTime : { type : Date , default : null } , streamEndTime : { type : Date , default : null } , streamDuration : { type : Number , default : 0 } , } , } , { timestamps : true } ) ; export const memberModel = model < memberInterface > ( "Member" , memberSchema ) ; export class DBMember extends DBWrapper < GuildMember , memberInterface > { protected async getOrCreate ( member ) { const savedMember = await memberModel . findOne ( { userID : member . user . id , guildID : member . guild . id , } ) ; return savedMember ?? this . create ( member ) ; } async create ( member ) { const newMember = await new memberModel ( { userID : member . user . id , guildID : member . guild . id , username : member . user . username , } ) ; return this . save ( newMember ) ; } async update ( member , settings ) { const savedMember = await this . get ( member ) ; const memberSettings = savedMember . settings ; for ( const key in settings ) { if ( memberSettings [ key ] !== settings [ key ] ) memberSettings [ key ] = settings [ key ] ; else continue ; } return memberModel . findOneAndUpdate ( { userID : member . user . id , guildID : member . guild . id , } , { settings : memberSettings , } , { new : true } ) ; } async updateJoinEffect ( member , soundEffect ) { const savedMember = await this . get ( member ) ; savedMember . settings . joinSoundEffect = soundEffect ?. _id ?? null ; let memberSoundEffects = this . client . databaseCache . memberSoundEffects . get ( `${ member . guild . id }${ member . user . id }` ) ; if ( ! memberSoundEffects ) { memberSoundEffects = { joinSoundEffect : soundEffect , leaveSoundEffect : null , } ; } else { memberSoundEffects . joinSoundEffect = soundEffect ; } await this . save ( savedMember ) . then ( ( ) => { this . client . databaseCache . memberSoundEffects . set ( `${ member . guild . id }${ member . user . id }` , memberSoundEffects ) ; } ) ; } async updateLeaveEffect ( member , soundEffect ) { const savedMember = await this . get ( member ) ; savedMember . settings . leaveSoundEffect = soundEffect ?. _id || null ; let memberSoundEffects = this . client . databaseCache . memberSoundEffects . get ( `${ member . guild . id }${ member . user . id }` ) ; if ( ! memberSoundEffects ) { memberSoundEffects = { joinSoundEffect : null , leaveSoundEffect : soundEffect , } ; } else { memberSoundEffects . leaveSoundEffect = soundEffect ; } await this . save ( savedMember ) . then ( ( ) => { this . client . databaseCache . memberSoundEffects . set ( `${ member . guild . id }${ member . user . id }` , memberSoundEffects ) ; } ) ; } async mute ( member , endTime ) { return memberModel . findOneAndUpdate ( { userID : member . user . id , guildID : member . guild . id } , { "mute.endTime" : endTime } , { upsert : true } ) ; } async unmute ( member ) { return memberModel . findOneAndUpdate ( { userID : member . user . id , guildID : member . guild . id } , { "mute.endTime" : null } ) ; } async ban ( member , endTime ) { return memberModel . findOneAndUpdate ( { userID : member . user . id , guildID : member . guild . id } , { "ban.endTime" : endTime } , { upsert : true } ) ; } async unban ( userID , guildID ) { return memberModel . findOneAndUpdate ( { userID : userID , guildID : guildID } , { "ban.endTime" : null } ) ; } }