-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Message.ts
377 lines (321 loc) Β· 11.3 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
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
import type { Model } from "./Base.ts";
import type { Session } from "../session/Session.ts";
import type {
AllowedMentionsTypes,
DiscordEmbed,
DiscordMessage,
DiscordUser,
FileContent,
MessageActivityTypes,
MessageTypes,
} from "../vendor/external.ts";
import type { Component } from "./components/Component.ts";
import type { GetReactions } from "../util/Routes.ts";
import { MessageFlags } from "../util/shared/flags.ts";
import { iconHashToBigInt } from "../util/hash.ts";
import { Snowflake } from "../util/Snowflake.ts";
import User from "./User.ts";
import Member from "./Member.ts";
import Attachment from "./Attachment.ts";
import ComponentFactory from "./components/ComponentFactory.ts";
import MessageReaction from "./MessageReaction.ts";
// import ThreadChannel from "./channels/ThreadChannel.ts";
import * as Routes from "../util/Routes.ts";
/**
* @link https://discord.com/developers/docs/resources/channel#allowed-mentions-object
*/
export interface AllowedMentions {
parse?: AllowedMentionsTypes[];
repliedUser?: boolean;
roles?: Snowflake[];
users?: Snowflake[];
}
export interface CreateMessageReference {
messageId: Snowflake;
channelId?: Snowflake;
guildId?: Snowflake;
failIfNotExists?: boolean;
}
/**
* @link https://discord.com/developers/docs/resources/channel#create-message-json-params
*/
export interface CreateMessage {
embeds?: DiscordEmbed[];
content?: string;
allowedMentions?: AllowedMentions;
files?: FileContent[];
messageReference?: CreateMessageReference;
tts?: boolean;
}
/**
* @link https://discord.com/developers/docs/resources/channel#edit-message-json-params
*/
export interface EditMessage extends Partial<CreateMessage> {
flags?: MessageFlags;
}
export type ReactionResolvable = string | {
name: string;
id: Snowflake;
};
export interface WebhookAuthor {
id: string;
username: string;
discriminator: string;
avatar?: bigint;
}
/**
* Represents a message
* @link https://discord.com/developers/docs/resources/channel#message-object
*/
export class Message implements Model {
constructor(session: Session, data: DiscordMessage) {
this.session = session;
this.id = data.id;
this.type = data.type;
this.channelId = data.channel_id;
this.guildId = data.guild_id;
this.applicationId = data.application_id;
if (!data.webhook_id) {
this.author = new User(session, data.author);
}
this.flags = data.flags;
this.pinned = !!data.pinned;
this.tts = !!data.tts;
this.content = data.content!;
this.nonce = data.nonce;
this.mentionEveryone = data.mention_everyone;
this.timestamp = Date.parse(data.timestamp);
this.editedTimestamp = data.edited_timestamp ? Date.parse(data.edited_timestamp) : undefined;
this.reactions = data.reactions?.map((react) => new MessageReaction(session, react)) ?? [];
this.attachments = data.attachments.map((attachment) => new Attachment(session, attachment));
this.embeds = data.embeds;
if (data.thread && data.guild_id) {
// this.thread = new ThreadChannel(session, data.thread, data.guild_id);
}
// webhook handling
if (data.webhook_id && data.author.discriminator === "0000") {
this.webhook = {
id: data.webhook_id!,
username: data.author.username,
discriminator: data.author.discriminator,
avatar: data.author.avatar ? iconHashToBigInt(data.author.avatar) : undefined,
};
}
// user is always null on MessageCreate and its replaced with author
if (data.guild_id && data.member && !this.isWebhookMessage()) {
this.member = new Member(session, { ...data.member, user: data.author }, data.guild_id);
}
this.components = data.components?.map((component) => ComponentFactory.from(session, component)) ?? [];
if (data.activity) {
this.activity = {
partyId: data.activity.party_id,
type: data.activity.type,
};
}
}
readonly session: Session;
readonly id: Snowflake;
type: MessageTypes;
channelId: Snowflake;
guildId?: Snowflake;
applicationId?: Snowflake;
author!: User;
flags?: MessageFlags;
pinned: boolean;
tts: boolean;
content: string;
nonce?: string | number;
mentionEveryone: boolean;
timestamp: number;
editedTimestamp?: number;
reactions: MessageReaction[];
attachments: Attachment[];
embeds: DiscordEmbed[];
member?: Member;
thread?: ThreadChannel;
components: Component[];
webhook?: WebhookAuthor;
activity?: {
partyId?: Snowflake;
type: MessageActivityTypes;
};
get createdTimestamp() {
return Snowflake.snowflakeToTimestamp(this.id);
}
get createdAt() {
return new Date(this.createdTimestamp);
}
get sentAt() {
return new Date(this.timestamp);
}
get editedAt() {
return this.editedTimestamp ? new Date(this.editedTimestamp) : undefined;
}
get edited() {
return this.editedTimestamp;
}
get url() {
return `https://discord.com/channels/${this.guildId ?? "@me"}/${this.channelId}/${this.id}`;
}
async pin() {
await this.session.rest.runMethod<undefined>(
this.session.rest,
"PUT",
Routes.CHANNEL_PIN(this.channelId, this.id),
);
}
async unpin() {
await this.session.rest.runMethod<undefined>(
this.session.rest,
"DELETE",
Routes.CHANNEL_PIN(this.channelId, this.id),
);
}
/** Edits the current message */
async edit(options: EditMessage): Promise<Message> {
const message = await this.session.rest.runMethod(
this.session.rest,
"POST",
Routes.CHANNEL_MESSAGE(this.id, this.channelId),
{
content: options.content,
allowed_mentions: {
parse: options.allowedMentions?.parse,
roles: options.allowedMentions?.roles,
users: options.allowedMentions?.users,
replied_user: options.allowedMentions?.repliedUser,
},
flags: options.flags,
embeds: options.embeds,
},
);
return message;
}
async suppressEmbeds(suppress: true): Promise<Message>;
async suppressEmbeds(suppress: false): Promise<Message | undefined>;
async suppressEmbeds(suppress = true) {
if (this.flags === MessageFlags.SupressEmbeds && suppress === false) {
return;
}
const message = await this.edit({ flags: MessageFlags.SupressEmbeds });
return message;
}
async delete({ reason }: { reason: string }): Promise<Message> {
await this.session.rest.runMethod<undefined>(
this.session.rest,
"DELETE",
Routes.CHANNEL_MESSAGE(this.channelId, this.id),
{ reason },
);
return this;
}
/** Replies directly in the channel the message was sent */
async reply(options: CreateMessage): Promise<Message> {
const message = await this.session.rest.runMethod<DiscordMessage>(
this.session.rest,
"POST",
Routes.CHANNEL_MESSAGES(this.channelId),
{
content: options.content,
file: options.files,
allowed_mentions: {
parse: options.allowedMentions?.parse,
roles: options.allowedMentions?.roles,
users: options.allowedMentions?.users,
replied_user: options.allowedMentions?.repliedUser,
},
message_reference: options.messageReference
? {
message_id: options.messageReference.messageId,
channel_id: options.messageReference.channelId,
guild_id: options.messageReference.guildId,
fail_if_not_exists: options.messageReference.failIfNotExists ?? true,
}
: undefined,
embeds: options.embeds,
tts: options.tts,
},
);
return new Message(this.session, message);
}
/**
* alias for Message.addReaction
*/
get react() {
return this.addReaction;
}
async addReaction(reaction: ReactionResolvable) {
const r = typeof reaction === "string" ? reaction : `${reaction.name}:${reaction.id}`;
await this.session.rest.runMethod<undefined>(
this.session.rest,
"PUT",
Routes.CHANNEL_MESSAGE_REACTION_ME(this.channelId, this.id, r),
{},
);
}
async removeReaction(reaction: ReactionResolvable, options?: { userId: Snowflake }) {
const r = typeof reaction === "string" ? reaction : `${reaction.name}:${reaction.id}`;
await this.session.rest.runMethod<undefined>(
this.session.rest,
"DELETE",
options?.userId
? Routes.CHANNEL_MESSAGE_REACTION_USER(
this.channelId,
this.id,
r,
options.userId,
)
: Routes.CHANNEL_MESSAGE_REACTION_ME(this.channelId, this.id, r),
);
}
/**
* Get users who reacted with this emoji
*/
async fetchReactions(reaction: ReactionResolvable, options?: GetReactions): Promise<User[]> {
const r = typeof reaction === "string" ? reaction : `${reaction.name}:${reaction.id}`;
const users = await this.session.rest.runMethod<DiscordUser[]>(
this.session.rest,
"GET",
Routes.CHANNEL_MESSAGE_REACTION(this.channelId, this.id, encodeURIComponent(r), options),
);
return users.map((user) => new User(this.session, user));
}
async removeReactionEmoji(reaction: ReactionResolvable) {
const r = typeof reaction === "string" ? reaction : `${reaction.name}:${reaction.id}`;
await this.session.rest.runMethod<undefined>(
this.session.rest,
"DELETE",
Routes.CHANNEL_MESSAGE_REACTION(this.channelId, this.id, r),
);
}
async nukeReactions() {
await this.session.rest.runMethod<undefined>(
this.session.rest,
"DELETE",
Routes.CHANNEL_MESSAGE_REACTIONS(this.channelId, this.id),
);
}
async crosspost() {
const message = await this.session.rest.runMethod<DiscordMessage>(
this.session.rest,
"POST",
Routes.CHANNEL_MESSAGE_CROSSPOST(this.channelId, this.id),
);
return new Message(this.session, message);
}
/*
* alias of Message.crosspost
* */
get publish() {
return this.crosspost;
}
/** wheter the message comes from a guild **/
inGuild(): this is Message & { guildId: Snowflake } {
return !!this.guildId;
}
/** wheter the messages comes from a Webhook */
isWebhookMessage(): this is Message & { author: Partial<User>; webhook: WebhookAuthor; member: undefined } {
return !!this.webhook;
}
}
export default Message;