harmony/src/structures/presence.ts

108 lines
2.8 KiB
TypeScript
Raw Normal View History

2020-11-25 11:53:40 +00:00
import { ActivityGame, ClientActivity, StatusType } from '../types/presence.ts'
import { StatusUpdatePayload } from '../types/gateway.ts'
2020-11-02 07:27:14 +00:00
2020-11-25 11:53:40 +00:00
enum ActivityTypes {
2020-11-02 07:27:14 +00:00
PLAYING = 0,
STREAMING = 1,
LISTENING = 2,
WATCHING = 3,
CUSTOM_STATUS = 4,
2020-11-25 11:53:40 +00:00
COMPETING = 5
2020-11-02 07:27:14 +00:00
}
export class ClientPresence {
status: StatusType = 'online'
2020-11-25 11:53:40 +00:00
activity?: ActivityGame | ActivityGame[]
2020-11-02 07:27:14 +00:00
since?: number | null
afk?: boolean
2020-11-25 11:53:40 +00:00
constructor (data?: ClientActivity | StatusUpdatePayload | ActivityGame) {
2020-11-03 09:21:29 +00:00
if (data !== undefined) {
2020-11-03 15:19:20 +00:00
if ((data as ClientActivity).activity !== undefined) {
2020-11-02 07:27:14 +00:00
Object.assign(this, data)
2020-11-25 11:53:40 +00:00
} else if ((data as StatusUpdatePayload).activities !== undefined) {
2020-11-03 15:19:20 +00:00
} else if ((data as ActivityGame).name !== undefined) {
if (this.activity === undefined) {
2020-11-02 07:27:14 +00:00
this.activity = data as ActivityGame
2020-11-03 15:19:20 +00:00
} else if (this.activity instanceof Array) {
2020-11-02 07:27:14 +00:00
this.activity.push(data as ActivityGame)
2020-11-25 11:53:40 +00:00
} else this.activity = [this.activity, data as ActivityGame]
2020-11-02 07:27:14 +00:00
}
}
}
2020-11-25 11:53:40 +00:00
parse (payload: StatusUpdatePayload): ClientPresence {
2020-11-02 07:27:14 +00:00
this.afk = payload.afk
this.activity = payload.activities ?? undefined
this.since = payload.since
this.status = payload.status
2020-11-03 09:21:29 +00:00
return this
2020-11-02 07:27:14 +00:00
}
2020-11-25 11:53:40 +00:00
static parse (payload: StatusUpdatePayload): ClientPresence {
2020-11-02 07:27:14 +00:00
return new ClientPresence().parse(payload)
}
2020-11-25 11:53:40 +00:00
create (): StatusUpdatePayload {
2020-11-02 07:27:14 +00:00
return {
2020-11-03 09:21:29 +00:00
afk: this.afk === undefined ? false : this.afk,
2020-11-02 07:27:14 +00:00
activities: this.createActivity(),
2020-11-03 09:21:29 +00:00
since: this.since === undefined ? null : this.since,
status: this.status === undefined ? 'online' : this.status
2020-11-02 07:27:14 +00:00
}
}
2020-11-25 11:53:40 +00:00
createActivity (): ActivityGame[] | null {
2020-11-03 09:21:29 +00:00
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
2020-11-25 11:53:40 +00:00
const activity =
this.activity === undefined
? null
: this.activity instanceof Array
? this.activity
: [this.activity]
2020-11-03 15:19:20 +00:00
if (activity === null) return activity
2020-11-02 07:27:14 +00:00
else {
activity.map(e => {
2020-11-25 11:53:40 +00:00
if (typeof e.type === 'string') e.type = ActivityTypes[e.type]
2020-11-02 07:27:14 +00:00
return e
})
return activity
}
}
2020-11-25 11:53:40 +00:00
setStatus (status: StatusType): ClientPresence {
2020-11-02 07:27:14 +00:00
this.status = status
return this
}
2020-11-25 11:53:40 +00:00
setActivity (activity: ActivityGame): ClientPresence {
2020-11-02 07:27:14 +00:00
this.activity = activity
return this
}
2020-11-25 11:53:40 +00:00
setActivities (activities: ActivityGame[]): ClientPresence {
2020-11-02 07:27:14 +00:00
this.activity = activities
return this
}
2020-11-25 11:53:40 +00:00
setAFK (afk: boolean): ClientPresence {
2020-11-02 07:27:14 +00:00
this.afk = afk
2020-11-03 09:21:29 +00:00
return this
2020-11-02 07:27:14 +00:00
}
2020-11-25 11:53:40 +00:00
removeAFK (): ClientPresence {
2020-11-02 07:27:14 +00:00
this.afk = false
return this
}
2020-11-25 11:53:40 +00:00
toggleAFK (): ClientPresence {
2020-11-03 09:21:29 +00:00
this.afk = this.afk === undefined ? true : !this.afk
2020-11-02 07:27:14 +00:00
return this
}
2020-11-25 11:53:40 +00:00
setSince (since?: number): ClientPresence {
2020-11-02 07:27:14 +00:00
this.since = since
return this
}
2020-11-25 11:53:40 +00:00
}