-
Notifications
You must be signed in to change notification settings - Fork 417
/
Message.js
312 lines (282 loc) · 13.6 KB
/
Message.js
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
"use strict";
const Base = require("./Base");
const Call = require("./Call");
const {SystemJoinMessages} = require("../Constants");
const User = require("./User");
/**
* Represents a message
* @prop {String} id The ID of the message
* @prop {PrivateChannel | TextChannel | NewsChannel} channel The channel the message is in
* @prop {Number} timestamp Timestamp of message creation
* @prop {Number} type The type of the message
* @prop {User} author The message author
* @prop {Member?} member The message author with server-specific data
* @prop {User[]} mentions Array of mentioned users
* @prop {String} content Message content
* @prop {String?} cleanContent Message content with mentions replaced by names, and @everyone/@here escaped
* @prop {String[]} roleMentions Array of mentioned roles' ids
* @prop {String[]} channelMentions Array of mentions channels' ids
* @prop {Number?} editedTimestamp Timestamp of latest message edit
* @prop {Boolean} tts Whether to play the message using TTS or not
* @prop {Boolean} mentionEveryone Whether the message mentions everyone/here or not
* @prop {Object?} messageReference An object containing the reference to the original message if it is a crossposted message
* @prop {String} messageReference.messageID The id of the original message this message was crossposted from
* @prop {String} messageReference.channelID The id of the channel this message was crossposted from
* @prop {String} messageReference.guildID The id of the guild this message was crossposted from
* @prop {Number} flags Message flags (see constants)
* @prop {Object[]} attachments Array of attachments
* @prop {Object[]} embeds Array of embeds
* @prop {Object?} activity The activity specified in the message
* @prop {Object?} application The application of the activity in the message
* @prop {Object} reactions An object containing the reactions on the message
* @prop {Number} reactions.count The number of times the reaction was used
* @prop {Boolean} reactions.me Whether or not the bot user did the reaction
* @prop {Boolean} pinned Whether the message is pinned or not
* @prop {Command?} command The Command used in the Message, if any (CommandClient only)
*/
class Message extends Base {
constructor(data, client) {
super(data.id);
this._client = client;
this.type = data.type || 0;
this.timestamp = Date.parse(data.timestamp);
this.channel = client.getChannel(data.channel_id) || {
id: data.channel_id
};
this.content = "";
this.hit = !!data.hit;
this.reactions = {};
if(data.message_reference) {
this.messageReference = {
messageID: data.message_reference.message_id,
channelID: data.message_reference.channel_id,
guildID: data.message_reference.guild_id
};
} else {
this.messageReference = null;
}
this.flags = data.flags || 0;
if(data.author) {
if(data.author.discriminator !== "0000") {
this.author = client.users.add(data.author, client);
} else {
this.author = new User(data.author, client);
}
} else {
this._client.emit("error", new Error("MESSAGE_CREATE but no message author:\n" + JSON.stringify(data, null, 2)));
}
if(this.channel.guild) {
if(data.member) {
data.member.id = this.author.id;
this.member = this.channel.guild.members.update(data.member, this.channel.guild);
} else if(this.channel.guild.members.has(this.author.id)) {
this.member = this.channel.guild.members.get(this.author.id);
} else {
this.member = null;
}
} else {
this.member = null;
}
if(this.type === 0 || this.type === undefined);
else if(this.type === 1) {
data.content = `${this.author.mention} added <@${data.mentions[0].id}>.`;
} else if(this.type === 2) {
if(this.author.id === data.mentions[0].id) {
data.content = `@${this.author.username} left the group.`;
} else {
data.content = `${this.author.mention} removed @${data.mentions[0].username}.`;
}
} else if(this.type === 3) { // (╯°□°)╯︵ ┻━┻
if(data.call.ended_timestamp) {
if((!this.channel.lastCall || this.channel.lastCall.endedTimestamp < Date.parse(data.call.ended_timestamp))) {
data.call.id = this.id;
this.channel.lastCall = new Call(data.call, this.channel);
}
if(data.call.participants.includes(client.user.id)) {
data.content = `You missed a call from ${this.author.mention}.`;
} else {
data.content = `${this.author.mention} started a call.`;
}
} else {
if(!this.channel.call) {
data.call.id = this.id;
this.channel.call = new Call(data.call, this.channel);
}
data.content = `${this.author.mention} started a call. — Join the call.`;
}
} else if(this.type === 4) {
data.content = `${this.author.mention} changed the channel name: ${data.content}`;
} else if(this.type === 5) {
data.content = `${this.author.mention} changed the channel icon.`;
} else if(this.type === 6) {
data.content = `${this.author.mention} pinned a message to this channel. See all the pins.`;
} else if(this.type === 7) {
data.content = SystemJoinMessages[~~(this.createdAt % SystemJoinMessages.length)].replace(/%user%/g, this.author.mention);
} else if(this.type === 8) {
data.content = `${this.author.mention} just boosted the server!`;
} else if(this.type === 9 || this.type === 10 || this.type === 11) {
data.content = `${this.author.mention} just boosted the server! ${this.channel.guild ? this.channel.guild.name : data.guild_id} has achieved **Level ${this.type - 8}!**`;
} else if(this.type === 12) {
data.content = `${this.author.mention} has added ${data.content} to this channel`;
} else {
client.emit("warn", "Unhandled MESSAGE_CREATE type: " + JSON.stringify(data, null, 2));
}
this.update(data, client);
}
update(data, client) {
if(this.type === 3) { // (╯°□°)╯︵ ┻━┻
(this.channel.call || this.channel.lastCall).update(data.call);
}
if(data.content !== undefined) {
this.content = data.content || "";
this.mentionEveryone = !!data.mention_everyone;
this.mentions = data.mentions.map((mention) => {
const user = client.users.add(mention, client);
if(mention.member && this.channel.guild) {
mention.member.id = mention.id;
this.channel.guild.members.update(mention.member, this.channel.guild);
}
return user;
});
this.roleMentions = data.mention_roles;
}
if(data.pinned !== undefined) this.pinned = !!data.pinned;
if(data.edited_timestamp != undefined) this.editedTimestamp = Date.parse(data.edited_timestamp);
if(data.tts !== undefined) this.tts = data.tts;
if(data.attachments !== undefined) this.attachments = data.attachments;
if(data.embeds !== undefined) this.embeds = data.embeds;
if(data.activity !== undefined) this.activity = data.activity;
if(data.application !== undefined) this.application = data.application;
if(data.reactions) {
data.reactions.forEach((reaction) => {
this.reactions[reaction.emoji.id ? `${reaction.emoji.name}:${reaction.emoji.id}` : reaction.emoji.name] = {
count: reaction.count,
me: reaction.me
};
});
}
}
get cleanContent() {
let cleanContent = this.content;
cleanContent = cleanContent.replace(/<(:\w+:)[0-9]+>/g, "$1");
let authorName = this.author.username;
if(this.channel.guild) {
const member = this.channel.guild.members.get(this.author.id);
if(member && member.nick) {
authorName = member.nick;
}
}
cleanContent = cleanContent.replace(new RegExp(`<@!?${this.author.id}>`, "g"), "@" + authorName);
if(this.mentions) {
this.mentions.forEach((mention) => {
if(this.channel.guild) {
const member = this.channel.guild.members.get(mention.id);
if(member && member.nick) {
cleanContent = cleanContent.replace(new RegExp(`<@!?${mention.id}>`, "g"), "@" + member.nick);
}
}
cleanContent = cleanContent.replace(new RegExp(`<@!?${mention.id}>`, "g"), "@" + mention.username);
});
}
if(this.channel.guild && this.roleMentions) {
for(const roleID of this.roleMentions) {
const role = this.channel.guild.roles.get(roleID);
const roleName = role ? role.name : "deleted-role";
cleanContent = cleanContent.replace(new RegExp(`<@&${roleID}>`, "g"), "@" + roleName);
}
}
this.channelMentions.forEach((id) => {
const channel = this._client.getChannel(id);
if(channel && channel.name && channel.mention) {
cleanContent = cleanContent.replace(channel.mention, "#" + channel.name);
}
});
return cleanContent.replace(/@everyone/g, "@\u200beveryone").replace(/@here/g, "@\u200bhere");
}
get channelMentions() {
if(this._channelMentions) {
return this._channelMentions;
}
return (this._channelMentions = (this.content.match(/<#[0-9]+>/g) || []).map((mention) => mention.substring(2, mention.length - 1)));
}
/**
* Edit the message
* @arg {String | Array | Object} content A string, array of strings, or object. If an object is passed:
* @arg {String} content.content A content string
* @arg {Boolean} [content.disableEveryone] Whether to filter @everyone/@here or not (overrides default)
* @arg {Object} [content.embed] An embed object. See [the official Discord API documentation entry](https://discordapp.com/developers/docs/resources/channel#embed-object) for object structure
* @arg {Number} [content.flags] A number representing the flags to apply to the message. See [the official Discord API documentation entry](https://discordapp.com/developers/docs/resources/channel#message-object-message-flags) for flags reference
* @returns {Promise<Message>}
*/
edit(content) {
return this._client.editMessage.call(this._client, this.channel.id, this.id, content);
}
/**
* Pin the message
* @returns {Promise}
*/
pin() {
return this._client.pinMessage.call(this._client, this.channel.id, this.id);
}
/**
* Unpin the message
* @returns {Promise}
*/
unpin() {
return this._client.unpinMessage.call(this._client, this.channel.id, this.id);
}
/**
* Get a list of users who reacted with a specific reaction
* @arg {String} reaction The reaction (Unicode string if Unicode emoji, `emojiName:emojiID` if custom emoji)
* @arg {Number} [limit=100] The maximum number of users to get
* @arg {String} [before] Get users before this user ID
* @arg {String} [after] Get users after this user ID
* @returns {Promise<User[]>}
*/
getReaction(reaction, limit, before, after) {
return this._client.getMessageReaction.call(this._client, this.channel.id, this.id, reaction, limit, before, after);
}
/**
* Add a reaction to a message
* @arg {String} reaction The reaction (Unicode string if Unicode emoji, `emojiName:emojiID` if custom emoji)
* @arg {String} [userID="@me"] The ID of the user to react as
* @returns {Promise}
*/
addReaction(reaction, userID) {
return this._client.addMessageReaction.call(this._client, this.channel.id, this.id, reaction, userID);
}
/**
* Remove a reaction from a message
* @arg {String} reaction The reaction (Unicode string if Unicode emoji, `emojiName:emojiID` if custom emoji)
* @arg {String} [userID="@me"] The ID of the user to remove the reaction for
* @returns {Promise}
*/
removeReaction(reaction, userID) {
return this._client.removeMessageReaction.call(this._client, this.channel.id, this.id, reaction, userID);
}
/**
* Remove all reactions from a message
* @returns {Promise}
*/
removeReactions() {
return this._client.removeMessageReactions.call(this._client, this.channel.id, this.id);
}
/**
* Delete the message
* @arg {String} [reason] The reason to be displayed in audit logs
* @returns {Promise}
*/
delete(reason) {
return this._client.deleteMessage.call(this._client, this.channel.id, this.id, reason);
}
toJSON() {
const base = super.toJSON(true);
for(const prop of ["attachments", "author", "content", "editedTimestamp", "embeds", "hit", "mentionEveryone", "mentions", "pinned", "reactions", "roleMentions", "timestamp", "tts", "type"]) {
if(this[prop] !== undefined) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
}
return base;
}
}
module.exports = Message;