/
presence.ts
174 lines (156 loc) · 4.35 KB
/
presence.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import {
ActivityGame,
ActivityPayload,
ClientActivity,
ClientStatus,
StatusType
} from '../types/presence.ts'
import { PresenceUpdatePayload, StatusUpdatePayload } from '../types/gateway.ts'
import { Base } from './base.ts'
import type { Guild } from './guild.ts'
import type { User } from './user.ts'
import type { Client } from '../client/mod.ts'
export enum ActivityTypes {
PLAYING = 0,
STREAMING = 1,
LISTENING = 2,
WATCHING = 3,
CUSTOM_STATUS = 4,
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.fromPayload(data)
}
fromPayload(data: PresenceUpdatePayload): Presence {
this.status = data.status
this.activities = data.activities
this.clientStatus = data.client_status
return this
}
}
interface StatusPayload extends StatusUpdatePayload {
client_status?: ClientStatus
}
export class ClientPresence {
status: StatusType = 'online'
activity?: ActivityGame | ActivityGame[]
since?: number | null
afk?: boolean
clientStatus?: ClientStatus
constructor(data?: ClientActivity | StatusPayload | ActivityGame) {
if (data !== undefined) {
if (['name', 'type', 'url'].some((k) => k in data)) {
// ActivityGame
if (this.activity === undefined) {
this.activity = data as ActivityGame
} else if (this.activity instanceof Array) {
this.activity.push(data as ActivityGame)
} else this.activity = [this.activity, data as ActivityGame]
} else if (['client_status', 'activities'].some((k) => k in data)) {
// StatusPayload
this.parse(data as StatusPayload)
} else if (
['since', 'activity', 'status', 'afk'].some((k) => k in data)
) {
// ClientActivity
Object.assign(this, data)
}
}
}
/** Parses from Payload */
parse(payload: StatusPayload): ClientPresence {
this.afk = payload.afk
this.activity = payload.activities ?? undefined
this.since = payload.since
this.status = payload.status
// this.clientStatus = payload.client_status
return this
}
/** Parses from Payload and creates new ClientPresence */
static parse(payload: StatusUpdatePayload): ClientPresence {
return new ClientPresence().parse(payload)
}
/** Creates Presence Payload */
create(): StatusPayload {
return {
afk: this.afk === undefined ? false : this.afk,
activities: this.createActivity() ?? [],
since:
this.since === undefined
? this.status === 'idle'
? Date.now()
: null
: this.since,
status: this.status === undefined ? 'online' : this.status
// client_status: this.clientStatus
}
}
/** Creates Activity Payload */
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]
if (activity === null) return activity
else {
activity.map((e) => {
if (typeof e.type === 'string') e.type = ActivityTypes[e.type]
return e
})
return activity
}
}
/** Set Status of Presence */
setStatus(status: StatusType): ClientPresence {
this.status = status
return this
}
/** Set Activity for Presence */
setActivity(activity: ActivityGame): ClientPresence {
this.activity = activity
return this
}
/** Set Activities for Presence */
setActivities(activities: ActivityGame[]): ClientPresence {
this.activity = activities
return this
}
/** Set AFK value */
setAFK(afk: boolean): ClientPresence {
this.afk = afk
return this
}
/** Remove AFK (set false) */
removeAFK(): ClientPresence {
this.afk = false
return this
}
/** Toggle AFK (boolean) value */
toggleAFK(): ClientPresence {
this.afk = this.afk === undefined ? true : !this.afk
return this
}
/** Set Since property of Activity */
setSince(since?: number): ClientPresence {
this.since = since
return this
}
}