forked from stoatchat/javascript-client-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.ts
More file actions
467 lines (413 loc) · 10.7 KB
/
Copy pathMessage.ts
File metadata and controls
467 lines (413 loc) · 10.7 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import type { ReactiveMap } from "@solid-primitives/map";
import type { ReactiveSet } from "@solid-primitives/set";
import type {
Message as APIMessage,
MessageWebhook as APIMessageWebhook,
DataEditMessage,
DataMessageSend,
Masquerade,
} from "stoat-api";
import { decodeTime } from "ulid";
import type { Client } from "../Client.js";
import type { MessageCollection } from "../collections/MessageCollection.js";
import { MessageFlags } from "../hydration/message.js";
import type { Channel } from "./Channel.js";
import { File } from "./File.js";
import type { MessageEmbed } from "./MessageEmbed.js";
import type { Server } from "./Server.js";
import type { ServerMember } from "./ServerMember.js";
import { ServerRole } from "./ServerRole.js";
import type { SystemMessage } from "./SystemMessage.js";
import type { User } from "./User.js";
/**
* Message Class
*/
export class Message {
readonly #collection: MessageCollection;
readonly id: string;
/**
* Construct Message
* @param collection Collection
* @param id Message Id
*/
constructor(collection: MessageCollection, id: string) {
this.#collection = collection;
this.id = id;
}
/**
* Whether this object exists
*/
get $exists(): boolean {
return !!this.#collection.getUnderlyingObject(this.id).id;
}
/**
* Time when this message was posted
*/
get createdAt(): Date {
return new Date(decodeTime(this.id));
}
/**
* Absolute pathname to this message in the client
*/
get path(): string {
return `${this.channel?.path}/${this.id}`;
}
/**
* URL to this message
*/
get url(): string | undefined {
return this.#collection.client.configuration?.app + this.path;
}
/**
* Nonce value
*/
get nonce(): string | undefined {
return this.#collection.getUnderlyingObject(this.id).nonce;
}
/**
* Id of channel this message was sent in
*/
get channelId(): string {
return this.#collection.getUnderlyingObject(this.id).channelId;
}
/**
* Channel this message was sent in
*/
get channel(): Channel | undefined {
return this.#collection.client.channels.get(
this.#collection.getUnderlyingObject(this.id).channelId,
);
}
/**
* Server this message was sent in
*/
get server(): Server | undefined {
return this.channel?.server;
}
/**
* Member this message was sent by
*/
get member(): ServerMember | undefined {
return this.#collection.client.serverMembers.getByKey({
server: this.channel?.serverId as string,
user: this.authorId!,
});
}
/**
* Id of user or webhook this message was sent by
*/
get authorId(): string | undefined {
return this.#collection.getUnderlyingObject(this.id).authorId;
}
/**
* User this message was sent by
*/
get author(): User | undefined {
return this.#collection.client.users.get(
this.#collection.getUnderlyingObject(this.id).authorId!,
);
}
/**
* Webhook information for this message
*/
get webhook(): MessageWebhook | undefined {
return this.#collection.getUnderlyingObject(this.id).webhook!;
}
/**
* Content
*/
get content(): string {
return this.#collection.getUnderlyingObject(this.id).content ?? "";
}
/**
* Content converted to plain text
*/
get contentPlain(): string {
return this.#collection.client.markdownToText(this.content);
}
/**
* System message content
*/
get systemMessage(): SystemMessage | undefined {
return this.#collection.getUnderlyingObject(this.id).systemMessage;
}
/**
* Attachments
*/
get attachments(): File[] | undefined {
return this.#collection.getUnderlyingObject(this.id).attachments;
}
/**
* Time at which this message was edited
*/
get editedAt(): Date | undefined {
return this.#collection.getUnderlyingObject(this.id).editedAt;
}
/**
* Embeds
*/
get embeds(): MessageEmbed[] | undefined {
return this.#collection.getUnderlyingObject(this.id).embeds;
}
/**
* IDs of users this message mentions
*/
get mentionIds(): string[] | undefined {
return this.#collection.getUnderlyingObject(this.id).mentionIds;
}
/**
* IDs of roles this message mentions
*/
get roleMentionIds(): string[] | undefined {
return this.#collection.getUnderlyingObject(this.id).roleMentionIds;
}
/**
* Roles this message mentions
*/
get roleMentions(): ServerRole[] | undefined {
return this.roleMentionIds
?.map((roleId) => this.server?.roles.get(roleId) as ServerRole)
.filter((role) => role);
}
/**
* Whether this message mentions us
*/
get mentioned(): boolean {
return (
!!(this.flags & MessageFlags.MentionsEveryone) ||
!!(this.flags & MessageFlags.MentionsOnline) ||
this.mentionIds?.includes(this.#collection.client.user!.id) ||
this.roleMentions?.some((role) => role.assigned) ||
false
);
}
/**
* IDs of messages this message replies to
*/
get replyIds(): string[] | undefined {
return this.#collection.getUnderlyingObject(this.id).replyIds;
}
/**
* Reactions
*/
get reactions(): ReactiveMap<string, ReactiveSet<string>> {
return this.#collection.getUnderlyingObject(this.id).reactions;
}
/**
* Interactions
*/
get interactions(): APIMessage["interactions"] {
return this.#collection.getUnderlyingObject(this.id).interactions;
}
/**
* Masquerade
*/
get masquerade(): Masquerade | undefined {
return this.#collection.getUnderlyingObject(this.id).masquerade;
}
/**
* Whether this message is pinned
*/
get pinned(): boolean {
return this.#collection.getUnderlyingObject(this.id).pinned || false;
}
/**
* Flags
*/
get flags(): number {
return this.#collection.getUnderlyingObject(this.id).flags || 0;
}
/**
* Get the username for this message
*/
get username(): string | undefined {
const webhook = this.webhook;
return (
this.masquerade?.name ??
(webhook
? webhook.name
: (this.member?.nickname ?? this.author?.username))
);
}
/**
* Get the role colour for this message
*/
get roleColour(): string | null | undefined {
return this.masquerade?.colour ?? this.member?.roleColour;
}
/**
* Get the avatar URL for this message
*/
get avatarURL(): string | undefined {
const webhook = this.webhook;
return (
this.masqueradeAvatarURL ??
(webhook
? webhook.avatarURL
: (this.member?.avatarURL ?? this.author?.avatarURL))
);
}
/**
* Get the animated avatar URL for this message
*/
get animatedAvatarURL(): string | undefined {
const webhook = this.webhook;
return (
this.masqueradeAvatarURL ??
(webhook
? webhook.avatarURL
: this.member
? this.member?.animatedAvatarURL
: this.author?.animatedAvatarURL)
);
}
/**
* Avatar URL from the masquerade
*/
get masqueradeAvatarURL(): string | undefined {
const avatar = this.masquerade?.avatar;
return avatar ? this.#collection.client.proxyFile(avatar) : undefined;
}
/**
* Whether this message has suppressed desktop/push notifications
*/
get isSuppressed(): boolean {
return (this.flags & 1) === 1;
}
/**
* Edit a message
* @param data Message edit route data
*/
async edit(data: DataEditMessage): Promise<APIMessage> {
return await this.#collection.client.api.patch(
`/channels/${this.channelId as ""}/messages/${this.id as ""}`,
data,
);
}
/**
* Delete a message
*/
async delete(): Promise<void> {
return await this.#collection.client.api.delete(
`/channels/${this.channelId as ""}/messages/${this.id as ""}`,
);
}
/**
* Acknowledge this message as read
* @param skipRateLimiter Whether to skip the internal rate limiter
* @param skipRequest For internal updates only
* @param skipNextMarking For internal usage only
* @requires `SavedMessages`, `DirectMessage`, `Group`, `TextChannel`
*/
ack(
skipRateLimiter?: boolean,
skipRequest?: boolean,
skipNextMarking?: boolean,
): void {
this.channel?.ack(this, skipRateLimiter, skipRequest, skipNextMarking);
}
/**
* Reply to Message
*/
reply(
data:
| string
| (Omit<DataMessageSend, "nonce"> & {
nonce?: string;
}),
mention = true,
): Promise<Message> | undefined {
const obj = typeof data === "string" ? { content: data } : data;
return this.channel?.sendMessage({
...obj,
replies: [{ id: this.id, mention }],
});
}
/**
* Clear all reactions from this message
*/
async clearReactions(): Promise<void> {
return await this.#collection.client.api.delete(
`/channels/${this.channelId as ""}/messages/${this.id as ""}/reactions`,
);
}
/**
* React to a message
* @param emoji Unicode or emoji ID
*/
async react(emoji: string): Promise<void> {
return await this.#collection.client.api.put(
`/channels/${this.channelId as ""}/messages/${this.id as ""}/reactions/${
emoji as ""
}`,
);
}
/**
* Un-react from a message
* @param emoji Unicode or emoji ID
* @param deleteAll Remove all reactions
*/
async unreact(emoji: string, deleteAll = false): Promise<void> {
return await this.#collection.client.api.delete(
`/channels/${this.channelId as ""}/messages/${this.id as ""}/reactions/${
emoji as ""
}`,
{ remove_all: deleteAll },
);
}
/**
* Pin the message
*/
pin(): Promise<void> {
return this.#collection.client.api.post(
`/channels/${this.channelId as ""}/messages/${this.id as ""}/pin`,
);
}
/**
* Unpin the message
*/
unpin(): Promise<void> {
return this.#collection.client.api.delete(
`/channels/${this.channelId as ""}/messages/${this.id as ""}/pin`,
);
}
}
/**
* Message Webhook Class
*/
export class MessageWebhook {
#client: Client;
readonly id: string;
readonly name: string;
readonly avatar?: File;
/**
* Construct Message Webhook
* @param client Client
* @param webhook Webhook data
*/
constructor(client: Client, webhook: APIMessageWebhook, id: string) {
this.#client = client;
this.id = id;
this.name = webhook.name;
this.avatar = webhook.avatar
? new File(client, {
_id: webhook.avatar,
tag: "avatars",
metadata: {
type: "Image",
width: 256,
height: 256,
},
})
: undefined;
}
/**
* Get the avatar URL for this message webhook
*/
get avatarURL(): string {
return (
this.avatar?.createFileURL() ??
`${this.#client.options.baseURL}/users/${this.id}/default_avatar`
);
}
}