-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathMessage.ts
More file actions
177 lines (155 loc) · 4.55 KB
/
Message.ts
File metadata and controls
177 lines (155 loc) · 4.55 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
import { DiscordSnowflake } from '@sapphire/snowflake';
import type { APIMessage, MessageFlags } from 'discord-api-types/v10';
import { Structure } from '../Structure.js';
import { MessageFlagsBitField } from '../bitfields/MessageFlagsBitField.js';
import { dateToDiscordISOTimestamp } from '../utils/optimization.js';
import { kData, kEditedTimestamp } from '../utils/symbols.js';
import { isFieldSet, isIdSet } from '../utils/type-guards.js';
import type { Partialize } from '../utils/types.js';
/**
* Represents a message on Discord.
*
* @typeParam Omitted - Specify the properties that will not be stored in the raw data field as a union, implement via `DataTemplate`
* @remarks has substructures `Message`, `Channel`, `MessageActivity`, `MessageCall`, `MessageReference`, `Attachment`, `Application`, `ChannelMention`, `Reaction`, `Poll`, `ResolvedInteractionData`, `RoleSubscriptionData`, `Sticker`, all the different `Component`s, ... which need to be instantiated and stored by an extending class using it
*/
export class Message<Omitted extends keyof APIMessage | '' = 'edited_timestamp' | 'timestamp'> extends Structure<
APIMessage,
Omitted
> {
/**
* The template used for removing data from the raw data stored for each Message
*/
public static override DataTemplate: Partial<APIMessage> = {
set timestamp(_: string) {},
set edited_timestamp(_: string) {},
};
protected [kEditedTimestamp]: number | null = null;
/**
* @param data - The raw data received from the API for the message
*/
public constructor(data: Partialize<APIMessage, Omitted>) {
super(data);
this.optimizeData(data);
}
/**
* {@inheritDoc Structure.optimizeData}
*
* @internal
*/
protected override optimizeData(data: Partial<APIMessage>) {
if (data.edited_timestamp) {
this[kEditedTimestamp] = Date.parse(data.edited_timestamp);
}
}
/**
* The message's id
*/
public get id() {
return this[kData].id;
}
/**
* The id of the interaction's application, if this message is a reply to an interaction
*/
public get applicationId() {
return this[kData].application_id;
}
/**
* The channel's id this message was sent in
*/
public get channelId() {
return this[kData].channel_id;
}
/**
* The timestamp this message was created at
*/
public get createdTimestamp() {
return isIdSet(this.id) ? DiscordSnowflake.timestampFrom(this.id) : null;
}
/**
* The time the message was created at
*/
public get createdAt() {
const createdTimestamp = this.createdTimestamp;
return createdTimestamp ? new Date(createdTimestamp) : null;
}
/**
* The content of the message
*/
public get content() {
return this[kData].content;
}
/**
* The timestamp this message was last edited at, or `null` if it never was edited
*/
public get editedTimestamp() {
return this[kEditedTimestamp];
}
/**
* The time the message was last edited at, or `null` if it never was edited
*/
public get editedAt() {
const editedTimestamp = this.editedTimestamp;
return editedTimestamp ? new Date(editedTimestamp) : null;
}
/**
* The flags of this message as a bit field
*/
public get flags() {
return isFieldSet(this[kData], 'flags', 'number')
? new MessageFlagsBitField(this[kData].flags as MessageFlags)
: null;
}
/**
* The nonce used when sending this message.
*
* @remarks This is only present in MESSAGE_CREATE event, if a nonce was provided when sending
*/
public get nonce() {
return this[kData].nonce;
}
/**
* Whether this message is pinned in its channel
*/
public get pinned() {
return this[kData].pinned;
}
/**
* A generally increasing integer (there may be gaps or duplicates) that represents the approximate position of the message in a thread
* It can be used to estimate the relative position of the message in a thread in company with `totalMessageSent` on parent thread
*/
public get position() {
return this[kData].position;
}
/**
* Whether this message was a TTS message
*/
public get tts() {
return this[kData].tts;
}
/**
* The type of message
*/
public get type() {
return this[kData].type;
}
/**
* If the message is generated by a webhook, this is the webhook's id
*/
public get webhookId() {
return this[kData].webhook_id;
}
/**
* {@inheritDoc Structure.toJSON}
*/
public override toJSON() {
const clone = super.toJSON();
if (this[kEditedTimestamp]) {
clone.edited_timestamp = dateToDiscordISOTimestamp(new Date(this[kEditedTimestamp]));
}
const createdAt = this.createdAt;
if (createdAt) {
clone.timestamp = dateToDiscordISOTimestamp(createdAt);
}
return clone;
}
}