-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Message.ts
331 lines (280 loc) · 9.12 KB
/
Message.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
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
import type { ChatMessagePayload, EmotePayload, MentionsPayload, WSPayload } from "@guildedjs/api";
import type { MessageContent } from "../typings";
import { buildMemberKey, buildReactionKey, parseToStamp, resolveContentToData } from "../util";
import { Base } from "./Base";
import type { Client } from "./Client";
import { Embed } from "./Embed";
import type { Member } from "./Member";
import type { Server } from "./Server";
import type { User } from "./User";
import type { Channel } from "./channels";
import type { ThreadChannel } from "./channels/ThreadChannel";
import type { CollectorOptions, CollectorReturnValue } from "./collectors/Collector";
export enum MessageType {
Default = 0,
System = 1,
}
export class Message extends Base<ChatMessagePayload> {
/** The ID of the channel */
readonly channelId: string;
/** The ID of the server this message belongs to */
readonly serverId: string | null;
/** The ID of the group this message was posted in */
readonly groupId: string | null;
/** The type of chat message. "system" messages are generated by Guilded, while "default" messages are user or bot-generated. */
readonly type: MessageType;
/** The content of the message */
content: string;
/** The mentions within this message */
mentions?: MentionsPayload;
/** The ID of the messages that this is replying to. */
readonly replyMessageIds: string[] = [];
/** If set, this message will only be seen by those mentioned or replied to. */
readonly isPrivate: boolean;
/** If set, this message did not notify, mention or reply recipients. */
readonly isSilent: boolean;
/** The ID of the user who created this message (Note: If this event has createdByBotId or createdByWebhookId present, this field will still be populated, but can be ignored. In these cases, the value of this field will always be Ann6LewA) */
readonly createdById: string;
/** Bool value to wether message is a reply or not */
readonly isReply: boolean;
/** The ID of the webhook who created this message, if it was created by a webhook */
readonly createdByWebhookId: string | null;
/** The timestamp that the message was created at. */
readonly _createdAt: number;
/** The timestamp that the message was updated at, if relevant */
_updatedAt: number | null;
/** Whether the message has been deleted */
deleted = false;
/** When the message was deleted, if it was */
_deletedAt: number | null = null;
/** Embeds contained within this message */
embeds: Embed[] = [];
constructor(client: Client, data: ChatMessagePayload) {
super(client, data);
this.isReply = Boolean(data.replyMessageIds);
this.channelId = data.channelId;
this.groupId = data.groupId ?? null;
this.content = data.content ?? "";
this.serverId = data.serverId ?? null;
this.replyMessageIds = data.replyMessageIds ?? [];
this.createdById = data.createdBy;
this.createdByWebhookId = data.createdByWebhookId ?? null;
this._createdAt = parseToStamp(data.createdAt)!;
this._updatedAt = null;
this.isPrivate = data.isPrivate ?? false;
this.isSilent = data.isSilent ?? false;
this.type = data.type === "system" ? MessageType.System : MessageType.Default;
this._update(data);
}
/** Update details of this structure */
_update(
data:
| Partial<ChatMessagePayload>
| {
deletedAt: string;
},
): this {
if ("content" in data && typeof data.content !== "undefined") {
this.content = data.content;
}
if ("mentions" in data) {
this.mentions = data.mentions;
}
if ("updatedAt" in data) {
this._updatedAt = parseToStamp(data.updatedAt);
}
if ("deletedAt" in data) {
this.deleted = true;
this._deletedAt = parseToStamp(data.deletedAt);
}
if ("embeds" in data) {
this.embeds = data.embeds?.map((x) => new Embed(x)) ?? [];
}
return this;
}
get createdAt(): Date {
return new Date(this._createdAt);
}
/**
* Returns the date and time the message was last updated, if relevant.
*/
get updatedAt(): Date | null {
return this._updatedAt ? new Date(this._updatedAt) : null;
}
/**
* Returns the date and time the message was deleted, if it was.
*/
get deletedAt(): Date | null {
return this._deletedAt ? new Date(this._deletedAt) : null;
}
/** Returns the url of this message */
get url(): string {
if (!this.serverId) return "";
return `https://www.guilded.gg/chat/${this.channelId}?messageId=${this.id}`;
}
/**
* Returns the author of this message, or null if the author is not cached.
*/
get author(): User | null {
return this.client.users.cache.get(this.createdById) ?? null;
}
/**
* Returns the ID of the user who sent this message.
*/
get authorId(): string {
return this.createdByWebhookId ?? this.createdById;
}
/**
* Returns the member of this message, if the message is in a server, or null otherwise or if the member is not cached.
*/
get member(): Member | null {
return this.serverId ? this.client.members.cache.get(buildMemberKey(this.serverId, this.authorId)) ?? null : null;
}
/**
* Returns the channel that this message belongs to, or null if the channel is not cached.
*/
get channel(): Channel | null {
return this.client.channels.cache.get(this.channelId) ?? null;
}
get server(): Server | null {
return this.serverId ? this.client.servers.cache.get(this.serverId) ?? null : null;
}
/**
* Edit message content.
*
* @param newContent - The new content of the message.
* @returns A promise that resolves with the updated message.
*/
async edit(newContent: MessageContent): Promise<Message> {
return this.client.messages.update(this.channelId, this.id, newContent);
}
/**
* Send a message in the same channel as this message.
*
* @param content - The content of the message.
* @example
* let replyObj = {
* content: 'This is text, supports **markdown**.',
* embeds: [{
* title: 'This is an embed title!',
* description: 'A description may go here'
* }]
* };
* message.send(replyObj)
*/
send(content: MessageContent): Promise<Message> {
return this.client.messages.send(this.channelId, content);
}
/**
* Send a message that replies to this message. It mentions the user who sent this message.
*
* @param content - The content of the message to send.
* @example
* let replyObj = {
* content: 'This is text, supports **markdown**.',
* embeds: [{
* title: 'This is an embed title!',
* description: 'A description may go here'
* }]
* };
* message.reply(replyObj)
*/
reply(content: MessageContent): Promise<Message> {
return this.client.messages.send(this.channelId, {
...resolveContentToData(content),
replyMessageIds: [this.id],
});
}
createThread(name: string): Promise<ThreadChannel> {
return this.client.channels.create({
name,
messageId: this.id,
type: "chat",
}) as Promise<ThreadChannel>;
}
awaitReactions(options: CollectorOptions<MessageReaction>): Promise<CollectorReturnValue<MessageReaction>> {
return this.client.reactions.awaitReactions(this.id, options);
}
/**
* Add a reaction emote.
*
* @param emoteId - The ID of the emote to add.
* @returns A promise that resolves when the emote has been added.
*/
async addReaction(emoteId: number): Promise<void> {
return this.client.rest.router.reactions.channelMessageReactionCreate({
channelId: this.channelId,
messageId: this.id,
emoteId,
});
}
/**
* Deletes either a whole reaction emote from a message or a specific user's if a userId is provided.
*
* @param emoteId - The ID of the emote to delete.
* @returns A promise that resolves when the emote has been deleted.
*/
deleteReaction(emoteId: number, userId?: string): Promise<void> {
return this.client.rest.router.reactions.channelMessageReactionDelete({
channelId: this.channelId,
messageId: this.id,
emoteId,
userId,
});
}
/**
* Delete this message.
*
* @returns A promise that resolves when the message has been deleted.
*/
delete(): Promise<void> {
return this.client.messages.delete(this.channelId, this.id);
}
}
/**
* Represents a reaction to a message.
*/
export class MessageReaction extends Base<FlattenedReactionData> {
/**
* The ID of the channel where the message was sent.
*/
readonly channelId: string;
/**
* The ID of the message this reaction belongs to.
*/
readonly messageId: string;
/**
* The ID of the user who created the reaction.
*/
readonly createdBy: string;
/**
* The emote associated with this reaction.
*/
readonly emote: EmotePayload;
/**
* The ID of the server where the reaction was made.
*/
readonly serverId: string;
/**
* Creates a new instance of the MessageReaction class.
*
* @param client The client that instantiated this object.
* @param data The data representing the reaction.
*/
constructor(client: Client, data: FlattenedReactionData) {
const formedId = buildReactionKey(data.createdBy, data.emote.id);
super(client, {
...data,
id: formedId,
});
this.id = formedId;
this.channelId = data.channelId;
this.messageId = data.messageId;
this.createdBy = data.createdBy;
this.emote = data.emote;
this.serverId = data.serverId;
}
}
type FlattenedReactionData = WSPayload<"ChannelMessageReactionCreated">["reaction"] & {
serverId: string;
};