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,532 @@
|
|
1
|
+
const util = require('util');
|
2
|
+
|
3
|
+
/**
|
4
|
+
* A Map with additional utility methods. This is used throughout discord.js rather than Arrays for anything that has
|
5
|
+
* an ID, for significantly improved performance and ease-of-use.
|
6
|
+
* @extends {Map}
|
7
|
+
*/
|
8
|
+
class Collection extends Map {
|
9
|
+
constructor(iterable) {
|
10
|
+
super(iterable);
|
11
|
+
|
12
|
+
/**
|
13
|
+
* Cached array for the `array()` method - will be reset to `null` whenever `set()` or `delete()` are called
|
14
|
+
* @name Collection#_array
|
15
|
+
* @type {?Array}
|
16
|
+
* @private
|
17
|
+
*/
|
18
|
+
Object.defineProperty(this, '_array', { value: null, writable: true, configurable: true });
|
19
|
+
|
20
|
+
/**
|
21
|
+
* Cached array for the `keyArray()` method - will be reset to `null` whenever `set()` or `delete()` are called
|
22
|
+
* @name Collection#_keyArray
|
23
|
+
* @type {?Array}
|
24
|
+
* @private
|
25
|
+
*/
|
26
|
+
Object.defineProperty(this, '_keyArray', { value: null, writable: true, configurable: true });
|
27
|
+
}
|
28
|
+
|
29
|
+
set(key, val) {
|
30
|
+
this._array = null;
|
31
|
+
this._keyArray = null;
|
32
|
+
return super.set(key, val);
|
33
|
+
}
|
34
|
+
|
35
|
+
delete(key) {
|
36
|
+
this._array = null;
|
37
|
+
this._keyArray = null;
|
38
|
+
return super.delete(key);
|
39
|
+
}
|
40
|
+
|
41
|
+
/**
|
42
|
+
* Creates an ordered array of the values of this collection, and caches it internally. The array will only be
|
43
|
+
* reconstructed if an item is added to or removed from the collection, or if you change the length of the array
|
44
|
+
* itself. If you don't want this caching behavior, use `[...collection.values()]` or
|
45
|
+
* `Array.from(collection.values())` instead.
|
46
|
+
* @returns {Array}
|
47
|
+
*/
|
48
|
+
array() {
|
49
|
+
if (!this._array || this._array.length !== this.size) this._array = [...this.values()];
|
50
|
+
return this._array;
|
51
|
+
}
|
52
|
+
|
53
|
+
/**
|
54
|
+
* Creates an ordered array of the keys of this collection, and caches it internally. The array will only be
|
55
|
+
* reconstructed if an item is added to or removed from the collection, or if you change the length of the array
|
56
|
+
* itself. If you don't want this caching behavior, use `[...collection.keys()]` or
|
57
|
+
* `Array.from(collection.keys())` instead.
|
58
|
+
* @returns {Array}
|
59
|
+
*/
|
60
|
+
keyArray() {
|
61
|
+
if (!this._keyArray || this._keyArray.length !== this.size) this._keyArray = [...this.keys()];
|
62
|
+
return this._keyArray;
|
63
|
+
}
|
64
|
+
|
65
|
+
/**
|
66
|
+
* Obtains the first value(s) in this collection.
|
67
|
+
* @param {number} [count] Number of values to obtain from the beginning
|
68
|
+
* @returns {*|Array<*>} The single value if `count` is undefined, or an array of values of `count` length
|
69
|
+
*/
|
70
|
+
first(count) {
|
71
|
+
if (count === undefined) return this.values().next().value;
|
72
|
+
if (typeof count !== 'number') throw new TypeError('The count must be a number.');
|
73
|
+
if (!Number.isInteger(count) || count < 1) throw new RangeError('The count must be an integer greater than 0.');
|
74
|
+
count = Math.min(this.size, count);
|
75
|
+
const arr = new Array(count);
|
76
|
+
const iter = this.values();
|
77
|
+
for (let i = 0; i < count; i++) arr[i] = iter.next().value;
|
78
|
+
return arr;
|
79
|
+
}
|
80
|
+
|
81
|
+
/**
|
82
|
+
* Obtains the first key(s) in this collection.
|
83
|
+
* @param {number} [count] Number of keys to obtain from the beginning
|
84
|
+
* @returns {*|Array<*>} The single key if `count` is undefined, or an array of keys of `count` length
|
85
|
+
*/
|
86
|
+
firstKey(count) {
|
87
|
+
if (count === undefined) return this.keys().next().value;
|
88
|
+
if (typeof count !== 'number') throw new TypeError('The count must be a number.');
|
89
|
+
if (!Number.isInteger(count) || count < 1) throw new RangeError('The count must be an integer greater than 0.');
|
90
|
+
count = Math.min(this.size, count);
|
91
|
+
const arr = new Array(count);
|
92
|
+
const iter = this.keys();
|
93
|
+
for (let i = 0; i < count; i++) arr[i] = iter.next().value;
|
94
|
+
return arr;
|
95
|
+
}
|
96
|
+
|
97
|
+
/**
|
98
|
+
* Obtains the last value(s) in this collection. This relies on {@link Collection#array}, and thus the caching
|
99
|
+
* mechanism applies here as well.
|
100
|
+
* @param {number} [count] Number of values to obtain from the end
|
101
|
+
* @returns {*|Array<*>} The single value if `count` is undefined, or an array of values of `count` length
|
102
|
+
*/
|
103
|
+
last(count) {
|
104
|
+
const arr = this.array();
|
105
|
+
if (count === undefined) return arr[arr.length - 1];
|
106
|
+
if (typeof count !== 'number') throw new TypeError('The count must be a number.');
|
107
|
+
if (!Number.isInteger(count) || count < 1) throw new RangeError('The count must be an integer greater than 0.');
|
108
|
+
return arr.slice(-count);
|
109
|
+
}
|
110
|
+
|
111
|
+
/**
|
112
|
+
* Obtains the last key(s) in this collection. This relies on {@link Collection#keyArray}, and thus the caching
|
113
|
+
* mechanism applies here as well.
|
114
|
+
* @param {number} [count] Number of keys to obtain from the end
|
115
|
+
* @returns {*|Array<*>} The single key if `count` is undefined, or an array of keys of `count` length
|
116
|
+
*/
|
117
|
+
lastKey(count) {
|
118
|
+
const arr = this.keyArray();
|
119
|
+
if (count === undefined) return arr[arr.length - 1];
|
120
|
+
if (typeof count !== 'number') throw new TypeError('The count must be a number.');
|
121
|
+
if (!Number.isInteger(count) || count < 1) throw new RangeError('The count must be an integer greater than 0.');
|
122
|
+
return arr.slice(-count);
|
123
|
+
}
|
124
|
+
|
125
|
+
/**
|
126
|
+
* Obtains random value(s) from this collection. This relies on {@link Collection#array}, and thus the caching
|
127
|
+
* mechanism applies here as well.
|
128
|
+
* @param {number} [count] Number of values to obtain randomly
|
129
|
+
* @returns {*|Array<*>} The single value if `count` is undefined, or an array of values of `count` length
|
130
|
+
*/
|
131
|
+
random(count) {
|
132
|
+
let arr = this.array();
|
133
|
+
if (count === undefined) return arr[Math.floor(Math.random() * arr.length)];
|
134
|
+
if (typeof count !== 'number') throw new TypeError('The count must be a number.');
|
135
|
+
if (!Number.isInteger(count) || count < 1) throw new RangeError('The count must be an integer greater than 0.');
|
136
|
+
if (arr.length === 0) return [];
|
137
|
+
const rand = new Array(count);
|
138
|
+
arr = arr.slice();
|
139
|
+
for (let i = 0; i < count; i++) rand[i] = arr.splice(Math.floor(Math.random() * arr.length), 1)[0];
|
140
|
+
return rand;
|
141
|
+
}
|
142
|
+
|
143
|
+
/**
|
144
|
+
* Obtains random key(s) from this collection. This relies on {@link Collection#keyArray}, and thus the caching
|
145
|
+
* mechanism applies here as well.
|
146
|
+
* @param {number} [count] Number of keys to obtain randomly
|
147
|
+
* @returns {*|Array<*>} The single key if `count` is undefined, or an array of keys of `count` length
|
148
|
+
*/
|
149
|
+
randomKey(count) {
|
150
|
+
let arr = this.keyArray();
|
151
|
+
if (count === undefined) return arr[Math.floor(Math.random() * arr.length)];
|
152
|
+
if (typeof count !== 'number') throw new TypeError('The count must be a number.');
|
153
|
+
if (!Number.isInteger(count) || count < 1) throw new RangeError('The count must be an integer greater than 0.');
|
154
|
+
if (arr.length === 0) return [];
|
155
|
+
const rand = new Array(count);
|
156
|
+
arr = arr.slice();
|
157
|
+
for (let i = 0; i < count; i++) rand[i] = arr.splice(Math.floor(Math.random() * arr.length), 1)[0];
|
158
|
+
return rand;
|
159
|
+
}
|
160
|
+
|
161
|
+
/**
|
162
|
+
* Searches for all items where their specified property's value is identical to the given value
|
163
|
+
* (`item[prop] === value`).
|
164
|
+
* @param {string} prop The property to test against
|
165
|
+
* @param {*} value The expected value
|
166
|
+
* @returns {Array}
|
167
|
+
* @deprecated
|
168
|
+
* @example
|
169
|
+
* collection.findAll('username', 'Bob');
|
170
|
+
*/
|
171
|
+
findAll(prop, value) {
|
172
|
+
if (typeof prop !== 'string') throw new TypeError('Key must be a string.');
|
173
|
+
if (typeof value === 'undefined') throw new Error('Value must be specified.');
|
174
|
+
const results = [];
|
175
|
+
for (const item of this.values()) {
|
176
|
+
if (item[prop] === value) results.push(item);
|
177
|
+
}
|
178
|
+
return results;
|
179
|
+
}
|
180
|
+
|
181
|
+
/**
|
182
|
+
* Searches for a single item where its specified property's value is identical to the given value
|
183
|
+
* (`item[prop] === value`), or the given function returns a truthy value. In the latter case, this is identical to
|
184
|
+
* [Array.find()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find).
|
185
|
+
* <warn>All collections used in Discord.js are mapped using their `id` property, and if you want to find by id you
|
186
|
+
* should use the `get` method. See
|
187
|
+
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get) for details.</warn>
|
188
|
+
* @param {string|Function} propOrFn The property to test against, or the function to test with
|
189
|
+
* @param {*} [value] The expected value - only applicable and required if using a property for the first argument
|
190
|
+
* @returns {*}
|
191
|
+
* @example
|
192
|
+
* collection.find('username', 'Bob');
|
193
|
+
* @example
|
194
|
+
* collection.find(val => val.username === 'Bob');
|
195
|
+
*/
|
196
|
+
find(propOrFn, value) {
|
197
|
+
if (typeof propOrFn === 'string') {
|
198
|
+
if (typeof value === 'undefined') throw new Error('Value must be specified.');
|
199
|
+
for (const item of this.values()) {
|
200
|
+
if (item[propOrFn] === value) return item;
|
201
|
+
}
|
202
|
+
return null;
|
203
|
+
} else if (typeof propOrFn === 'function') {
|
204
|
+
for (const [key, val] of this) {
|
205
|
+
if (propOrFn(val, key, this)) return val;
|
206
|
+
}
|
207
|
+
return null;
|
208
|
+
} else {
|
209
|
+
throw new Error('First argument must be a property string or a function.');
|
210
|
+
}
|
211
|
+
}
|
212
|
+
|
213
|
+
/* eslint-disable max-len */
|
214
|
+
/**
|
215
|
+
* Searches for the key of a single item where its specified property's value is identical to the given value
|
216
|
+
* (`item[prop] === value`), or the given function returns a truthy value. In the latter case, this is identical to
|
217
|
+
* [Array.findIndex()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex).
|
218
|
+
* @param {string|Function} propOrFn The property to test against, or the function to test with
|
219
|
+
* @param {*} [value] The expected value - only applicable and required if using a property for the first argument
|
220
|
+
* @returns {*}
|
221
|
+
* @example
|
222
|
+
* collection.findKey('username', 'Bob');
|
223
|
+
* @example
|
224
|
+
* collection.findKey(val => val.username === 'Bob');
|
225
|
+
*/
|
226
|
+
findKey(propOrFn, value) {
|
227
|
+
/* eslint-enable max-len */
|
228
|
+
if (typeof propOrFn === 'string') {
|
229
|
+
if (typeof value === 'undefined') throw new Error('Value must be specified.');
|
230
|
+
for (const [key, val] of this) {
|
231
|
+
if (val[propOrFn] === value) return key;
|
232
|
+
}
|
233
|
+
return null;
|
234
|
+
} else if (typeof propOrFn === 'function') {
|
235
|
+
for (const [key, val] of this) {
|
236
|
+
if (propOrFn(val, key, this)) return key;
|
237
|
+
}
|
238
|
+
return null;
|
239
|
+
} else {
|
240
|
+
throw new Error('First argument must be a property string or a function.');
|
241
|
+
}
|
242
|
+
}
|
243
|
+
|
244
|
+
/**
|
245
|
+
* Searches for the existence of a single item where its specified property's value is identical to the given value
|
246
|
+
* (`item[prop] === value`).
|
247
|
+
* <warn>Do not use this to check for an item by its ID. Instead, use `collection.has(id)`. See
|
248
|
+
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has) for details.</warn>
|
249
|
+
* @param {string} prop The property to test against
|
250
|
+
* @param {*} value The expected value
|
251
|
+
* @returns {boolean}
|
252
|
+
* @deprecated
|
253
|
+
* @example
|
254
|
+
* if (collection.exists('username', 'Bob')) {
|
255
|
+
* console.log('user here!');
|
256
|
+
* }
|
257
|
+
*/
|
258
|
+
exists(prop, value) {
|
259
|
+
return Boolean(this.find(prop, value));
|
260
|
+
}
|
261
|
+
|
262
|
+
/**
|
263
|
+
* Removes entries that satisfy the provided filter function.
|
264
|
+
* @param {Function} fn Function used to test (should return a boolean)
|
265
|
+
* @param {Object} [thisArg] Value to use as `this` when executing function
|
266
|
+
* @returns {number} The number of removed entries
|
267
|
+
*/
|
268
|
+
sweep(fn, thisArg) {
|
269
|
+
if (thisArg) fn = fn.bind(thisArg);
|
270
|
+
const previousSize = this.size;
|
271
|
+
for (const [key, val] of this) {
|
272
|
+
if (fn(val, key, this)) this.delete(key);
|
273
|
+
}
|
274
|
+
return previousSize - this.size;
|
275
|
+
}
|
276
|
+
|
277
|
+
/**
|
278
|
+
* Identical to
|
279
|
+
* [Array.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter),
|
280
|
+
* but returns a Collection instead of an Array.
|
281
|
+
* @param {Function} fn Function used to test (should return a boolean)
|
282
|
+
* @param {Object} [thisArg] Value to use as `this` when executing function
|
283
|
+
* @returns {Collection}
|
284
|
+
*/
|
285
|
+
filter(fn, thisArg) {
|
286
|
+
if (thisArg) fn = fn.bind(thisArg);
|
287
|
+
const results = new Collection();
|
288
|
+
for (const [key, val] of this) {
|
289
|
+
if (fn(val, key, this)) results.set(key, val);
|
290
|
+
}
|
291
|
+
return results;
|
292
|
+
}
|
293
|
+
|
294
|
+
/**
|
295
|
+
* Identical to
|
296
|
+
* [Array.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter).
|
297
|
+
* @param {Function} fn Function used to test (should return a boolean)
|
298
|
+
* @param {Object} [thisArg] Value to use as `this` when executing function
|
299
|
+
* @returns {Array}
|
300
|
+
* @deprecated
|
301
|
+
*/
|
302
|
+
filterArray(fn, thisArg) {
|
303
|
+
if (thisArg) fn = fn.bind(thisArg);
|
304
|
+
const results = [];
|
305
|
+
for (const [key, val] of this) {
|
306
|
+
if (fn(val, key, this)) results.push(val);
|
307
|
+
}
|
308
|
+
return results;
|
309
|
+
}
|
310
|
+
|
311
|
+
/**
|
312
|
+
* Partitions the collection into two collections where the first collection
|
313
|
+
* contains the items that passed and the second contains the items that failed.
|
314
|
+
* @param {Function} fn Function used to test (should return a boolean)
|
315
|
+
* @param {*} [thisArg] Value to use as `this` when executing function
|
316
|
+
* @returns {Collection[]}
|
317
|
+
* @example const [big, small] = collection.partition(guild => guild.memberCount > 250);
|
318
|
+
*/
|
319
|
+
partition(fn, thisArg) {
|
320
|
+
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
|
321
|
+
const results = [new Collection(), new Collection()];
|
322
|
+
for (const [key, val] of this) {
|
323
|
+
if (fn(val, key, this)) {
|
324
|
+
results[0].set(key, val);
|
325
|
+
} else {
|
326
|
+
results[1].set(key, val);
|
327
|
+
}
|
328
|
+
}
|
329
|
+
return results;
|
330
|
+
}
|
331
|
+
|
332
|
+
/**
|
333
|
+
* Identical to
|
334
|
+
* [Array.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map).
|
335
|
+
* @param {Function} fn Function that produces an element of the new array, taking three arguments
|
336
|
+
* @param {*} [thisArg] Value to use as `this` when executing function
|
337
|
+
* @returns {Array}
|
338
|
+
*/
|
339
|
+
map(fn, thisArg) {
|
340
|
+
if (thisArg) fn = fn.bind(thisArg);
|
341
|
+
const arr = new Array(this.size);
|
342
|
+
let i = 0;
|
343
|
+
for (const [key, val] of this) arr[i++] = fn(val, key, this);
|
344
|
+
return arr;
|
345
|
+
}
|
346
|
+
|
347
|
+
/**
|
348
|
+
* Identical to
|
349
|
+
* [Array.some()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some).
|
350
|
+
* @param {Function} fn Function used to test (should return a boolean)
|
351
|
+
* @param {Object} [thisArg] Value to use as `this` when executing function
|
352
|
+
* @returns {boolean}
|
353
|
+
*/
|
354
|
+
some(fn, thisArg) {
|
355
|
+
if (thisArg) fn = fn.bind(thisArg);
|
356
|
+
for (const [key, val] of this) {
|
357
|
+
if (fn(val, key, this)) return true;
|
358
|
+
}
|
359
|
+
return false;
|
360
|
+
}
|
361
|
+
|
362
|
+
/**
|
363
|
+
* Identical to
|
364
|
+
* [Array.every()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every).
|
365
|
+
* @param {Function} fn Function used to test (should return a boolean)
|
366
|
+
* @param {Object} [thisArg] Value to use as `this` when executing function
|
367
|
+
* @returns {boolean}
|
368
|
+
*/
|
369
|
+
every(fn, thisArg) {
|
370
|
+
if (thisArg) fn = fn.bind(thisArg);
|
371
|
+
for (const [key, val] of this) {
|
372
|
+
if (!fn(val, key, this)) return false;
|
373
|
+
}
|
374
|
+
return true;
|
375
|
+
}
|
376
|
+
|
377
|
+
/**
|
378
|
+
* Identical to
|
379
|
+
* [Array.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce).
|
380
|
+
* @param {Function} fn Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
|
381
|
+
* and `collection`
|
382
|
+
* @param {*} [initialValue] Starting value for the accumulator
|
383
|
+
* @returns {*}
|
384
|
+
*/
|
385
|
+
reduce(fn, initialValue) {
|
386
|
+
let accumulator;
|
387
|
+
if (typeof initialValue !== 'undefined') {
|
388
|
+
accumulator = initialValue;
|
389
|
+
for (const [key, val] of this) accumulator = fn(accumulator, val, key, this);
|
390
|
+
} else {
|
391
|
+
let first = true;
|
392
|
+
for (const [key, val] of this) {
|
393
|
+
if (first) {
|
394
|
+
accumulator = val;
|
395
|
+
first = false;
|
396
|
+
continue;
|
397
|
+
}
|
398
|
+
accumulator = fn(accumulator, val, key, this);
|
399
|
+
}
|
400
|
+
}
|
401
|
+
return accumulator;
|
402
|
+
}
|
403
|
+
|
404
|
+
/**
|
405
|
+
* Identical to
|
406
|
+
* [Map.forEach()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach),
|
407
|
+
* but returns the collection instead of undefined.
|
408
|
+
* @param {Function} fn Function to execute for each element
|
409
|
+
* @param {*} [thisArg] Value to use as `this` when executing function
|
410
|
+
* @returns {Collection}
|
411
|
+
* @example
|
412
|
+
* collection
|
413
|
+
* .tap(user => console.log(user.username))
|
414
|
+
* .filter(user => user.bot)
|
415
|
+
* .tap(user => console.log(user.username));
|
416
|
+
*/
|
417
|
+
tap(fn, thisArg) {
|
418
|
+
this.forEach(fn, thisArg);
|
419
|
+
return this;
|
420
|
+
}
|
421
|
+
|
422
|
+
/**
|
423
|
+
* Creates an identical shallow copy of this collection.
|
424
|
+
* @returns {Collection}
|
425
|
+
* @example const newColl = someColl.clone();
|
426
|
+
*/
|
427
|
+
clone() {
|
428
|
+
return new this.constructor(this);
|
429
|
+
}
|
430
|
+
|
431
|
+
/**
|
432
|
+
* Combines this collection with others into a new collection. None of the source collections are modified.
|
433
|
+
* @param {...Collection} collections Collections to merge
|
434
|
+
* @returns {Collection}
|
435
|
+
* @example const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
|
436
|
+
*/
|
437
|
+
concat(...collections) {
|
438
|
+
const newColl = this.clone();
|
439
|
+
for (const coll of collections) {
|
440
|
+
for (const [key, val] of coll) newColl.set(key, val);
|
441
|
+
}
|
442
|
+
return newColl;
|
443
|
+
}
|
444
|
+
|
445
|
+
/**
|
446
|
+
* Calls the `delete()` method on all items that have it.
|
447
|
+
* @returns {Promise[]}
|
448
|
+
*/
|
449
|
+
deleteAll() {
|
450
|
+
const returns = [];
|
451
|
+
for (const item of this.values()) {
|
452
|
+
if (item.delete) returns.push(item.delete());
|
453
|
+
}
|
454
|
+
return returns;
|
455
|
+
}
|
456
|
+
|
457
|
+
/**
|
458
|
+
* Checks if this collection shares identical key-value pairings with another.
|
459
|
+
* This is different to checking for equality using equal-signs, because
|
460
|
+
* the collections may be different objects, but contain the same data.
|
461
|
+
* @param {Collection} collection Collection to compare with
|
462
|
+
* @returns {boolean} Whether the collections have identical contents
|
463
|
+
*/
|
464
|
+
equals(collection) {
|
465
|
+
if (!collection) return false;
|
466
|
+
if (this === collection) return true;
|
467
|
+
if (this.size !== collection.size) return false;
|
468
|
+
return !this.find((value, key) => {
|
469
|
+
const testVal = collection.get(key);
|
470
|
+
return testVal !== value || (testVal === undefined && !collection.has(key));
|
471
|
+
});
|
472
|
+
}
|
473
|
+
|
474
|
+
/**
|
475
|
+
* The sort() method sorts the elements of a collection in place and returns the collection.
|
476
|
+
* The sort is not necessarily stable. The default sort order is according to string Unicode code points.
|
477
|
+
* @param {Function} [compareFunction] Specifies a function that defines the sort order.
|
478
|
+
* if omitted, the collection is sorted according to each character's Unicode code point value,
|
479
|
+
* according to the string conversion of each element.
|
480
|
+
* @returns {Collection}
|
481
|
+
*/
|
482
|
+
sort(compareFunction = (x, y) => +(x > y) || +(x === y) - 1) {
|
483
|
+
return new Collection([...this.entries()].sort((a, b) => compareFunction(a[1], b[1], a[0], b[0])));
|
484
|
+
}
|
485
|
+
}
|
486
|
+
|
487
|
+
Collection.prototype.findAll =
|
488
|
+
util.deprecate(Collection.prototype.findAll, 'Collection#findAll: use Collection#filter instead');
|
489
|
+
|
490
|
+
Collection.prototype.filterArray =
|
491
|
+
util.deprecate(Collection.prototype.filterArray, 'Collection#filterArray: use Collection#filter instead');
|
492
|
+
|
493
|
+
Collection.prototype.exists =
|
494
|
+
util.deprecate(Collection.prototype.exists, 'Collection#exists: use Collection#some instead');
|
495
|
+
|
496
|
+
Collection.prototype.find = function find(propOrFn, value) {
|
497
|
+
if (typeof propOrFn === 'string') {
|
498
|
+
process.emitWarning('Collection#find: pass a function instead', 'DeprecationWarning');
|
499
|
+
if (typeof value === 'undefined') throw new Error('Value must be specified.');
|
500
|
+
for (const item of this.values()) {
|
501
|
+
if (item[propOrFn] === value) return item;
|
502
|
+
}
|
503
|
+
return null;
|
504
|
+
} else if (typeof propOrFn === 'function') {
|
505
|
+
for (const [key, val] of this) {
|
506
|
+
if (propOrFn(val, key, this)) return val;
|
507
|
+
}
|
508
|
+
return null;
|
509
|
+
} else {
|
510
|
+
throw new Error('First argument must be a property string or a function.');
|
511
|
+
}
|
512
|
+
};
|
513
|
+
|
514
|
+
Collection.prototype.findKey = function findKey(propOrFn, value) {
|
515
|
+
if (typeof propOrFn === 'string') {
|
516
|
+
process.emitWarning('Collection#findKey: pass a function instead', 'DeprecationWarning');
|
517
|
+
if (typeof value === 'undefined') throw new Error('Value must be specified.');
|
518
|
+
for (const [key, val] of this) {
|
519
|
+
if (val[propOrFn] === value) return key;
|
520
|
+
}
|
521
|
+
return null;
|
522
|
+
} else if (typeof propOrFn === 'function') {
|
523
|
+
for (const [key, val] of this) {
|
524
|
+
if (propOrFn(val, key, this)) return key;
|
525
|
+
}
|
526
|
+
return null;
|
527
|
+
} else {
|
528
|
+
throw new Error('First argument must be a property string or a function.');
|
529
|
+
}
|
530
|
+
};
|
531
|
+
|
532
|
+
module.exports = Collection;
|