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.

Files changed (188) hide show
  1. package/.dccache +1 -0
  2. package/LICENSE +190 -0
  3. package/README.md +87 -0
  4. package/browser.js +9 -0
  5. package/deploy/deploy-key.enc +0 -0
  6. package/deploy/deploy.sh +90 -0
  7. package/deploy/test.sh +34 -0
  8. package/docs/README.md +1 -0
  9. package/docs/examples/attachments.md +163 -0
  10. package/docs/examples/avatars.js +29 -0
  11. package/docs/examples/embed.js +38 -0
  12. package/docs/examples/greeting.js +30 -0
  13. package/docs/examples/moderation.md +145 -0
  14. package/docs/examples/ping.js +29 -0
  15. package/docs/examples/webhook.js +12 -0
  16. package/docs/general/faq.md +23 -0
  17. package/docs/general/updating.md +181 -0
  18. package/docs/general/welcome.md +95 -0
  19. package/docs/index.yml +30 -0
  20. package/docs/logo.svg +19 -0
  21. package/docs/topics/voice.md +113 -0
  22. package/docs/topics/web.md +38 -0
  23. package/package.json +147 -0
  24. package/src/client/Client.js +564 -0
  25. package/src/client/ClientDataManager.js +150 -0
  26. package/src/client/ClientDataResolver.js +376 -0
  27. package/src/client/ClientManager.js +74 -0
  28. package/src/client/WebhookClient.js +118 -0
  29. package/src/client/actions/Action.js +23 -0
  30. package/src/client/actions/ActionsManager.js +40 -0
  31. package/src/client/actions/ChannelCreate.js +11 -0
  32. package/src/client/actions/ChannelDelete.js +30 -0
  33. package/src/client/actions/ChannelUpdate.js +74 -0
  34. package/src/client/actions/GuildBanRemove.js +13 -0
  35. package/src/client/actions/GuildChannelsPositionUpdate.js +19 -0
  36. package/src/client/actions/GuildDelete.js +57 -0
  37. package/src/client/actions/GuildEmojiCreate.js +17 -0
  38. package/src/client/actions/GuildEmojiDelete.js +18 -0
  39. package/src/client/actions/GuildEmojiUpdate.js +17 -0
  40. package/src/client/actions/GuildEmojisUpdate.js +38 -0
  41. package/src/client/actions/GuildMemberGet.js +10 -0
  42. package/src/client/actions/GuildMemberRemove.js +41 -0
  43. package/src/client/actions/GuildRoleCreate.js +26 -0
  44. package/src/client/actions/GuildRoleDelete.js +42 -0
  45. package/src/client/actions/GuildRoleUpdate.js +41 -0
  46. package/src/client/actions/GuildRolesPositionUpdate.js +19 -0
  47. package/src/client/actions/GuildSync.js +29 -0
  48. package/src/client/actions/GuildUpdate.js +34 -0
  49. package/src/client/actions/MessageCreate.js +53 -0
  50. package/src/client/actions/MessageDelete.js +35 -0
  51. package/src/client/actions/MessageDeleteBulk.js +26 -0
  52. package/src/client/actions/MessageReactionAdd.js +37 -0
  53. package/src/client/actions/MessageReactionRemove.js +37 -0
  54. package/src/client/actions/MessageReactionRemoveAll.js +25 -0
  55. package/src/client/actions/MessageUpdate.js +40 -0
  56. package/src/client/actions/Ready.js +1 -0
  57. package/src/client/actions/Ready.js.bak +65 -0
  58. package/src/client/actions/UserGet.js +11 -0
  59. package/src/client/actions/UserNoteUpdate.js +30 -0
  60. package/src/client/actions/UserUpdate.js +33 -0
  61. package/src/client/rest/APIRequest.js +56 -0
  62. package/src/client/rest/DiscordAPIError.js +60 -0
  63. package/src/client/rest/RESTManager.js +58 -0
  64. package/src/client/rest/RESTMethods.js +1006 -0
  65. package/src/client/rest/RequestHandlers/Burst.js +90 -0
  66. package/src/client/rest/RequestHandlers/RequestHandler.js +54 -0
  67. package/src/client/rest/RequestHandlers/Sequential.js +132 -0
  68. package/src/client/rest/UserAgentManager.js +25 -0
  69. package/src/client/voice/ClientVoiceManager.js +81 -0
  70. package/src/client/voice/VoiceBroadcast.js +366 -0
  71. package/src/client/voice/VoiceConnection.js +530 -0
  72. package/src/client/voice/VoiceUDPClient.js +127 -0
  73. package/src/client/voice/VoiceWebSocket.js +246 -0
  74. package/src/client/voice/dispatcher/StreamDispatcher.js +331 -0
  75. package/src/client/voice/opus/BaseOpusEngine.js +60 -0
  76. package/src/client/voice/opus/NodeOpusEngine.js +40 -0
  77. package/src/client/voice/opus/OpusEngineList.js +28 -0
  78. package/src/client/voice/opus/OpusScriptEngine.js +45 -0
  79. package/src/client/voice/player/AudioPlayer.js +170 -0
  80. package/src/client/voice/receiver/VoiceReadable.js +17 -0
  81. package/src/client/voice/receiver/VoiceReceiver.js +219 -0
  82. package/src/client/voice/util/SecretKey.js +16 -0
  83. package/src/client/voice/util/Secretbox.js +33 -0
  84. package/src/client/voice/util/VolumeInterface.js +86 -0
  85. package/src/client/websocket/WebSocketConnection.js +506 -0
  86. package/src/client/websocket/WebSocketManager.js +90 -0
  87. package/src/client/websocket/packets/WebSocketPacketManager.js +110 -0
  88. package/src/client/websocket/packets/handlers/AbstractHandler.js +11 -0
  89. package/src/client/websocket/packets/handlers/ChannelCreate.js +17 -0
  90. package/src/client/websocket/packets/handlers/ChannelDelete.js +20 -0
  91. package/src/client/websocket/packets/handlers/ChannelPinsUpdate.js +37 -0
  92. package/src/client/websocket/packets/handlers/ChannelUpdate.js +11 -0
  93. package/src/client/websocket/packets/handlers/GuildBanAdd.js +23 -0
  94. package/src/client/websocket/packets/handlers/GuildBanRemove.js +20 -0
  95. package/src/client/websocket/packets/handlers/GuildCreate.js +22 -0
  96. package/src/client/websocket/packets/handlers/GuildDelete.js +19 -0
  97. package/src/client/websocket/packets/handlers/GuildEmojisUpdate.js +11 -0
  98. package/src/client/websocket/packets/handlers/GuildIntegrationsUpdate.js +19 -0
  99. package/src/client/websocket/packets/handlers/GuildMemberAdd.js +17 -0
  100. package/src/client/websocket/packets/handlers/GuildMemberRemove.js +13 -0
  101. package/src/client/websocket/packets/handlers/GuildMemberUpdate.js +18 -0
  102. package/src/client/websocket/packets/handlers/GuildMembersChunk.js +33 -0
  103. package/src/client/websocket/packets/handlers/GuildRoleCreate.js +11 -0
  104. package/src/client/websocket/packets/handlers/GuildRoleDelete.js +11 -0
  105. package/src/client/websocket/packets/handlers/GuildRoleUpdate.js +11 -0
  106. package/src/client/websocket/packets/handlers/GuildSync.js +11 -0
  107. package/src/client/websocket/packets/handlers/GuildUpdate.js +11 -0
  108. package/src/client/websocket/packets/handlers/MessageCreate.js +19 -0
  109. package/src/client/websocket/packets/handlers/MessageDelete.js +19 -0
  110. package/src/client/websocket/packets/handlers/MessageDeleteBulk.js +17 -0
  111. package/src/client/websocket/packets/handlers/MessageReactionAdd.js +11 -0
  112. package/src/client/websocket/packets/handlers/MessageReactionRemove.js +11 -0
  113. package/src/client/websocket/packets/handlers/MessageReactionRemoveAll.js +11 -0
  114. package/src/client/websocket/packets/handlers/MessageUpdate.js +11 -0
  115. package/src/client/websocket/packets/handlers/PresenceUpdate.js +76 -0
  116. package/src/client/websocket/packets/handlers/Ready.js +83 -0
  117. package/src/client/websocket/packets/handlers/RelationshipAdd.js +19 -0
  118. package/src/client/websocket/packets/handlers/RelationshipRemove.js +19 -0
  119. package/src/client/websocket/packets/handlers/Resumed.js +28 -0
  120. package/src/client/websocket/packets/handlers/TypingStart.js +68 -0
  121. package/src/client/websocket/packets/handlers/UserGuildSettingsUpdate.js +21 -0
  122. package/src/client/websocket/packets/handlers/UserNoteUpdate.js +12 -0
  123. package/src/client/websocket/packets/handlers/UserSettingsUpdate.js +18 -0
  124. package/src/client/websocket/packets/handlers/UserUpdate.js +11 -0
  125. package/src/client/websocket/packets/handlers/VoiceServerUpdate.js +19 -0
  126. package/src/client/websocket/packets/handlers/VoiceStateUpdate.js +52 -0
  127. package/src/client/websocket/packets/handlers/WebhooksUpdate.js +19 -0
  128. package/src/index.js +66 -0
  129. package/src/sharding/Shard.js +282 -0
  130. package/src/sharding/ShardClientUtil.js +146 -0
  131. package/src/sharding/ShardingManager.js +220 -0
  132. package/src/structures/Attachment.js +75 -0
  133. package/src/structures/CategoryChannel.js +22 -0
  134. package/src/structures/Channel.js +78 -0
  135. package/src/structures/ClientUser.js +447 -0
  136. package/src/structures/ClientUserChannelOverride.js +30 -0
  137. package/src/structures/ClientUserGuildSettings.js +60 -0
  138. package/src/structures/ClientUserSettings.js +80 -0
  139. package/src/structures/DMChannel.js +76 -0
  140. package/src/structures/Emoji.js +256 -0
  141. package/src/structures/GroupDMChannel.js +246 -0
  142. package/src/structures/Guild.js +1461 -0
  143. package/src/structures/GuildAuditLogs.js +371 -0
  144. package/src/structures/GuildChannel.js +537 -0
  145. package/src/structures/GuildMember.js +613 -0
  146. package/src/structures/Invite.js +164 -0
  147. package/src/structures/Message.js +605 -0
  148. package/src/structures/MessageAttachment.js +68 -0
  149. package/src/structures/MessageCollector.js +100 -0
  150. package/src/structures/MessageEmbed.js +386 -0
  151. package/src/structures/MessageMentions.js +144 -0
  152. package/src/structures/MessageReaction.js +96 -0
  153. package/src/structures/NewsChannel.js +24 -0
  154. package/src/structures/OAuth2Application.js +148 -0
  155. package/src/structures/PartialGuild.js +51 -0
  156. package/src/structures/PartialGuildChannel.js +44 -0
  157. package/src/structures/PermissionOverwrites.js +69 -0
  158. package/src/structures/Presence.js +241 -0
  159. package/src/structures/ReactionCollector.js +85 -0
  160. package/src/structures/ReactionEmoji.js +49 -0
  161. package/src/structures/RichEmbed.js +295 -0
  162. package/src/structures/Role.js +376 -0
  163. package/src/structures/StoreChannel.js +25 -0
  164. package/src/structures/TextChannel.js +154 -0
  165. package/src/structures/User.js +329 -0
  166. package/src/structures/UserConnection.js +48 -0
  167. package/src/structures/UserProfile.js +62 -0
  168. package/src/structures/VoiceChannel.js +146 -0
  169. package/src/structures/VoiceRegion.js +50 -0
  170. package/src/structures/Webhook.js +304 -0
  171. package/src/structures/interfaces/Collector.js +179 -0
  172. package/src/structures/interfaces/TextBasedChannel.js +635 -0
  173. package/src/structures/shared/resolvePermissions.js +26 -0
  174. package/src/util/Collection.js +532 -0
  175. package/src/util/Constants.js +845 -0
  176. package/src/util/Permissions.js +306 -0
  177. package/src/util/Snowflake.js +82 -0
  178. package/src/util/Util.js +221 -0
  179. package/test/random.js +207 -0
  180. package/test/shard.js +31 -0
  181. package/test/sharder.js +7 -0
  182. package/test/voice.js +78 -0
  183. package/test/webpack.html +31 -0
  184. package/tsconfig.json +13 -0
  185. package/tslint.json +62 -0
  186. package/typings/discord.js-test.ts +69 -0
  187. package/typings/index.d.ts +2190 -0
  188. package/webpack.config.js +62 -0
