discord.js-selfv13 13.0.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of discord.js-selfv13 might be problematic. Click here for more details.
- package/.dccache +1 -0
- package/LICENSE +190 -0
- package/README.md +87 -0
- package/browser.js +9 -0
- package/deploy/deploy-key.enc +0 -0
- package/deploy/deploy.sh +90 -0
- package/deploy/test.sh +34 -0
- package/docs/README.md +1 -0
- package/docs/examples/attachments.md +163 -0
- package/docs/examples/avatars.js +29 -0
- package/docs/examples/embed.js +38 -0
- package/docs/examples/greeting.js +30 -0
- package/docs/examples/moderation.md +145 -0
- package/docs/examples/ping.js +29 -0
- package/docs/examples/webhook.js +12 -0
- package/docs/general/faq.md +23 -0
- package/docs/general/updating.md +181 -0
- package/docs/general/welcome.md +95 -0
- package/docs/index.yml +30 -0
- package/docs/logo.svg +19 -0
- package/docs/topics/voice.md +113 -0
- package/docs/topics/web.md +38 -0
- package/package.json +147 -0
- package/src/client/Client.js +564 -0
- package/src/client/ClientDataManager.js +150 -0
- package/src/client/ClientDataResolver.js +376 -0
- package/src/client/ClientManager.js +74 -0
- package/src/client/WebhookClient.js +118 -0
- package/src/client/actions/Action.js +23 -0
- package/src/client/actions/ActionsManager.js +40 -0
- package/src/client/actions/ChannelCreate.js +11 -0
- package/src/client/actions/ChannelDelete.js +30 -0
- package/src/client/actions/ChannelUpdate.js +74 -0
- package/src/client/actions/GuildBanRemove.js +13 -0
- package/src/client/actions/GuildChannelsPositionUpdate.js +19 -0
- package/src/client/actions/GuildDelete.js +57 -0
- package/src/client/actions/GuildEmojiCreate.js +17 -0
- package/src/client/actions/GuildEmojiDelete.js +18 -0
- package/src/client/actions/GuildEmojiUpdate.js +17 -0
- package/src/client/actions/GuildEmojisUpdate.js +38 -0
- package/src/client/actions/GuildMemberGet.js +10 -0
- package/src/client/actions/GuildMemberRemove.js +41 -0
- package/src/client/actions/GuildRoleCreate.js +26 -0
- package/src/client/actions/GuildRoleDelete.js +42 -0
- package/src/client/actions/GuildRoleUpdate.js +41 -0
- package/src/client/actions/GuildRolesPositionUpdate.js +19 -0
- package/src/client/actions/GuildSync.js +29 -0
- package/src/client/actions/GuildUpdate.js +34 -0
- package/src/client/actions/MessageCreate.js +53 -0
- package/src/client/actions/MessageDelete.js +35 -0
- package/src/client/actions/MessageDeleteBulk.js +26 -0
- package/src/client/actions/MessageReactionAdd.js +37 -0
- package/src/client/actions/MessageReactionRemove.js +37 -0
- package/src/client/actions/MessageReactionRemoveAll.js +25 -0
- package/src/client/actions/MessageUpdate.js +40 -0
- package/src/client/actions/Ready.js +1 -0
- package/src/client/actions/Ready.js.bak +65 -0
- package/src/client/actions/UserGet.js +11 -0
- package/src/client/actions/UserNoteUpdate.js +30 -0
- package/src/client/actions/UserUpdate.js +33 -0
- package/src/client/rest/APIRequest.js +56 -0
- package/src/client/rest/DiscordAPIError.js +60 -0
- package/src/client/rest/RESTManager.js +58 -0
- package/src/client/rest/RESTMethods.js +1006 -0
- package/src/client/rest/RequestHandlers/Burst.js +90 -0
- package/src/client/rest/RequestHandlers/RequestHandler.js +54 -0
- package/src/client/rest/RequestHandlers/Sequential.js +132 -0
- package/src/client/rest/UserAgentManager.js +25 -0
- package/src/client/voice/ClientVoiceManager.js +81 -0
- package/src/client/voice/VoiceBroadcast.js +366 -0
- package/src/client/voice/VoiceConnection.js +530 -0
- package/src/client/voice/VoiceUDPClient.js +127 -0
- package/src/client/voice/VoiceWebSocket.js +246 -0
- package/src/client/voice/dispatcher/StreamDispatcher.js +331 -0
- package/src/client/voice/opus/BaseOpusEngine.js +60 -0
- package/src/client/voice/opus/NodeOpusEngine.js +40 -0
- package/src/client/voice/opus/OpusEngineList.js +28 -0
- package/src/client/voice/opus/OpusScriptEngine.js +45 -0
- package/src/client/voice/player/AudioPlayer.js +170 -0
- package/src/client/voice/receiver/VoiceReadable.js +17 -0
- package/src/client/voice/receiver/VoiceReceiver.js +219 -0
- package/src/client/voice/util/SecretKey.js +16 -0
- package/src/client/voice/util/Secretbox.js +33 -0
- package/src/client/voice/util/VolumeInterface.js +86 -0
- package/src/client/websocket/WebSocketConnection.js +506 -0
- package/src/client/websocket/WebSocketManager.js +90 -0
- package/src/client/websocket/packets/WebSocketPacketManager.js +110 -0
- package/src/client/websocket/packets/handlers/AbstractHandler.js +11 -0
- package/src/client/websocket/packets/handlers/ChannelCreate.js +17 -0
- package/src/client/websocket/packets/handlers/ChannelDelete.js +20 -0
- package/src/client/websocket/packets/handlers/ChannelPinsUpdate.js +37 -0
- package/src/client/websocket/packets/handlers/ChannelUpdate.js +11 -0
- package/src/client/websocket/packets/handlers/GuildBanAdd.js +23 -0
- package/src/client/websocket/packets/handlers/GuildBanRemove.js +20 -0
- package/src/client/websocket/packets/handlers/GuildCreate.js +22 -0
- package/src/client/websocket/packets/handlers/GuildDelete.js +19 -0
- package/src/client/websocket/packets/handlers/GuildEmojisUpdate.js +11 -0
- package/src/client/websocket/packets/handlers/GuildIntegrationsUpdate.js +19 -0
- package/src/client/websocket/packets/handlers/GuildMemberAdd.js +17 -0
- package/src/client/websocket/packets/handlers/GuildMemberRemove.js +13 -0
- package/src/client/websocket/packets/handlers/GuildMemberUpdate.js +18 -0
- package/src/client/websocket/packets/handlers/GuildMembersChunk.js +33 -0
- package/src/client/websocket/packets/handlers/GuildRoleCreate.js +11 -0
- package/src/client/websocket/packets/handlers/GuildRoleDelete.js +11 -0
- package/src/client/websocket/packets/handlers/GuildRoleUpdate.js +11 -0
- package/src/client/websocket/packets/handlers/GuildSync.js +11 -0
- package/src/client/websocket/packets/handlers/GuildUpdate.js +11 -0
- package/src/client/websocket/packets/handlers/MessageCreate.js +19 -0
- package/src/client/websocket/packets/handlers/MessageDelete.js +19 -0
- package/src/client/websocket/packets/handlers/MessageDeleteBulk.js +17 -0
- package/src/client/websocket/packets/handlers/MessageReactionAdd.js +11 -0
- package/src/client/websocket/packets/handlers/MessageReactionRemove.js +11 -0
- package/src/client/websocket/packets/handlers/MessageReactionRemoveAll.js +11 -0
- package/src/client/websocket/packets/handlers/MessageUpdate.js +11 -0
- package/src/client/websocket/packets/handlers/PresenceUpdate.js +76 -0
- package/src/client/websocket/packets/handlers/Ready.js +83 -0
- package/src/client/websocket/packets/handlers/RelationshipAdd.js +19 -0
- package/src/client/websocket/packets/handlers/RelationshipRemove.js +19 -0
- package/src/client/websocket/packets/handlers/Resumed.js +28 -0
- package/src/client/websocket/packets/handlers/TypingStart.js +68 -0
- package/src/client/websocket/packets/handlers/UserGuildSettingsUpdate.js +21 -0
- package/src/client/websocket/packets/handlers/UserNoteUpdate.js +12 -0
- package/src/client/websocket/packets/handlers/UserSettingsUpdate.js +18 -0
- package/src/client/websocket/packets/handlers/UserUpdate.js +11 -0
- package/src/client/websocket/packets/handlers/VoiceServerUpdate.js +19 -0
- package/src/client/websocket/packets/handlers/VoiceStateUpdate.js +52 -0
- package/src/client/websocket/packets/handlers/WebhooksUpdate.js +19 -0
- package/src/index.js +66 -0
- package/src/sharding/Shard.js +282 -0
- package/src/sharding/ShardClientUtil.js +146 -0
- package/src/sharding/ShardingManager.js +220 -0
- package/src/structures/Attachment.js +75 -0
- package/src/structures/CategoryChannel.js +22 -0
- package/src/structures/Channel.js +78 -0
- package/src/structures/ClientUser.js +447 -0
- package/src/structures/ClientUserChannelOverride.js +30 -0
- package/src/structures/ClientUserGuildSettings.js +60 -0
- package/src/structures/ClientUserSettings.js +80 -0
- package/src/structures/DMChannel.js +76 -0
- package/src/structures/Emoji.js +256 -0
- package/src/structures/GroupDMChannel.js +246 -0
- package/src/structures/Guild.js +1461 -0
- package/src/structures/GuildAuditLogs.js +371 -0
- package/src/structures/GuildChannel.js +537 -0
- package/src/structures/GuildMember.js +613 -0
- package/src/structures/Invite.js +164 -0
- package/src/structures/Message.js +605 -0
- package/src/structures/MessageAttachment.js +68 -0
- package/src/structures/MessageCollector.js +100 -0
- package/src/structures/MessageEmbed.js +386 -0
- package/src/structures/MessageMentions.js +144 -0
- package/src/structures/MessageReaction.js +96 -0
- package/src/structures/NewsChannel.js +24 -0
- package/src/structures/OAuth2Application.js +148 -0
- package/src/structures/PartialGuild.js +51 -0
- package/src/structures/PartialGuildChannel.js +44 -0
- package/src/structures/PermissionOverwrites.js +69 -0
- package/src/structures/Presence.js +241 -0
- package/src/structures/ReactionCollector.js +85 -0
- package/src/structures/ReactionEmoji.js +49 -0
- package/src/structures/RichEmbed.js +295 -0
- package/src/structures/Role.js +376 -0
- package/src/structures/StoreChannel.js +25 -0
- package/src/structures/TextChannel.js +154 -0
- package/src/structures/User.js +329 -0
- package/src/structures/UserConnection.js +48 -0
- package/src/structures/UserProfile.js +62 -0
- package/src/structures/VoiceChannel.js +146 -0
- package/src/structures/VoiceRegion.js +50 -0
- package/src/structures/Webhook.js +304 -0
- package/src/structures/interfaces/Collector.js +179 -0
- package/src/structures/interfaces/TextBasedChannel.js +635 -0
- package/src/structures/shared/resolvePermissions.js +26 -0
- package/src/util/Collection.js +532 -0
- package/src/util/Constants.js +845 -0
- package/src/util/Permissions.js +306 -0
- package/src/util/Snowflake.js +82 -0
- package/src/util/Util.js +221 -0
- package/test/random.js +207 -0
- package/test/shard.js +31 -0
- package/test/sharder.js +7 -0
- package/test/voice.js +78 -0
- package/test/webpack.html +31 -0
- package/tsconfig.json +13 -0
- package/tslint.json +62 -0
- package/typings/discord.js-test.ts +69 -0
- package/typings/index.d.ts +2190 -0
- package/webpack.config.js +62 -0
@@ -0,0 +1,605 @@
|
|
1
|
+
const Mentions = require('./MessageMentions');
|
2
|
+
const Attachment = require('./MessageAttachment');
|
3
|
+
const Embed = require('./MessageEmbed');
|
4
|
+
const RichEmbed = require('./RichEmbed');
|
5
|
+
const MessageReaction = require('./MessageReaction');
|
6
|
+
const ReactionCollector = require('./ReactionCollector');
|
7
|
+
const Util = require('../util/Util');
|
8
|
+
const Collection = require('../util/Collection');
|
9
|
+
const Constants = require('../util/Constants');
|
10
|
+
const Permissions = require('../util/Permissions');
|
11
|
+
let GuildMember;
|
12
|
+
|
13
|
+
/**
|
14
|
+
* Represents a message on Discord.
|
15
|
+
*/
|
16
|
+
class Message {
|
17
|
+
constructor(channel, data, client) {
|
18
|
+
/**
|
19
|
+
* The client that instantiated the Message
|
20
|
+
* @name Message#client
|
21
|
+
* @type {Client}
|
22
|
+
* @readonly
|
23
|
+
*/
|
24
|
+
Object.defineProperty(this, 'client', { value: client });
|
25
|
+
|
26
|
+
/**
|
27
|
+
* The channel that the message was sent in
|
28
|
+
* @type {TextChannel|DMChannel|GroupDMChannel}
|
29
|
+
*/
|
30
|
+
this.channel = channel;
|
31
|
+
|
32
|
+
/**
|
33
|
+
* Whether this message has been deleted
|
34
|
+
* @type {boolean}
|
35
|
+
*/
|
36
|
+
this.deleted = false;
|
37
|
+
|
38
|
+
if (data) this.setup(data);
|
39
|
+
}
|
40
|
+
|
41
|
+
setup(data) { // eslint-disable-line complexity
|
42
|
+
/**
|
43
|
+
* The ID of the message
|
44
|
+
* @type {Snowflake}
|
45
|
+
*/
|
46
|
+
this.id = data.id;
|
47
|
+
|
48
|
+
/**
|
49
|
+
* The type of the message
|
50
|
+
* @type {MessageType}
|
51
|
+
*/
|
52
|
+
this.type = Constants.MessageTypes[data.type];
|
53
|
+
|
54
|
+
/**
|
55
|
+
* The content of the message
|
56
|
+
* @type {string}
|
57
|
+
*/
|
58
|
+
this.content = data.content;
|
59
|
+
|
60
|
+
/**
|
61
|
+
* The author of the message
|
62
|
+
* @type {User}
|
63
|
+
*/
|
64
|
+
this.author = this.client.dataManager.newUser(data.author, !data.webhook_id);
|
65
|
+
|
66
|
+
/**
|
67
|
+
* Represents the author of the message as a guild member
|
68
|
+
* Only available if the message comes from a guild where the author is still a member
|
69
|
+
* @type {?GuildMember}
|
70
|
+
*/
|
71
|
+
this.member = this.guild ? this.guild.member(this.author) || null : null;
|
72
|
+
|
73
|
+
/**
|
74
|
+
* Whether or not this message is pinned
|
75
|
+
* @type {boolean}
|
76
|
+
*/
|
77
|
+
this.pinned = data.pinned;
|
78
|
+
|
79
|
+
/**
|
80
|
+
* Whether or not the message was Text-To-Speech
|
81
|
+
* @type {boolean}
|
82
|
+
*/
|
83
|
+
this.tts = data.tts;
|
84
|
+
|
85
|
+
/**
|
86
|
+
* A random number or string used for checking message delivery
|
87
|
+
* @type {string}
|
88
|
+
*/
|
89
|
+
this.nonce = data.nonce;
|
90
|
+
|
91
|
+
/**
|
92
|
+
* Whether or not this message was sent by Discord, not actually a user (e.g. pin notifications)
|
93
|
+
* @type {boolean}
|
94
|
+
*/
|
95
|
+
this.system = data.type === 6;
|
96
|
+
|
97
|
+
/**
|
98
|
+
* A list of embeds in the message - e.g. YouTube Player
|
99
|
+
* @type {MessageEmbed[]}
|
100
|
+
*/
|
101
|
+
this.embeds = data.embeds.map(e => new Embed(this, e));
|
102
|
+
|
103
|
+
/**
|
104
|
+
* A collection of attachments in the message - e.g. Pictures - mapped by their ID
|
105
|
+
* @type {Collection<Snowflake, MessageAttachment>}
|
106
|
+
*/
|
107
|
+
this.attachments = new Collection();
|
108
|
+
for (const attachment of data.attachments) this.attachments.set(attachment.id, new Attachment(this, attachment));
|
109
|
+
|
110
|
+
/**
|
111
|
+
* The timestamp the message was sent at
|
112
|
+
* @type {number}
|
113
|
+
*/
|
114
|
+
this.createdTimestamp = new Date(data.timestamp).getTime();
|
115
|
+
|
116
|
+
/**
|
117
|
+
* The timestamp the message was last edited at (if applicable)
|
118
|
+
* @type {?number}
|
119
|
+
*/
|
120
|
+
this.editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp).getTime() : null;
|
121
|
+
|
122
|
+
/**
|
123
|
+
* A collection of reactions to this message, mapped by the reaction ID
|
124
|
+
* @type {Collection<Snowflake, MessageReaction>}
|
125
|
+
*/
|
126
|
+
this.reactions = new Collection();
|
127
|
+
if (data.reactions && data.reactions.length > 0) {
|
128
|
+
for (const reaction of data.reactions) {
|
129
|
+
const id = reaction.emoji.id ? `${reaction.emoji.name}:${reaction.emoji.id}` : reaction.emoji.name;
|
130
|
+
this.reactions.set(id, new MessageReaction(this, reaction.emoji, reaction.count, reaction.me));
|
131
|
+
}
|
132
|
+
}
|
133
|
+
|
134
|
+
/**
|
135
|
+
* All valid mentions that the message contains
|
136
|
+
* @type {MessageMentions}
|
137
|
+
*/
|
138
|
+
this.mentions = new Mentions(this, data.mentions, data.mention_roles, data.mention_everyone);
|
139
|
+
|
140
|
+
/**
|
141
|
+
* ID of the webhook that sent the message, if applicable
|
142
|
+
* @type {?Snowflake}
|
143
|
+
*/
|
144
|
+
this.webhookID = data.webhook_id || null;
|
145
|
+
|
146
|
+
/**
|
147
|
+
* Whether this message is a hit in a search
|
148
|
+
* @type {?boolean}
|
149
|
+
*/
|
150
|
+
this.hit = typeof data.hit === 'boolean' ? data.hit : null;
|
151
|
+
|
152
|
+
/**
|
153
|
+
* The previous versions of the message, sorted with the most recent first
|
154
|
+
* @type {Message[]}
|
155
|
+
* @private
|
156
|
+
*/
|
157
|
+
this._edits = [];
|
158
|
+
}
|
159
|
+
|
160
|
+
/**
|
161
|
+
* Updates the message.
|
162
|
+
* @param {Object} data Raw Discord message update data
|
163
|
+
* @private
|
164
|
+
*/
|
165
|
+
patch(data) {
|
166
|
+
const clone = Util.cloneObject(this);
|
167
|
+
this._edits.unshift(clone);
|
168
|
+
|
169
|
+
if ('edited_timestamp' in data) this.editedTimestamp = new Date(data.edited_timestamp).getTime();
|
170
|
+
if ('content' in data) this.content = data.content;
|
171
|
+
if ('pinned' in data) this.pinned = data.pinned;
|
172
|
+
if ('tts' in data) this.tts = data.tts;
|
173
|
+
if ('embeds' in data) this.embeds = data.embeds.map(e => new Embed(this, e));
|
174
|
+
else this.embeds = this.embeds.slice();
|
175
|
+
|
176
|
+
if ('attachments' in data) {
|
177
|
+
this.attachments = new Collection();
|
178
|
+
for (const attachment of data.attachments) this.attachments.set(attachment.id, new Attachment(this, attachment));
|
179
|
+
} else {
|
180
|
+
this.attachments = new Collection(this.attachments);
|
181
|
+
}
|
182
|
+
|
183
|
+
this.mentions = new Mentions(
|
184
|
+
this,
|
185
|
+
'mentions' in data ? data.mentions : this.mentions.users,
|
186
|
+
'mentions_roles' in data ? data.mentions_roles : this.mentions.roles,
|
187
|
+
'mention_everyone' in data ? data.mention_everyone : this.mentions.everyone
|
188
|
+
);
|
189
|
+
}
|
190
|
+
|
191
|
+
/**
|
192
|
+
* The time the message was sent
|
193
|
+
* @type {Date}
|
194
|
+
* @readonly
|
195
|
+
*/
|
196
|
+
get createdAt() {
|
197
|
+
return new Date(this.createdTimestamp);
|
198
|
+
}
|
199
|
+
|
200
|
+
/**
|
201
|
+
* The time the message was last edited at (if applicable)
|
202
|
+
* @type {?Date}
|
203
|
+
* @readonly
|
204
|
+
*/
|
205
|
+
get editedAt() {
|
206
|
+
return this.editedTimestamp ? new Date(this.editedTimestamp) : null;
|
207
|
+
}
|
208
|
+
|
209
|
+
/**
|
210
|
+
* The guild the message was sent in (if in a guild channel)
|
211
|
+
* @type {?Guild}
|
212
|
+
* @readonly
|
213
|
+
*/
|
214
|
+
get guild() {
|
215
|
+
return this.channel.guild || null;
|
216
|
+
}
|
217
|
+
|
218
|
+
/**
|
219
|
+
* The url to jump to the message
|
220
|
+
* @type {string}
|
221
|
+
* @readonly
|
222
|
+
*/
|
223
|
+
get url() {
|
224
|
+
return `https://discordapp.com/channels/${this.guild ? this.guild.id : '@me'}/${this.channel.id}/${this.id}`;
|
225
|
+
}
|
226
|
+
|
227
|
+
/**
|
228
|
+
* The message contents with all mentions replaced by the equivalent text.
|
229
|
+
* If mentions cannot be resolved to a name, the relevant mention in the message content will not be converted.
|
230
|
+
* @type {string}
|
231
|
+
* @readonly
|
232
|
+
*/
|
233
|
+
get cleanContent() {
|
234
|
+
return this.content
|
235
|
+
.replace(/@(everyone|here)/g, '@\u200b$1')
|
236
|
+
.replace(/<@!?[0-9]+>/g, input => {
|
237
|
+
const id = input.replace(/<|!|>|@/g, '');
|
238
|
+
if (this.channel.type === 'dm' || this.channel.type === 'group') {
|
239
|
+
return this.client.users.has(id) ? `@${this.client.users.get(id).username}` : input;
|
240
|
+
}
|
241
|
+
|
242
|
+
const member = this.channel.guild.members.get(id);
|
243
|
+
if (member) {
|
244
|
+
if (member.nickname) return `@${member.nickname}`;
|
245
|
+
return `@${member.user.username}`;
|
246
|
+
} else {
|
247
|
+
const user = this.client.users.get(id);
|
248
|
+
if (user) return `@${user.username}`;
|
249
|
+
return input;
|
250
|
+
}
|
251
|
+
})
|
252
|
+
.replace(/<#[0-9]+>/g, input => {
|
253
|
+
const channel = this.client.channels.get(input.replace(/<|#|>/g, ''));
|
254
|
+
if (channel) return `#${channel.name}`;
|
255
|
+
return input;
|
256
|
+
})
|
257
|
+
.replace(/<@&[0-9]+>/g, input => {
|
258
|
+
if (this.channel.type === 'dm' || this.channel.type === 'group') return input;
|
259
|
+
const role = this.guild.roles.get(input.replace(/<|@|>|&/g, ''));
|
260
|
+
if (role) return `@${role.name}`;
|
261
|
+
return input;
|
262
|
+
});
|
263
|
+
}
|
264
|
+
|
265
|
+
/**
|
266
|
+
* Creates a reaction collector.
|
267
|
+
* @param {CollectorFilter} filter The filter to apply
|
268
|
+
* @param {ReactionCollectorOptions} [options={}] Options to send to the collector
|
269
|
+
* @returns {ReactionCollector}
|
270
|
+
* @example
|
271
|
+
* // Create a reaction collector
|
272
|
+
* const filter = (reaction, user) => reaction.emoji.name === '👌' && user.id === 'someID'
|
273
|
+
* const collector = message.createReactionCollector(filter, { time: 15000 });
|
274
|
+
* collector.on('collect', r => console.log(`Collected ${r.emoji.name}`));
|
275
|
+
* collector.on('end', collected => console.log(`Collected ${collected.size} items`));
|
276
|
+
*/
|
277
|
+
createReactionCollector(filter, options = {}) {
|
278
|
+
return new ReactionCollector(this, filter, options);
|
279
|
+
}
|
280
|
+
|
281
|
+
/**
|
282
|
+
* An object containing the same properties as CollectorOptions, but a few more:
|
283
|
+
* @typedef {ReactionCollectorOptions} AwaitReactionsOptions
|
284
|
+
* @property {string[]} [errors] Stop/end reasons that cause the promise to reject
|
285
|
+
*/
|
286
|
+
|
287
|
+
/**
|
288
|
+
* Similar to createMessageCollector but in promise form.
|
289
|
+
* Resolves with a collection of reactions that pass the specified filter.
|
290
|
+
* @param {CollectorFilter} filter The filter function to use
|
291
|
+
* @param {AwaitReactionsOptions} [options={}] Optional options to pass to the internal collector
|
292
|
+
* @returns {Promise<Collection<string, MessageReaction>>}
|
293
|
+
* @example
|
294
|
+
* // Create a reaction collector
|
295
|
+
* const filter = (reaction, user) => reaction.emoji.name === '👌' && user.id === 'someID'
|
296
|
+
* message.awaitReactions(filter, { time: 15000 })
|
297
|
+
* .then(collected => console.log(`Collected ${collected.size} reactions`))
|
298
|
+
* .catch(console.error);
|
299
|
+
*/
|
300
|
+
awaitReactions(filter, options = {}) {
|
301
|
+
return new Promise((resolve, reject) => {
|
302
|
+
const collector = this.createReactionCollector(filter, options);
|
303
|
+
collector.once('end', (reactions, reason) => {
|
304
|
+
if (options.errors && options.errors.includes(reason)) reject(reactions);
|
305
|
+
else resolve(reactions);
|
306
|
+
});
|
307
|
+
});
|
308
|
+
}
|
309
|
+
|
310
|
+
/**
|
311
|
+
* An array of cached versions of the message, including the current version
|
312
|
+
* Sorted from latest (first) to oldest (last)
|
313
|
+
* @type {Message[]}
|
314
|
+
* @readonly
|
315
|
+
*/
|
316
|
+
get edits() {
|
317
|
+
const copy = this._edits.slice();
|
318
|
+
copy.unshift(this);
|
319
|
+
return copy;
|
320
|
+
}
|
321
|
+
|
322
|
+
/**
|
323
|
+
* Whether the message is editable by the client user
|
324
|
+
* @type {boolean}
|
325
|
+
* @readonly
|
326
|
+
*/
|
327
|
+
get editable() {
|
328
|
+
return this.author.id === this.client.user.id;
|
329
|
+
}
|
330
|
+
|
331
|
+
/**
|
332
|
+
* Whether the message is deletable by the client user
|
333
|
+
* @type {boolean}
|
334
|
+
* @readonly
|
335
|
+
*/
|
336
|
+
get deletable() {
|
337
|
+
return !this.deleted && (this.author.id === this.client.user.id || (this.guild &&
|
338
|
+
this.channel.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_MESSAGES)
|
339
|
+
));
|
340
|
+
}
|
341
|
+
|
342
|
+
/**
|
343
|
+
* Whether the message is pinnable by the client user
|
344
|
+
* @type {boolean}
|
345
|
+
* @readonly
|
346
|
+
*/
|
347
|
+
get pinnable() {
|
348
|
+
return this.type === 'DEFAULT' && (!this.guild ||
|
349
|
+
this.channel.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_MESSAGES));
|
350
|
+
}
|
351
|
+
|
352
|
+
/**
|
353
|
+
* Whether or not a user, channel or role is mentioned in this message.
|
354
|
+
* @param {GuildChannel|User|Role|string} data Either a guild channel, user or a role object, or a string representing
|
355
|
+
* the ID of any of these
|
356
|
+
* @returns {boolean}
|
357
|
+
*/
|
358
|
+
isMentioned(data) {
|
359
|
+
data = data && data.id ? data.id : data;
|
360
|
+
return this.mentions.users.has(data) || this.mentions.channels.has(data) || this.mentions.roles.has(data);
|
361
|
+
}
|
362
|
+
|
363
|
+
/**
|
364
|
+
* Whether or not a guild member is mentioned in this message. Takes into account
|
365
|
+
* user mentions, role mentions, and @everyone/@here mentions.
|
366
|
+
* @param {GuildMember|User} member The member/user to check for a mention of
|
367
|
+
* @returns {boolean}
|
368
|
+
*/
|
369
|
+
isMemberMentioned(member) {
|
370
|
+
// Lazy-loading is used here to get around a circular dependency that breaks things
|
371
|
+
if (!GuildMember) GuildMember = require('./GuildMember');
|
372
|
+
if (this.mentions.everyone) return true;
|
373
|
+
if (this.mentions.users.has(member.id)) return true;
|
374
|
+
if (member instanceof GuildMember && member.roles.some(r => this.mentions.roles.has(r.id))) return true;
|
375
|
+
return false;
|
376
|
+
}
|
377
|
+
|
378
|
+
/**
|
379
|
+
* Options that can be passed into editMessage.
|
380
|
+
* @typedef {Object} MessageEditOptions
|
381
|
+
* @property {Object} [embed] An embed to be added/edited
|
382
|
+
* @property {string|boolean} [code] Language for optional codeblock formatting to apply
|
383
|
+
*/
|
384
|
+
|
385
|
+
/**
|
386
|
+
* Edit the content of the message.
|
387
|
+
* @param {StringResolvable} [content] The new content for the message
|
388
|
+
* @param {MessageEditOptions|RichEmbed} [options] The options to provide
|
389
|
+
* @returns {Promise<Message>}
|
390
|
+
* @example
|
391
|
+
* // Update the content of a message
|
392
|
+
* message.edit('This is my new content!')
|
393
|
+
* .then(msg => console.log(`New message content: ${msg}`))
|
394
|
+
* .catch(console.error);
|
395
|
+
*/
|
396
|
+
edit(content, options) {
|
397
|
+
if (!options && typeof content === 'object' && !(content instanceof Array)) {
|
398
|
+
options = content;
|
399
|
+
content = '';
|
400
|
+
} else if (!options) {
|
401
|
+
options = {};
|
402
|
+
}
|
403
|
+
if (options instanceof RichEmbed) options = { embed: options };
|
404
|
+
return this.client.rest.methods.updateMessage(this, content, options);
|
405
|
+
}
|
406
|
+
|
407
|
+
/**
|
408
|
+
* Edit the content of the message, with a code block.
|
409
|
+
* @param {string} lang The language for the code block
|
410
|
+
* @param {StringResolvable} content The new content for the message
|
411
|
+
* @returns {Promise<Message>}
|
412
|
+
* @deprecated
|
413
|
+
*/
|
414
|
+
editCode(lang, content) {
|
415
|
+
content = Util.escapeMarkdown(this.client.resolver.resolveString(content), true);
|
416
|
+
return this.edit(`\`\`\`${lang || ''}\n${content}\n\`\`\``);
|
417
|
+
}
|
418
|
+
|
419
|
+
/**
|
420
|
+
* Pins this message to the channel's pinned messages.
|
421
|
+
* @returns {Promise<Message>}
|
422
|
+
*/
|
423
|
+
pin() {
|
424
|
+
return this.client.rest.methods.pinMessage(this);
|
425
|
+
}
|
426
|
+
|
427
|
+
/**
|
428
|
+
* Unpins this message from the channel's pinned messages.
|
429
|
+
* @returns {Promise<Message>}
|
430
|
+
*/
|
431
|
+
unpin() {
|
432
|
+
return this.client.rest.methods.unpinMessage(this);
|
433
|
+
}
|
434
|
+
|
435
|
+
/**
|
436
|
+
* Add a reaction to the message.
|
437
|
+
* @param {string|Emoji|ReactionEmoji} emoji The emoji to react with
|
438
|
+
* @returns {Promise<MessageReaction>}
|
439
|
+
* @example
|
440
|
+
* // React to a message with a unicode emoji
|
441
|
+
* message.react('🤔')
|
442
|
+
* .then(console.log)
|
443
|
+
* .catch(console.error);
|
444
|
+
* @example
|
445
|
+
* // React to a message with a custom emoji
|
446
|
+
* message.react(message.guild.emojis.get('123456789012345678'))
|
447
|
+
* .then(console.log)
|
448
|
+
* .catch(console.error);
|
449
|
+
*/
|
450
|
+
react(emoji) {
|
451
|
+
emoji = this.client.resolver.resolveEmojiIdentifier(emoji);
|
452
|
+
if (!emoji) throw new TypeError('Emoji must be a string or Emoji/ReactionEmoji');
|
453
|
+
|
454
|
+
return this.client.rest.methods.addMessageReaction(this, emoji);
|
455
|
+
}
|
456
|
+
|
457
|
+
/**
|
458
|
+
* Remove all reactions from a message.
|
459
|
+
* @returns {Promise<Message>}
|
460
|
+
*/
|
461
|
+
clearReactions() {
|
462
|
+
return this.client.rest.methods.removeMessageReactions(this);
|
463
|
+
}
|
464
|
+
|
465
|
+
/**
|
466
|
+
* Deletes the message.
|
467
|
+
* @param {number} [timeout=0] How long to wait to delete the message in milliseconds
|
468
|
+
* @returns {Promise<Message>}
|
469
|
+
* @example
|
470
|
+
* // Delete a message
|
471
|
+
* message.delete()
|
472
|
+
* .then(msg => console.log(`Deleted message from ${msg.author.username}`))
|
473
|
+
* .catch(console.error);
|
474
|
+
*/
|
475
|
+
delete(timeout = 0) {
|
476
|
+
if (timeout <= 0) {
|
477
|
+
return this.client.rest.methods.deleteMessage(this);
|
478
|
+
} else {
|
479
|
+
return new Promise(resolve => {
|
480
|
+
this.client.setTimeout(() => {
|
481
|
+
resolve(this.delete());
|
482
|
+
}, timeout);
|
483
|
+
});
|
484
|
+
}
|
485
|
+
}
|
486
|
+
|
487
|
+
/**
|
488
|
+
* Reply to the message.
|
489
|
+
* @param {StringResolvable} [content] The content for the message
|
490
|
+
* @param {MessageOptions} [options] The options to provide
|
491
|
+
* @returns {Promise<Message|Message[]>}
|
492
|
+
* @example
|
493
|
+
* // Reply to a message
|
494
|
+
* message.reply('Hey, I\'m a reply!')
|
495
|
+
* .then(sent => console.log(`Sent a reply to ${sent.author.username}`))
|
496
|
+
* .catch(console.error);
|
497
|
+
*/
|
498
|
+
reply(content, options) {
|
499
|
+
if (!options && typeof content === 'object' && !(content instanceof Array)) {
|
500
|
+
options = content;
|
501
|
+
content = '';
|
502
|
+
} else if (!options) {
|
503
|
+
options = {};
|
504
|
+
}
|
505
|
+
return this.channel.send(content, Object.assign(options, { reply: this.member || this.author }));
|
506
|
+
}
|
507
|
+
|
508
|
+
/**
|
509
|
+
* Marks the message as read.
|
510
|
+
* <warn>This is only available when using a user account.</warn>
|
511
|
+
* @returns {Promise<Message>}
|
512
|
+
* @deprecated
|
513
|
+
*/
|
514
|
+
acknowledge() {
|
515
|
+
return this.client.rest.methods.ackMessage(this);
|
516
|
+
}
|
517
|
+
|
518
|
+
/**
|
519
|
+
* Fetches the webhook used to create this message.
|
520
|
+
* @returns {Promise<?Webhook>}
|
521
|
+
*/
|
522
|
+
fetchWebhook() {
|
523
|
+
if (!this.webhookID) return Promise.reject(new Error('The message was not sent by a webhook.'));
|
524
|
+
return this.client.fetchWebhook(this.webhookID);
|
525
|
+
}
|
526
|
+
|
527
|
+
/**
|
528
|
+
* Used mainly internally. Whether two messages are identical in properties. If you want to compare messages
|
529
|
+
* without checking all the properties, use `message.id === message2.id`, which is much more efficient. This
|
530
|
+
* method allows you to see if there are differences in content, embeds, attachments, nonce and tts properties.
|
531
|
+
* @param {Message} message The message to compare it to
|
532
|
+
* @param {Object} rawData Raw data passed through the WebSocket about this message
|
533
|
+
* @returns {boolean}
|
534
|
+
*/
|
535
|
+
equals(message, rawData) {
|
536
|
+
if (!message) return false;
|
537
|
+
const embedUpdate = !message.author && !message.attachments;
|
538
|
+
if (embedUpdate) return this.id === message.id && this.embeds.length === message.embeds.length;
|
539
|
+
|
540
|
+
let equal = this.id === message.id &&
|
541
|
+
this.author.id === message.author.id &&
|
542
|
+
this.content === message.content &&
|
543
|
+
this.tts === message.tts &&
|
544
|
+
this.nonce === message.nonce &&
|
545
|
+
this.embeds.length === message.embeds.length &&
|
546
|
+
this.attachments.length === message.attachments.length;
|
547
|
+
|
548
|
+
if (equal && rawData) {
|
549
|
+
equal = this.mentions.everyone === message.mentions.everyone &&
|
550
|
+
this.createdTimestamp === new Date(rawData.timestamp).getTime() &&
|
551
|
+
this.editedTimestamp === new Date(rawData.edited_timestamp).getTime();
|
552
|
+
}
|
553
|
+
|
554
|
+
return equal;
|
555
|
+
}
|
556
|
+
|
557
|
+
/**
|
558
|
+
* When concatenated with a string, this automatically concatenates the message's content instead of the object.
|
559
|
+
* @returns {string}
|
560
|
+
* @example
|
561
|
+
* // Logs: Message: This is a message!
|
562
|
+
* console.log(`Message: ${message}`);
|
563
|
+
*/
|
564
|
+
toString() {
|
565
|
+
return this.content;
|
566
|
+
}
|
567
|
+
|
568
|
+
_addReaction(emoji, user) {
|
569
|
+
const emojiID = emoji.id ? `${emoji.name}:${emoji.id}` : emoji.name;
|
570
|
+
let reaction;
|
571
|
+
if (this.reactions.has(emojiID)) {
|
572
|
+
reaction = this.reactions.get(emojiID);
|
573
|
+
if (!reaction.me) reaction.me = user.id === this.client.user.id;
|
574
|
+
} else {
|
575
|
+
reaction = new MessageReaction(this, emoji, 0, user.id === this.client.user.id);
|
576
|
+
this.reactions.set(emojiID, reaction);
|
577
|
+
}
|
578
|
+
if (!reaction.users.has(user.id)) {
|
579
|
+
reaction.users.set(user.id, user);
|
580
|
+
reaction.count++;
|
581
|
+
}
|
582
|
+
return reaction;
|
583
|
+
}
|
584
|
+
|
585
|
+
_removeReaction(emoji, user) {
|
586
|
+
const emojiID = emoji.id ? `${emoji.name}:${emoji.id}` : emoji.name;
|
587
|
+
if (this.reactions.has(emojiID)) {
|
588
|
+
const reaction = this.reactions.get(emojiID);
|
589
|
+
if (reaction.users.has(user.id)) {
|
590
|
+
reaction.users.delete(user.id);
|
591
|
+
reaction.count--;
|
592
|
+
if (user.id === this.client.user.id) reaction.me = false;
|
593
|
+
if (reaction.count <= 0) this.reactions.delete(emojiID);
|
594
|
+
return reaction;
|
595
|
+
}
|
596
|
+
}
|
597
|
+
return null;
|
598
|
+
}
|
599
|
+
|
600
|
+
_clearReactions() {
|
601
|
+
this.reactions.clear();
|
602
|
+
}
|
603
|
+
}
|
604
|
+
|
605
|
+
module.exports = Message;
|
@@ -0,0 +1,68 @@
|
|
1
|
+
/**
|
2
|
+
* Represents an attachment in a message.
|
3
|
+
*/
|
4
|
+
class MessageAttachment {
|
5
|
+
constructor(message, data) {
|
6
|
+
/**
|
7
|
+
* The client that instantiated this MessageAttachment
|
8
|
+
* @name MessageAttachment#client
|
9
|
+
* @type {Client}
|
10
|
+
* @readonly
|
11
|
+
*/
|
12
|
+
Object.defineProperty(this, 'client', { value: message.client });
|
13
|
+
|
14
|
+
/**
|
15
|
+
* The message this attachment is part of
|
16
|
+
* @type {Message}
|
17
|
+
*/
|
18
|
+
this.message = message;
|
19
|
+
|
20
|
+
this.setup(data);
|
21
|
+
}
|
22
|
+
|
23
|
+
setup(data) {
|
24
|
+
/**
|
25
|
+
* The ID of this attachment
|
26
|
+
* @type {Snowflake}
|
27
|
+
*/
|
28
|
+
this.id = data.id;
|
29
|
+
|
30
|
+
/**
|
31
|
+
* The file name of this attachment
|
32
|
+
* @type {string}
|
33
|
+
*/
|
34
|
+
this.filename = data.filename;
|
35
|
+
|
36
|
+
/**
|
37
|
+
* The size of this attachment in bytes
|
38
|
+
* @type {number}
|
39
|
+
*/
|
40
|
+
this.filesize = data.size;
|
41
|
+
|
42
|
+
/**
|
43
|
+
* The URL to this attachment
|
44
|
+
* @type {string}
|
45
|
+
*/
|
46
|
+
this.url = data.url;
|
47
|
+
|
48
|
+
/**
|
49
|
+
* The Proxy URL to this attachment
|
50
|
+
* @type {string}
|
51
|
+
*/
|
52
|
+
this.proxyURL = data.proxy_url;
|
53
|
+
|
54
|
+
/**
|
55
|
+
* The height of this attachment (if an image)
|
56
|
+
* @type {?number}
|
57
|
+
*/
|
58
|
+
this.height = data.height;
|
59
|
+
|
60
|
+
/**
|
61
|
+
* The width of this attachment (if an image)
|
62
|
+
* @type {?number}
|
63
|
+
*/
|
64
|
+
this.width = data.width;
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
module.exports = MessageAttachment;
|