-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes_structures_message.js.html
489 lines (442 loc) · 22.5 KB
/
classes_structures_message.js.html
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>classes/structures/message.js - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav>
<li class="nav-link nav-home-link"><a href="index.html">Home</a></li><li class="nav-heading">Tutorials</li><li class="nav-item"><a href="tutorial-example-bot.html">Example Bot</a></li><li class="nav-heading">Classes</li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="Client.html">Client</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Client.html#login">login</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="ClientWebSocket.html">ClientWebSocket</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="ClientWebSocket.html#connect">connect</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="Member.html">Member</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="MemberBan.html">MemberBan</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="MemberRemove.html">MemberRemove</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="MemberUpdated.html">MemberUpdated</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="Message.html">Message</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Message.html#delete">delete</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Message.html#edit">edit</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Message.html#getMember">getMember</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Message.html#getUser">getUser</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Message.html#react">react</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Message.html#reply">reply</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="MessageEmbed.html">MessageEmbed</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="MessageEmbed.html#addField">addField</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="MessageEmbed.html#setAuthor">setAuthor</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="MessageEmbed.html#setColor">setColor</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="MessageEmbed.html#setDescription">setDescription</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="MessageEmbed.html#setFields">setFields</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="MessageEmbed.html#setFooter">setFooter</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="MessageEmbed.html#setImage">setImage</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="MessageEmbed.html#setThumbnail">setThumbnail</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="MessageEmbed.html#setTimestamp">setTimestamp</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="MessageEmbed.html#setTitle">setTitle</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="MessageEmbed.html#setURL">setURL</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="Reaction.html">Reaction</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="User.html">User</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="User.html#getAvatarURL">getAvatarURL</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="User.html#getBannerURL">getBannerURL</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="UserSummary.html">UserSummary</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="Webhook.html">Webhook</a></span></li><li class="nav-heading">Events</li><li class="nav-heading"><span class="nav-item-type type-event">E</span><span class="nav-item-name"><a href="Client.html#event:closed">closed</a></span></li><li class="nav-heading"><span class="nav-item-type type-event">E</span><span class="nav-item-name"><a href="Client.html#event:debug">debug</a></span></li><li class="nav-heading"><span class="nav-item-type type-event">E</span><span class="nav-item-name"><a href="Client.html#event:destroy">destroy</a></span></li><li class="nav-heading"><span class="nav-item-type type-event">E</span><span class="nav-item-name"><a href="Client.html#event:error">error</a></span></li><li class="nav-heading"><span class="nav-item-type type-event">E</span><span class="nav-item-name"><a href="Client.html#event:memberBan">memberBan</a></span></li><li class="nav-heading"><span class="nav-item-type type-event">E</span><span class="nav-item-name"><a href="Client.html#event:memberJoin">memberJoin</a></span></li><li class="nav-heading"><span class="nav-item-type type-event">E</span><span class="nav-item-name"><a href="Client.html#event:memberLeave">memberLeave</a></span></li><li class="nav-heading"><span class="nav-item-type type-event">E</span><span class="nav-item-name"><a href="Client.html#event:memberRoleUpdate">memberRoleUpdate</a></span></li><li class="nav-heading"><span class="nav-item-type type-event">E</span><span class="nav-item-name"><a href="Client.html#event:memberUnban">memberUnban</a></span></li><li class="nav-heading"><span class="nav-item-type type-event">E</span><span class="nav-item-name"><a href="Client.html#event:memberUpdate">memberUpdate</a></span></li><li class="nav-heading"><span class="nav-item-type type-event">E</span><span class="nav-item-name"><a href="Client.html#event:message">message</a></span></li><li class="nav-heading"><span class="nav-item-type type-event">E</span><span class="nav-item-name"><a href="Client.html#event:messageDelete">messageDelete</a></span></li><li class="nav-heading"><span class="nav-item-type type-event">E</span><span class="nav-item-name"><a href="Client.html#event:messageUpdate">messageUpdate</a></span></li><li class="nav-heading"><span class="nav-item-type type-event">E</span><span class="nav-item-name"><a href="Client.html#event:reactionAdd">reactionAdd</a></span></li><li class="nav-heading"><span class="nav-item-type type-event">E</span><span class="nav-item-name"><a href="Client.html#event:reactionRemove">reactionRemove</a></span></li><li class="nav-heading"><span class="nav-item-type type-event">E</span><span class="nav-item-name"><a href="Client.html#event:ready">ready</a></span></li><li class="nav-heading"><span class="nav-item-type type-event">E</span><span class="nav-item-name"><a href="Client.html#event:webhookCreate">webhookCreate</a></span></li><li class="nav-heading"><span class="nav-item-type type-event">E</span><span class="nav-item-name"><a href="Client.html#event:webhookUpdate">webhookUpdate</a></span></li>
</nav>
<div id="main">
<h1 class="page-title">classes/structures/message.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>const msgs = require("../../helper/messages.js");
const guser = require("../../helper/members.js");
const User = require("./user/user.js");
const Reaction = require("./reaction.js");
/**
* Represents a message
*/
class Message {
/**
* Creates a new message object
* @param {Object} messageData The message data
* @param {Object} client The client object
* @returns {Message}
* @example
* const { Message } = require('guilded.js');
* const message = new Message(messageData, client);
*/
constructor(messageData, client) {
/**
* Client object
* @type {Client}
* @readonly
*/
this.client = client;
/**
* The message id
* @type {String}
* @readonly
*/
this.id = messageData.id;
/**
* The content of the message
* @type {String}
* @readonly
*/
this.content = messageData.content ?? "";
/**
* The attachments of the message
* @type {Array}
* @readonly
*/
this.attachments = [];
/**
* The message channel id
* @type {Channel}
* @readonly
*/
this.channel = {
/**
* The channel id
* @type {String}
* @readonly
*/
id: messageData.channelId,
/**
* Send a message to the current channel
* @param {String | Object} content The content of the message, this can be a string or an object
* @param {String} content.content The content of the message
* @param {Boolean} content.isPrivate Whether the message is private or not
* @param {Boolean} content.isSilent Whether the message is silent or not
* @param {Array} content.replyMessageIds The message ids to reply to
* @param {Array} content.embeds The embeds to send
* @param {Array} content.attachments The attachments to send
* @returns {Promise} The message object
* @private
*/
send: (content) => {
switch (typeof content) {
case "string":
return msgs.sendMessage(
{
content,
channelId: messageData.channelId,
isPrivate: false,
isSilent: false,
client: this.client,
}
);
case "object":
return msgs.sendMessage(
{
content: content.content,
channelId: messageData.channelId,
isPrivate: !!content.isPrivate,
isSilent: !!content.isSilent,
replyMessageIds: content.replyMessageIds
? content.replyMessageIds
: null,
embeds: content.embeds ? content.embeds : null,
attachments: content.attachments ? content.attachments : null,
client: this.client,
}
);
}
},
};
/**
* The server Object
* @type {Server | null}
* @readonly
*/
this.serverId = messageData.serverId ?? null;
// Check if message have attachments
if (this.content.includes("![")) {
const attachments = this.content.match(/!\[(.*?)\]\((.*?)\)/g);
if (attachments) {
for (let i = 0; i < attachments.length; i++) {
const attachment = attachments[i];
const attachmentUrl = attachment.match(/\((.*?)\)/)[1];
this.attachments[i] = attachmentUrl;
}
}
}
if (this.attachments && this.attachments.length > 0) {
for (let i = 0; i < this.attachments.length; i++) {
this.attachments[i] = this.attachments[i]
.replace("![](", "")
.slice(0, -1);
}
}
// Convert the message to a better format
// Example of message: "Hello\n" + "\n" + "Goodbye\n" + "![](https://s3-us-west-2.amazonaws.com/www.guilded.gg/ContentMedia/42237368ad7882d89108acccf777c2ac-Full.webp?w=100&h=100)"
// To: "Hello \n \n Goodbye \n ![](https://s3-us-west-2.amazonaws.com/www.guilded.gg/ContentMedia/42237368ad7882d89108acccf777c2ac-Full.webp?w=100&h=100)"
this.content = this.content
.replace(/\n/g, " \n ")
.replace(/\n {2}\n/g, "\n \n");
// Delete all files of the content
// Example of message: "Hello ![](https://s3-us-west-2.amazonaws.com/www.guilded.gg/ContentMedia/42237368ad7882d89108acccf777c2ac-Full.webp?w=100&h=100)"
// To: "Hello"
this.content = this.content.replace(/!\[(.*?)\]\((.*?)\)/g, "");
if (messageData.mentions) {
this.mentions = {};
if (messageData.mentions.users) {
this.mentions.users = new Map();
for (let i = 0; i < messageData.mentions.users.length; i++) {
const mention = messageData.mentions.users[i];
mention.server = {
id: messageData.serverId,
};
const user = new User(mention);
this.mentions.users.set(user.id, user);
}
this.mentions.users.first = () => {
return this.mentions.users.values().next().value;
};
}
if (messageData.mentions.roles) {
this.mentions.roles = [];
for (let i = 0; i < messageData.mentions.roles.length; i++) {
this.mentions.roles.push(messageData.mentions.roles[i].id);
}
}
}
/**
* Check if the message has replies
* @type {Boolean}
* @readonly
*/
this.hasReplies = Boolean(messageData.replyMessageIds);
/**
* The message replies
* @type {Array<String>}
* @readonly
*/
this.replyMessageIds = messageData.replyMessageIds ?? null;
/**
* Check if the message is private
* @type {Boolean}
* @readonly
*/
this.private = messageData.isPrivate ?? false;
/**
* The created date of the message
* @type {Date}
* @readonly
*/
this.createdAt = new Date(messageData.createdAt);
/**
* The author of the message
* @type {User}
* @readonly
*/
this.author = null;
if (global.cache.users.has(messageData.createdBy)) {
// Check properties and update them
const newAuthor = new User(
{
id: messageData.createdBy,
serverId: messageData.serverId,
},
client
);
const oldAuthor = global.cache.users.get(messageData.createdBy);
for (const property in newAuthor) {
if (
newAuthor[property] !== oldAuthor[property] &&
property !== null &&
property !== undefined &&
property !== NaN
) {
oldAuthor[property] = newAuthor[property];
}
}
this.author = oldAuthor;
global.cache.users.set(messageData.createdBy, oldAuthor);
} else {
this.author = new User(
{
id: messageData.createdBy,
serverId: messageData.serverId,
},
client
);
global.cache.users.set(this.author.id, this.author);
}
/**
* The webhook id of the message
* @type {String | null}
* @readonly
*/
this.webhookId = messageData.createdByWebhookId ?? null;
/**
* The updated date (if the message was edited)
* @type {Date | null}
* @readonly
*/
this.editedAt = messageData.updatedAt
? new Date(messageData.updatedAt)
: null;
/**
* All data of the message (for debugging)
* @type {Object}
* @readonly
*/
this.raw = messageData;
/**
* Send a message to the current channel to the user who sent the message
* @param {String | Object} content The content of the message, this can be a string or an object
* @param {String} content.content The content of the message
* @param {Boolean} content.isPrivate Whether the message is private or not
* @param {Boolean} content.isSilent Whether the message is silent or not
* @param {Array} content.replyMessageIds The message ids to reply to
* @param {Array} content.embeds The embeds to send
* @param {Array} content.attachments The attachments to send
* @returns {Message} The message object
* @example
* message.reply("Hello");
*/
this.reply = (content) => {
switch (typeof content) {
case "string":
return msgs.sendMessage(
{
content,
channelId: messageData.channelId,
isPrivate: false,
isSilent: false,
replyMessageIds: [this.id],
client: this.client,
}
);
case "object":
return msgs.sendMessage(
{
content: content.content,
channelId: messageData.channelId,
isPrivate: !!content.isPrivate,
isSilent: !!content.isSilent,
replyMessageIds: [this.id],
embeds: content.embeds ? content.embeds : null,
attachments: content.attachments ? content.attachments : null,
client: this.client,
}
);
}
};
/**
* Edit the current message
* @param {String | Object} content The content of the message, this can be a string or an object
* @param {String} content.content The content of the message
* @param {Boolean} content.isPrivate Whether the message is private or not
* @param {Boolean} content.isSilent Whether the message is silent or not
* @param {Array} content.replyMessageIds The message ids to reply to
* @param {Array} content.embeds The embeds to send
* @param {Array} content.attachments The attachments to send
* @returns {Message} The message object
* @example
* message.edit("Hello world!");
*/
this.edit = (content) => {
switch (typeof content) {
case "string":
return msgs.editMessage(
{
id: this.id,
content,
channelId: messageData.channelId,
isPrivate: false,
isSilent: false,
client: this.client,
}
);
case "object":
return msgs.editMessage(
{
id: this.id,
content: content.content,
channelId: messageData.channelId,
isPrivate: !!content.isPrivate,
isSilent: !!content.isSilent,
replyMessageIds: [this.id],
embeds: content.embeds ? content.embeds : null,
attachments: content.attachments ? content.attachments : null,
client: this.client,
}
);
}
};
/**
* Delete the current message
* @param {Object} options The options for the delete
* @param {Number} options.timeout The timeout for the delete
* @returns {Message} The message object
* @example
* message.delete();
* message.delete({ timeout: 5000 });
*/
this.delete = (object) => {
return msgs.deleteMessage(
{
id: this.id,
channelId: messageData.channelId,
timeout: object.timeout ? object.timeout : 0,
client: this.client,
}
);
};
/**
* Add a reaction to the current message
* @param {Number} emoji The emoji to react with
* @returns {Reaction} The reaction object
* @example
* message.react(90001164);
*/
this.react = (emoji) => {
if (typeof emoji !== "number") emoji = 90001164;
return msgs.reactMessage(
{
id: this.id,
channelId: messageData.channelId,
emojiId: emoji ?? 90001164,
client: this.client,
}
);
};
/**
* Get the user object of the author of the message or the user you want to get
* @param {string} userId optional - The user id of the user you want to get info from
* @returns {User} - The user object
* @example
* message.getUser();
* message.getUser("123456789");
*/
this.getUser = async (userId) => {
// Check if the user id is in cache
if (client.cache.users.get(userId ?? this.author.id)) {
return client.cache.users.get(userId ?? this.author.id);
} else {
// If not, get the user from the api
const newUser = await guser.getUser(
{
id: userId ?? this.author.id,
serverId: messageData.serverId,
client: client,
}
);
// Add the user to the cache
client.cache.users.set(userId, newUser);
return newUser;
}
};
/**
* Get the member object of the author of the message or the member you want to get
* @param {string} userId optional - The user id of the member you want to get info from
* @returns {Member} - The member object
* @example
* message.getMember();
* message.getMember("123456789");
*/
this.getMember = async (userId) => {
const member = await guser.getMember(
{
id: userId ?? this.author.id,
serverId: messageData.serverId,
client: client,
}
);
return member;
};
}
}
module.exports = Message;
</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.11</a> on Sat Oct 08 2022 01:54:14 GMT-0500 (hora estándar de Colombia) using the Minami theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>