@@ -0,0 +1,376 @@
1
+ const path = require('path');
2
+ const fs = require('fs');
3
+ const snekfetch = require('snekfetch');
4
+
5
+ const Constants = require('../util/Constants');
6
+ const convertToBuffer = require('../util/Util').convertToBuffer;
7
+ const User = require('../structures/User');
8
+ const Message = require('../structures/Message');
9
+ const Guild = require('../structures/Guild');
10
+ const Channel = require('../structures/Channel');
11
+ const GuildMember = require('../structures/GuildMember');
12
+ const Emoji = require('../structures/Emoji');
13
+ const ReactionEmoji = require('../structures/ReactionEmoji');
14
+ const Role = require('../structures/Role');
15
+
16
+ /**
17
+ * The DataResolver identifies different objects and tries to resolve a specific piece of information from them, e.g.
18
+ * extracting a User from a Message object.
19
+ * @private
20
+ */
21
+ class ClientDataResolver {
22
+ /**
23
+ * @param {Client} client The client the resolver is for
24
+ */
25
+ constructor(client) {
26
+ this.client = client;
27
+ }
28
+
29
+ /**
30
+ * Data that resolves to give a User object. This can be:
31
+ * * A User object
32
+ * * A Snowflake
33
+ * * A Message object (resolves to the message author)
34
+ * * A Guild object (owner of the guild)
35
+ * * A GuildMember object
36
+ * @typedef {User|Snowflake|Message|Guild|GuildMember} UserResolvable
37
+ */
38
+
39
+ /**
40
+ * Resolves a UserResolvable to a User object.
41
+ * @param {UserResolvable} user The UserResolvable to identify
42
+ * @returns {?User}
43
+ */
44
+ resolveUser(user) {
45
+ if (user instanceof User) return user;
46
+ if (typeof user === 'string') return this.client.users.get(user) || null;
47
+ if (user instanceof GuildMember) return user.user;
48
+ if (user instanceof Message) return user.author;
49
+ if (user instanceof Guild) return user.owner;
50
+ return null;
51
+ }
52
+
53
+ /**
54
+ * Resolves a UserResolvable to a user ID string.
55
+ * @param {UserResolvable} user The UserResolvable to identify
56
+ * @returns {?Snowflake}
57
+ */
58
+ resolveUserID(user) {
59
+ if (user instanceof User || user instanceof GuildMember) return user.id;
60
+ if (typeof user === 'string') return user || null;
61
+ if (user instanceof Message) return user.author.id;
62
+ if (user instanceof Guild) return user.ownerID;
63
+ return null;
64
+ }
65
+
66
+ /**
67
+ * Data that resolves to give a Guild object. This can be:
68
+ * * A Guild object
69
+ * * A Snowflake
70
+ * @typedef {Guild|Snowflake} GuildResolvable
71
+ */
72
+
73
+ /**
74
+ * Resolves a GuildResolvable to a Guild object.
75
+ * @param {GuildResolvable} guild The GuildResolvable to identify
76
+ * @returns {?Guild}
77
+ */
78
+ resolveGuild(guild) {
79
+ if (guild instanceof Guild) return guild;
80
+ if (typeof guild === 'string') return this.client.guilds.get(guild) || null;
81
+ return null;
82
+ }
83
+
84
+ /**
85
+ * Data that resolves to give a GuildMember object. This can be:
86
+ * * A GuildMember object
87
+ * * A User object
88
+ * @typedef {GuildMember|User} GuildMemberResolvable
89
+ */
90
+
91
+ /**
92
+ * Resolves a GuildMemberResolvable to a GuildMember object.
93
+ * @param {GuildResolvable} guild The guild that the member is part of
94
+ * @param {UserResolvable} user The user that is part of the guild
95
+ * @returns {?GuildMember}
96
+ */
97
+ resolveGuildMember(guild, user) {
98
+ if (user instanceof GuildMember) return user;
99
+ guild = this.resolveGuild(guild);
100
+ user = this.resolveUser(user);
101
+ if (!guild || !user) return null;
102
+ return guild.members.get(user.id) || null;
103
+ }
104
+
105
+ /**
106
+ * Data that can be resolved to a Role object. This can be:
107
+ * * A Role
108
+ * * A Snowflake
109
+ * @typedef {Role|Snowflake} RoleResolvable
110
+ */
111
+
112
+ /**
113
+ * Resolves a RoleResolvable to a Role object.
114
+ * @param {GuildResolvable} guild The guild that this role is part of
115
+ * @param {RoleResolvable} role The role resolvable to resolve
116
+ * @returns {?Role}
117
+ */
118
+ resolveRole(guild, role) {
119
+ if (role instanceof Role) return role;
120
+ guild = this.resolveGuild(guild);
121
+ if (!guild) return null;
122
+ if (typeof role === 'string') return guild.roles.get(role);
123
+ return null;
124
+ }
125
+
126
+ /**
127
+ * Data that can be resolved to give a Channel object. This can be:
128
+ * * A Channel object
129
+ * * A Message object (the channel the message was sent in)
130
+ * * A Guild object (the #general channel)
131
+ * * A Snowflake
132
+ * @typedef {Channel|Guild|Message|Snowflake} ChannelResolvable
133
+ */
134
+
135
+ /**
136
+ * Resolves a ChannelResolvable to a Channel object.
137
+ * @param {ChannelResolvable} channel The channel resolvable to resolve
138
+ * @returns {?Channel}
139
+ */
140
+ resolveChannel(channel) {
141
+ if (channel instanceof Channel) return channel;
142
+ if (typeof channel === 'string') return this.client.channels.get(channel) || null;
143
+ if (channel instanceof Message) return channel.channel;
144
+ if (channel instanceof Guild) return channel.channels.get(channel.id) || null;
145
+ return null;
146
+ }
147
+
148
+ /**
149
+ * Resolves a ChannelResolvable to a channel ID.
150
+ * @param {ChannelResolvable} channel The channel resolvable to resolve
151
+ * @returns {?Snowflake}
152
+ */
153
+ resolveChannelID(channel) {
154
+ if (channel instanceof Channel) return channel.id;
155
+ if (typeof channel === 'string') return channel;
156
+ if (channel instanceof Message) return channel.channel.id;
157
+ if (channel instanceof Guild) return channel.defaultChannel.id;
158
+ return null;
159
+ }
160
+
161
+ /**
162
+ * Data that can be resolved to give an invite code. This can be:
163
+ * * An invite code
164
+ * * An invite URL
165
+ * @typedef {string} InviteResolvable
166
+ */
167
+
168
+ /**
169
+ * Resolves InviteResolvable to an invite code.
170
+ * @param {InviteResolvable} data The invite resolvable to resolve
171
+ * @returns {string}
172
+ */
173
+ resolveInviteCode(data) {
174
+ const inviteRegex = /discord(?:app\.com\/invite|\.gg(?:\/invite)?)\/([\w-]{2,255})/i;
175
+ const match = inviteRegex.exec(data);
176
+ if (match && match[1]) return match[1];
177
+ return data;
178
+ }
179
+
180
+ /**
181
+ * Data that can be resolved to give a string. This can be:
182
+ * * A string
183
+ * * An array (joined with a new line delimiter to give a string)
184
+ * * Any value
185
+ * @typedef {string|Array|*} StringResolvable
186
+ */
187
+
188
+ /**
189
+ * Resolves a StringResolvable to a string.
190
+ * @param {StringResolvable} data The string resolvable to resolve
191
+ * @returns {string}
192
+ */
193
+ resolveString(data) {
194
+ if (typeof data === 'string') return data;
195
+ if (data instanceof Array) return data.join('\n');
196
+ return String(data);
197
+ }
198
+
199
+
200
+ /**
201
+ * Resolves a Base64Resolvable, a string, or a BufferResolvable to a Base 64 image.
202
+ * @param {BufferResolvable|Base64Resolvable} image The image to be resolved
203
+ * @returns {Promise<?string>}
204
+ */
205
+ resolveImage(image) {
206
+ if (!image) return Promise.resolve(null);
207
+ if (typeof image === 'string' && image.startsWith('data:')) {
208
+ return Promise.resolve(image);
209
+ }
210
+ return this.resolveFile(image).then(this.resolveBase64);
211
+ }
212
+
213
+ /**
214
+ * Data that resolves to give a Base64 string, typically for image uploading. This can be:
215
+ * * A Buffer
216
+ * * A base64 string
217
+ * @typedef {Buffer|string} Base64Resolvable
218
+ */
219
+
220
+ /**
221
+ * Resolves a Base64Resolvable to a Base 64 image.
222
+ * @param {Base64Resolvable} data The base 64 resolvable you want to resolve
223
+ * @returns {?string}
224
+ */
225
+ resolveBase64(data) {
226
+ if (data instanceof Buffer) return `data:image/jpg;base64,${data.toString('base64')}`;
227
+ return data;
228
+ }
229
+
230
+ /**
231
+ * Data that can be resolved to give a Buffer. This can be:
232
+ * * A Buffer
233
+ * * The path to a local file
234
+ * * A URL
235
+ * * A Stream
236
+ * @typedef {string|Buffer} BufferResolvable
237
+ */
238
+
239
+ /**
240
+ * @external Stream
241
+ * @see {@link https://nodejs.org/api/stream.html}
242
+ */
243
+
244
+ /**
245
+ * Resolves a BufferResolvable to a Buffer.
246
+ * @param {BufferResolvable|Stream} resource The buffer or stream resolvable to resolve
247
+ * @returns {Promise<Buffer>}
248
+ */
249
+ resolveFile(resource) {
250
+ if (resource instanceof Buffer) return Promise.resolve(resource);
251
+ if (this.client.browser && resource instanceof ArrayBuffer) return Promise.resolve(convertToBuffer(resource));
252
+
253
+ if (typeof resource === 'string') {
254
+ if (/^https?:\/\//.test(resource)) {
255
+ return snekfetch.get(resource).then(res => res.body instanceof Buffer ? res.body : Buffer.from(res.text));
256
+ }
257
+ return new Promise((resolve, reject) => {
258
+ const file = path.resolve(resource);
259
+ fs.stat(file, (err, stats) => {
260
+ if (err) return reject(err);
261
+ if (!stats || !stats.isFile()) return reject(new Error(`The file could not be found: ${file}`));
262
+ fs.readFile(file, (err2, data) => {
263
+ if (err2) reject(err2);
264
+ else resolve(data);
265
+ });
266
+ return null;
267
+ });
268
+ });
269
+ } else if (resource && resource.pipe && typeof resource.pipe === 'function') {
270
+ return new Promise((resolve, reject) => {
271
+ const buffers = [];
272
+ resource.once('error', reject);
273
+ resource.on('data', data => buffers.push(data));
274
+ resource.once('end', () => resolve(Buffer.concat(buffers)));
275
+ });
276
+ }
277
+
278
+ return Promise.reject(new TypeError('The resource must be a string or Buffer.'));
279
+ }
280
+
281
+ /**
282
+ * Data that can be resolved to give an emoji identifier. This can be:
283
+ * * The unicode representation of an emoji
284
+ * * A custom emoji ID
285
+ * * An Emoji object
286
+ * * A ReactionEmoji object
287
+ * @typedef {string|Emoji|ReactionEmoji} EmojiIdentifierResolvable
288
+ */
289
+
290
+ /**
291
+ * Resolves an EmojiResolvable to an emoji identifier.
292
+ * @param {EmojiIdentifierResolvable} emoji The emoji resolvable to resolve
293
+ * @returns {?string}
294
+ */
295
+ resolveEmojiIdentifier(emoji) {
296
+ if (emoji instanceof Emoji || emoji instanceof ReactionEmoji) return emoji.identifier;
297
+ if (typeof emoji === 'string') {
298
+ if (this.client.emojis.has(emoji)) return this.client.emojis.get(emoji).identifier;
299
+ else if (!emoji.includes('%')) return encodeURIComponent(emoji);
300
+ else return emoji;
301
+ }
302
+ return null;
303
+ }
304
+
305
+ /**
306
+ * Can be a Hex Literal, Hex String, Number, RGB Array, or one of the following
307
+ * ```
308
+ * [
309
+ * 'DEFAULT',
310
+ * 'WHITE',
311
+ * 'AQUA',
312
+ * 'GREEN',
313
+ * 'BLUE',
314
+ * 'PURPLE',
315
+ * 'LUMINOUS_VIVID_PINK',
316
+ * 'GOLD',
317
+ * 'ORANGE',
318
+ * 'RED',
319
+ * 'GREY',
320
+ * 'DARKER_GREY',
321
+ * 'NAVY',
322
+ * 'DARK_AQUA',
323
+ * 'DARK_GREEN',
324
+ * 'DARK_BLUE',
325
+ * 'DARK_PURPLE',
326
+ * 'DARK_VIVID_PINK',
327
+ * 'DARK_GOLD',
328
+ * 'DARK_ORANGE',
329
+ * 'DARK_RED',
330
+ * 'DARK_GREY',
331
+ * 'LIGHT_GREY',
332
+ * 'DARK_NAVY',
333
+ * 'RANDOM',
334
+ * ]
335
+ * ```
336
+ * or something like
337
+ * ```
338
+ * [255, 0, 255]
339
+ * ```
340
+ * for purple
341
+ * @typedef {string|number|Array} ColorResolvable
342
+ */
343
+
344
+ /**
345
+ * Resolves a ColorResolvable into a color number.
346
+ * @param {ColorResolvable} color Color to resolve
347
+ * @returns {number} A color
348
+ */
349
+ static resolveColor(color) {
350
+ if (typeof color === 'string') {
351
+ if (color === 'RANDOM') return Math.floor(Math.random() * (0xFFFFFF + 1));
352
+ if (color === 'DEFAULT') return 0;
353
+ color = Constants.Colors[color] || parseInt(color.replace('#', ''), 16);
354
+ } else if (color instanceof Array) {
355
+ color = (color[0] << 16) + (color[1] << 8) + color[2];
356
+ }
357
+
358
+ if (color < 0 || color > 0xFFFFFF) {
359
+ throw new RangeError('Color must be within the range 0 - 16777215 (0xFFFFFF).');
360
+ } else if (color && isNaN(color)) {
361
+ throw new TypeError('Unable to convert color to a number.');
362
+ }
363
+
364
+ return color;
365
+ }
366
+
367
+ /**
368
+ * @param {ColorResolvable} color Color to resolve
369
+ * @returns {number} A color
370
+ */
371
+ resolveColor(color) {
372
+ return this.constructor.resolveColor(color);
373
+ }
374
+ }
375
+
376
+ module.exports = ClientDataResolver;
@@ -0,0 +1,74 @@
1
+ const Constants = require('../util/Constants');
2
+ const WebSocketConnection = require('./websocket/WebSocketConnection');
3
+
4
+ /**
5
+ * Manages the state and background tasks of the client.
6
+ * @private
7
+ */
8
+ class ClientManager {
9
+ constructor(client) {
10
+ /**
11
+ * The client that instantiated this Manager
12
+ * @type {Client}
13
+ */
14
+ this.client = client;
15
+
16
+ /**
17
+ * The heartbeat interval
18
+ * @type {?number}
19
+ */
20
+ this.heartbeatInterval = null;
21
+ }
22
+
23
+ /**
24
+ * The status of the client
25
+ * @type {number}
26
+ */
27
+ get status() {
28
+ return this.connection ? this.connection.status : Constants.Status.IDLE;
29
+ }
30
+
31
+ /**
32
+ * Connects the client to the WebSocket.
33
+ * @param {string} token The authorization token
34
+ * @param {Function} resolve Function to run when connection is successful
35
+ * @param {Function} reject Function to run when connection fails
36
+ */
37
+ connectToWebSocket(token, resolve, reject) {
38
+ this.client.emit(Constants.Events.DEBUG, `Authenticated using token ${token}`);
39
+ this.client.token = token;
40
+ const timeout = this.client.setTimeout(() => reject(new Error(Constants.Errors.TOOK_TOO_LONG)), 1000 * 300);
41
+ this.client.rest.methods.getGateway().then(res => {
42
+ const protocolVersion = Constants.DefaultOptions.ws.version;
43
+ const gateway = `${res.url}/?v=${protocolVersion}&encoding=${WebSocketConnection.ENCODING}`;
44
+ this.client.emit(Constants.Events.DEBUG, `Using gateway ${gateway}`);
45
+ this.client.ws.connect(gateway);
46
+ this.client.ws.connection.once('error', reject);
47
+ this.client.ws.connection.once('close', event => {
48
+ if (event.code === 4004) reject(new Error(Constants.Errors.BAD_LOGIN));
49
+ if (event.code === 4010) reject(new Error(Constants.Errors.INVALID_SHARD));
50
+ if (event.code === 4011) reject(new Error(Constants.Errors.SHARDING_REQUIRED));
51
+ });
52
+ this.client.once(Constants.Events.READY, () => {
53
+ resolve(token);
54
+ this.client.clearTimeout(timeout);
55
+ });
56
+ }, reject);
57
+ }
58
+
59
+ destroy() {
60
+ this.client.ws.destroy();
61
+ this.client.rest.destroy();
62
+ if (!this.client.user) return Promise.resolve();
63
+ if (this.client.user.bot) {
64
+ this.client.token = null;
65
+ return Promise.resolve();
66
+ } else {
67
+ return this.client.rest.methods.logout().then(() => {
68
+ this.client.token = null;
69
+ });
70
+ }
71
+ }
72
+ }
73
+
74
+ module.exports = ClientManager;
@@ -0,0 +1,118 @@
1
+ const Webhook = require('../structures/Webhook');
2
+ const RESTManager = require('./rest/RESTManager');
3
+ const ClientDataResolver = require('./ClientDataResolver');
4
+ const Constants = require('../util/Constants');
5
+ const Util = require('../util/Util');
6
+
7
+ /**
8
+ * The webhook client.
9
+ * @extends {Webhook}
10
+ */
11
+ class WebhookClient extends Webhook {
12
+ /**
13
+ * @param {Snowflake} id ID of the webhook
14
+ * @param {string} token Token of the webhook
15
+ * @param {ClientOptions} [options] Options for the client
16
+ * @example
17
+ * // Create a new webhook and send a message
18
+ * const hook = new Discord.WebhookClient('1234', 'abcdef');
19
+ * hook.sendMessage('This will send a message').catch(console.error);
20
+ */
21
+ constructor(id, token, options) {
22
+ super(null, id, token);
23
+
24
+ /**
25
+ * The options the client was instantiated with
26
+ * @type {ClientOptions}
27
+ */
28
+ this.options = Util.mergeDefault(Constants.DefaultOptions, options);
29
+
30
+ /**
31
+ * The REST manager of the client
32
+ * @type {RESTManager}
33
+ * @private
34
+ */
35
+ this.rest = new RESTManager(this);
36
+
37
+ /**
38
+ * The data resolver of the client
39
+ * @type {ClientDataResolver}
40
+ * @private
41
+ */
42
+ this.resolver = new ClientDataResolver(this);
43
+
44
+ /**
45
+ * Timeouts set by {@link WebhookClient#setTimeout} that are still active
46
+ * @type {Set<Timeout>}
47
+ * @private
48
+ */
49
+ this._timeouts = new Set();
50
+
51
+ /**
52
+ * Intervals set by {@link WebhookClient#setInterval} that are still active
53
+ * @type {Set<Timeout>}
54
+ * @private
55
+ */
56
+ this._intervals = new Set();
57
+ }
58
+
59
+ /**
60
+ * Sets a timeout that will be automatically cancelled if the client is destroyed.
61
+ * @param {Function} fn Function to execute
62
+ * @param {number} delay Time to wait before executing (in milliseconds)
63
+ * @param {...*} args Arguments for the function
64
+ * @returns {Timeout}
65
+ */
66
+ setTimeout(fn, delay, ...args) {
67
+ const timeout = setTimeout(() => {
68
+ fn(...args);
69
+ this._timeouts.delete(timeout);
70
+ }, delay);
71
+ this._timeouts.add(timeout);
72
+ return timeout;
73
+ }
74
+
75
+ /**
76
+ * Clears a timeout.
77
+ * @param {Timeout} timeout Timeout to cancel
78
+ */
79
+ clearTimeout(timeout) {
80
+ clearTimeout(timeout);
81
+ this._timeouts.delete(timeout);
82
+ }
83
+
84
+ /**
85
+ * Sets an interval that will be automatically cancelled if the client is destroyed.
86
+ * @param {Function} fn Function to execute
87
+ * @param {number} delay Time to wait before executing (in milliseconds)
88
+ * @param {...*} args Arguments for the function
89
+ * @returns {Timeout}
90
+ */
91
+ setInterval(fn, delay, ...args) {
92
+ const interval = setInterval(fn, delay, ...args);
93
+ this._intervals.add(interval);
94
+ return interval;
95
+ }
96
+
97
+ /**
98
+ * Clears an interval.
99
+ * @param {Timeout} interval Interval to cancel
100
+ */
101
+ clearInterval(interval) {
102
+ clearInterval(interval);
103
+ this._intervals.delete(interval);
104
+ }
105
+
106
+
107
+ /**
108
+ * Destroys the client.
109
+ */
110
+ destroy() {
111
+ for (const t of this._timeouts) clearTimeout(t);
112
+ for (const i of this._intervals) clearInterval(i);
113
+ this._timeouts.clear();
114
+ this._intervals.clear();
115
+ }
116
+ }
117
+
118
+ module.exports = WebhookClient;
@@ -0,0 +1,23 @@
1
+ /*
2
+
3
+ ABOUT ACTIONS
4
+
5
+ Actions are similar to WebSocket Packet Handlers, but since introducing
6
+ the REST API methods, in order to prevent rewriting code to handle data,
7
+ "actions" have been introduced. They're basically what Packet Handlers
8
+ used to be but they're strictly for manipulating data and making sure
9
+ that WebSocket events don't clash with REST methods.
10
+
11
+ */
12
+
13
+ class GenericAction {
14
+ constructor(client) {
15
+ this.client = client;
16
+ }
17
+
18
+ handle(data) {
19
+ return data;
20
+ }
21
+ }
22
+
23
+ module.exports = GenericAction;
@@ -0,0 +1,40 @@
1
+ class ActionsManager {
2
+ constructor(client) {
3
+ this.client = client;
4
+
5
+ this.register(require('./MessageCreate'));
6
+ this.register(require('./MessageDelete'));
7
+ this.register(require('./MessageDeleteBulk'));
8
+ this.register(require('./MessageUpdate'));
9
+ this.register(require('./MessageReactionAdd'));
10
+ this.register(require('./MessageReactionRemove'));
11
+ this.register(require('./MessageReactionRemoveAll'));
12
+ this.register(require('./ChannelCreate'));
13
+ this.register(require('./ChannelDelete'));
14
+ this.register(require('./ChannelUpdate'));
15
+ this.register(require('./GuildDelete'));
16
+ this.register(require('./GuildUpdate'));
17
+ this.register(require('./GuildMemberGet'));
18
+ this.register(require('./GuildMemberRemove'));
19
+ this.register(require('./GuildBanRemove'));
20
+ this.register(require('./GuildRoleCreate'));
21
+ this.register(require('./GuildRoleDelete'));
22
+ this.register(require('./GuildRoleUpdate'));
23
+ this.register(require('./UserGet'));
24
+ this.register(require('./UserUpdate'));
25
+ this.register(require('./UserNoteUpdate'));
26
+ this.register(require('./GuildSync'));
27
+ this.register(require('./GuildEmojiCreate'));
28
+ this.register(require('./GuildEmojiDelete'));
29
+ this.register(require('./GuildEmojiUpdate'));
30
+ this.register(require('./GuildEmojisUpdate'));
31
+ this.register(require('./GuildRolesPositionUpdate'));
32
+ this.register(require('./GuildChannelsPositionUpdate'));
33
+ }
34
+
35
+ register(Action) {
36
+ this[Action.name.replace(/Action$/, '')] = new Action(this.client);
37
+ }
38
+ }
39
+
40
+ module.exports = ActionsManager;
@@ -0,0 +1,11 @@
1
+ const Action = require('./Action');
2
+
3
+ class ChannelCreateAction extends Action {
4
+ handle(data) {
5
+ const client = this.client;
6
+ const channel = client.dataManager.newChannel(data);
7
+ return { channel };
8
+ }
9
+ }
10
+
11
+ module.exports = ChannelCreateAction;
@@ -0,0 +1,30 @@
1
+ const Action = require('./Action');
2
+
3
+ class ChannelDeleteAction extends Action {
4
+ constructor(client) {
5
+ super(client);
6
+ this.deleted = new Map();
7
+ }
8
+
9
+ handle(data) {
10
+ const client = this.client;
11
+
12
+ let channel = client.channels.get(data.id);
13
+ if (channel) {
14
+ client.dataManager.killChannel(channel);
15
+ this.deleted.set(channel.id, channel);
16
+ this.scheduleForDeletion(channel.id);
17
+ } else {
18
+ channel = this.deleted.get(data.id) || null;
19
+ }
20
+ if (channel) channel.deleted = true;
21
+
22
+ return { channel };
23
+ }
24
+
25
+ scheduleForDeletion(id) {
26
+ this.client.setTimeout(() => this.deleted.delete(id), this.client.options.restWsBridgeTimeout);
27
+ }
28
+ }
29
+
30
+ module.exports = ChannelDeleteAction;