harmony/src/structures/presence.ts

123 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-11-02 07:27:14 +00:00
export type ActivityType = 'PLAYING' | 'STREAMING' | 'LISTENING' | 'WATCHING' | 'CUSTOM_STATUS' | 'COMPETING';
export type StatusType = 'online' | 'invisible' | 'offline' | 'idle' | 'dnd';
export enum ActivityTypes {
PLAYING = 0,
STREAMING = 1,
LISTENING = 2,
WATCHING = 3,
CUSTOM_STATUS = 4,
COMPETING = 5,
}
export interface ActivityGame {
name: string;
type: 0 | 1 | 2 | 3 | 4 | 5 | ActivityType;
url?: string;
}
export interface ClientActivity {
status?: StatusType
activity?: ActivityGame | ActivityGame[]
since?: number | null
afk?: boolean
}
export interface ClientActivityPayload {
status: StatusType
activities: ActivityGame[] | null
since: number | null
afk: boolean
}
export class ClientPresence {
status: StatusType = 'online'
activity?: ActivityGame | ActivityGame[]
since?: number | null
afk?: boolean
constructor(data?: ClientActivity | ClientActivityPayload | 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-03 15:19:20 +00:00
} else if ((data as ClientActivityPayload).activities !== undefined) {
2020-11-02 07:27:14 +00:00
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)
} else this.activity = [ this.activity, data as ActivityGame ]
}
}
}
2020-11-03 09:21:29 +00:00
parse(payload: ClientActivityPayload): 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-03 09:21:29 +00:00
static parse(payload: ClientActivityPayload): ClientPresence {
2020-11-02 07:27:14 +00:00
return new ClientPresence().parse(payload)
}
create(): ClientActivityPayload {
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-03 09:21:29 +00:00
createActivity(): ActivityGame[] | null {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
const activity = this.activity === undefined ? null : (this.activity instanceof Array ? this.activity : [this.activity]) || null
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-03 15:19:20 +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-03 09:21:29 +00:00
setStatus(status: StatusType): ClientPresence {
2020-11-02 07:27:14 +00:00
this.status = status
return this
}
2020-11-03 09:21:29 +00:00
setActivity(activity: ActivityGame): ClientPresence {
2020-11-02 07:27:14 +00:00
this.activity = activity
return this
}
2020-11-03 09:21:29 +00:00
setActivities(activities: ActivityGame[]): ClientPresence {
2020-11-02 07:27:14 +00:00
this.activity = activities
return this
}
2020-11-03 09:21:29 +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-03 09:21:29 +00:00
removeAFK(): ClientPresence {
2020-11-02 07:27:14 +00:00
this.afk = false
return this
}
2020-11-03 09:21:29 +00:00
toggleAFK(): ClientPresence {
this.afk = this.afk === undefined ? true : !this.afk
2020-11-02 07:27:14 +00:00
return this
}
2020-11-03 09:21:29 +00:00
setSince(since?: number): ClientPresence {
2020-11-02 07:27:14 +00:00
this.since = since
return this
}
}