harmony/src/structures/presence.ts

148 lines
3.6 KiB
TypeScript
Raw Normal View History

import {
ActivityGame,
ActivityPayload,
ClientActivity,
ClientStatus,
2020-12-02 12:29:52 +00:00
StatusType
} from '../types/presence.ts'
import { PresenceUpdatePayload, StatusUpdatePayload } from '../types/gateway.ts'
import { Base } from './base.ts'
import { Guild } from './guild.ts'
import { User } from './user.ts'
import { Client } from '../models/client.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-12-02 12:29:52 +00:00
COMPETING = 5
}
export class Presence extends Base {
user: User
guild: Guild
status: StatusType
// TODO: Maybe a new structure for this?
activities: ActivityPayload[]
clientStatus: ClientStatus
constructor(
client: Client,
data: PresenceUpdatePayload,
user: User,
guild: Guild
) {
super(client, data)
this.user = user
this.guild = guild
this.status = data.status
this.activities = data.activities
this.clientStatus = data.client_status
}
fromPayload(data: PresenceUpdatePayload): Presence {
this.status = data.status
this.activities = data.activities
this.clientStatus = data.client_status
return this
}
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
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
}
}
}
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
}
static parse(payload: StatusUpdatePayload): ClientPresence {
2020-11-02 07:27:14 +00:00
return new ClientPresence().parse(payload)
}
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,
2020-12-02 12:29:52 +00:00
status: this.status === undefined ? 'online' : this.status
2020-11-02 07:27:14 +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
}
}
setStatus(status: StatusType): ClientPresence {
2020-11-02 07:27:14 +00:00
this.status = status
return this
}
setActivity(activity: ActivityGame): ClientPresence {
2020-11-02 07:27:14 +00:00
this.activity = activity
return this
}
setActivities(activities: ActivityGame[]): ClientPresence {
2020-11-02 07:27:14 +00:00
this.activity = activities
return this
}
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
}
removeAFK(): ClientPresence {
2020-11-02 07:27:14 +00:00
this.afk = false
return this
}
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
}
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
}