hypixel-api-reborn 9.0.3 → 11.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/.prettierrc +13 -0
  2. package/README.md +28 -16
  3. package/package.json +60 -58
  4. package/src/API/getAPIStatus.js +8 -7
  5. package/src/API/getBoosters.js +6 -5
  6. package/src/API/getGameCounts.js +6 -5
  7. package/src/API/getGuild.js +18 -17
  8. package/src/API/getLeaderboards.js +13 -12
  9. package/src/API/getPlayer.js +22 -26
  10. package/src/API/getRecentGames.js +18 -17
  11. package/src/API/getServerInfo.js +66 -77
  12. package/src/API/getStatus.js +8 -7
  13. package/src/API/getWatchdogStats.js +6 -5
  14. package/src/API/index.js +19 -9
  15. package/src/API/skyblock/getEndedSkyblockAuctions.js +10 -9
  16. package/src/API/skyblock/getSkyblockAuctions.js +64 -64
  17. package/src/API/skyblock/getSkyblockAuctionsByPlayer.js +11 -10
  18. package/src/API/skyblock/getSkyblockBazaar.js +10 -9
  19. package/src/API/skyblock/getSkyblockMember.js +28 -24
  20. package/src/API/skyblock/getSkyblockNews.js +6 -5
  21. package/src/API/skyblock/getSkyblockProfiles.js +44 -40
  22. package/src/Client.js +406 -430
  23. package/src/Errors.js +44 -38
  24. package/src/Private/defaultCache.js +77 -0
  25. package/src/Private/rateLimit.js +79 -98
  26. package/src/Private/requests.js +65 -39
  27. package/src/Private/updater.js +45 -42
  28. package/src/Private/uuidCache.js +37 -33
  29. package/src/Private/validate.js +97 -97
  30. package/src/index.js +82 -83
  31. package/src/structures/APIIncident.js +78 -78
  32. package/src/structures/APIStatus.js +47 -47
  33. package/src/structures/Boosters/Booster.js +83 -87
  34. package/src/structures/Color.js +129 -129
  35. package/src/structures/Game.js +164 -160
  36. package/src/structures/GameCounts.js +151 -151
  37. package/src/structures/Guild/Guild.js +204 -218
  38. package/src/structures/Guild/GuildMember.js +66 -66
  39. package/src/structures/Guild/GuildRank.js +48 -48
  40. package/src/structures/ItemBytes.js +33 -36
  41. package/src/structures/Leaderboard.js +31 -31
  42. package/src/structures/MiniGames/Arcade.js +567 -569
  43. package/src/structures/MiniGames/ArenaBrawl.js +52 -56
  44. package/src/structures/MiniGames/BedWars.js +423 -329
  45. package/src/structures/MiniGames/BlitzSurvivalGames.js +172 -148
  46. package/src/structures/MiniGames/BuildBattle.js +53 -53
  47. package/src/structures/MiniGames/CopsAndCrims.js +90 -90
  48. package/src/structures/MiniGames/Duels.js +694 -460
  49. package/src/structures/MiniGames/MegaWalls.js +137 -137
  50. package/src/structures/MiniGames/MurderMystery.js +93 -93
  51. package/src/structures/MiniGames/Paintball.js +67 -67
  52. package/src/structures/MiniGames/Pit.js +19 -19
  53. package/src/structures/MiniGames/Quakecraft.js +101 -101
  54. package/src/structures/MiniGames/SkyWars.js +508 -559
  55. package/src/structures/MiniGames/SmashHeroes.js +144 -151
  56. package/src/structures/MiniGames/SpeedUHC.js +115 -57
  57. package/src/structures/MiniGames/TNTGames.js +116 -116
  58. package/src/structures/MiniGames/TurboKartRacers.js +201 -71
  59. package/src/structures/MiniGames/UHC.js +164 -143
  60. package/src/structures/MiniGames/VampireZ.js +60 -60
  61. package/src/structures/MiniGames/Walls.js +52 -52
  62. package/src/structures/MiniGames/Warlords.js +62 -62
  63. package/src/structures/MiniGames/WoolWars.js +123 -0
  64. package/src/structures/Pet.js +96 -97
  65. package/src/structures/Pets.js +64 -64
  66. package/src/structures/Player.js +433 -453
  67. package/src/structures/PlayerCosmetics.js +100 -95
  68. package/src/structures/RecentGame.js +57 -57
  69. package/src/structures/ServerInfo.js +78 -78
  70. package/src/structures/SkyBlock/Auctions/Auction.js +104 -104
  71. package/src/structures/SkyBlock/Auctions/AuctionInfo.js +54 -54
  72. package/src/structures/SkyBlock/Auctions/BaseAuction.js +45 -45
  73. package/src/structures/SkyBlock/Auctions/Bid.js +48 -48
  74. package/src/structures/SkyBlock/Auctions/PartialAuction.js +25 -25
  75. package/src/structures/SkyBlock/Bazzar/Order.js +38 -38
  76. package/src/structures/SkyBlock/Bazzar/Product.js +53 -53
  77. package/src/structures/SkyBlock/News/SkyblockNews.js +60 -68
  78. package/src/structures/SkyBlock/SkyblockInventoryItem.js +124 -102
  79. package/src/structures/SkyBlock/SkyblockMember.js +820 -675
  80. package/src/structures/SkyBlock/SkyblockPet.js +71 -71
  81. package/src/structures/SkyBlock/SkyblockProfile.js +60 -60
  82. package/src/structures/Status.js +41 -41
  83. package/src/structures/Watchdog/Stats.js +36 -36
  84. package/src/utils/Constants.js +2789 -795
  85. package/src/utils/SkyblockUtils.js +208 -164
  86. package/src/utils/arrayTools.js +4 -4
  87. package/src/utils/divide.js +5 -5
  88. package/src/utils/guildExp.js +57 -67
  89. package/src/utils/index.js +13 -9
  90. package/src/utils/isGuildID.js +3 -3
  91. package/src/utils/isUUID.js +5 -5
  92. package/src/utils/oscillation.js +15 -23
  93. package/src/utils/removeSnakeCase.js +22 -35
  94. package/src/utils/rgbToHexColor.js +8 -0
  95. package/src/utils/romanize.js +11 -0
  96. package/src/utils/toIGN.js +20 -20
  97. package/src/utils/toUuid.js +19 -19
  98. package/src/utils/varInt.js +17 -21
  99. package/typings/index.d.ts +3532 -2625
  100. package/src/API/getFriends.js +0 -13
  101. package/src/API/getKeyInfo.js +0 -9
  102. package/src/API/getRankedSkyWars.js +0 -10
  103. package/src/structures/Friend.js +0 -38
  104. package/src/structures/KeyInfo.js +0 -42
  105. package/src/structures/MiniGames/SkyWarsRanked.js +0 -45
package/src/Client.js CHANGED
@@ -1,430 +1,406 @@
1
- /* eslint-disable require-jsdoc */
2
- /* eslint-disable max-len */
3
- const validate = new (require('./Private/validate'))();
4
- const rateLimit = new (require('./Private/rateLimit'))();
5
- const requests = new (require('./Private/requests'))();
6
- const updater = new (require('./Private/updater'))();
7
- const Errors = require('./Errors');
8
- const API = require('./API/index');
9
- const EventEmitter = require('events');
10
- const clients = [];
11
- /**
12
- * Client class
13
- */
14
- class Client extends EventEmitter {
15
- /**
16
- * @param {string} key API key [(?)](https://stavzdev.is-inside.me/cCMiZdoy.gif)
17
- * @param {ClientOptions} [options={}] Client options
18
- */
19
- constructor (key, options = {}) {
20
- super();
21
- // eslint-disable-next-line no-console
22
- if (options && !options.silent) this.on('warn', console.warn);
23
- // Test to check for multiple instances of client
24
- if (clients.find((x) => x.key === key)) {
25
- this.emit('warn', Errors.MULTIPLE_INSTANCES);
26
- return clients.find((x) => x.key === key);
27
- }
28
- validate.validateNodeVersion();
29
- this.key = validate.validateKey(key);
30
- this.options = validate.parseOptions(options);
31
- validate.validateOptions(this.options);
32
- // eslint-disable-next-line no-return-assign
33
- Object.keys(API).forEach((func) => Client.prototype[func] = function (...args) {
34
- return API[func].apply({ _makeRequest: this._makeRequest.bind(this, { ...(validate.cacheSuboptions(args[args.length - 1]) ? args[args.length - 1] : {}) }), ...this }, args);
35
- });
36
- if (this.options.checkForUpdates) {
37
- updater.checkForUpdates();
38
- }
39
- /**
40
- * All cache entries
41
- * @type {Map<string,object>}
42
- */
43
- this.cache = requests.cache;
44
- clients.push(this);
45
- rateLimit.init(this.getKeyInfo(), this.options, this).then(() => this.emit('ready'));
46
- }
47
- /**
48
- * Private function - make request
49
- * @param {MethodOptions} options Options for request
50
- * @param {string} url Endpoint URL to request
51
- * @param {boolean} [useRateLimitManager=true] Use rate limit
52
- * @returns {Promise<Object>} Response
53
- * @private
54
- */
55
- async _makeRequest (options, url, useRateLimitManager = true) {
56
- if (!url) return;
57
- if (url !== '/key' && !options.noCacheCheck && requests.cache.has(url)) return requests.cache.get(url);
58
- if (useRateLimitManager) await rateLimit.rateLimitManager();
59
- this.emit('outgoingRequest', url, { ...options, headers: { ...options.headers, ...this.options.headers } });
60
- const result = await requests.request.call(this, url, { ...options, headers: { ...options.headers, ...this.options.headers } });
61
- if (this.options.syncWithHeaders) rateLimit.sync(result._headers);
62
- return result;
63
- }
64
- /**
65
- * Emitted when a request is going to be sent
66
- * @event
67
- * @name Client#outgoingRequest
68
- * @param {string} url URL
69
- * @param {object} [options] Options, if any
70
- */
71
- /**
72
- * Emitted when rate limiter is ready. ( You don't have to wait for this event to emit UNLESS you are planning to do data scraping which means spamming requests )
73
- * @event
74
- * @name Client#ready
75
- * @example
76
- * // This example gets the first 100 friends of a player and gets their stats.
77
- * hypixel.once('ready',()=>{
78
- * hypixel.getFriends('StavZDev')
79
- * .then(friends => friends.map(x=>x.uuid).slice(0, 100))
80
- * .then(hypixel.getPlayer)
81
- * .catch(console.log);
82
- * })
83
- */
84
- /**
85
- * Emitted when there is a warning.
86
- * @event
87
- * @name Client#warn
88
- * @param {string} error Warning Message
89
- */
90
- /**
91
- * Allows you to get statistics of player
92
- * @method
93
- * @name Client#getPlayer
94
- * @param {string} query Player nickname or UUID
95
- * @param {PlayerMethodOptions} [options={}] Method options
96
- * @return {Promise<Player>}
97
- * @example
98
- * hypixel.getPlayer('StavZDev').then(player => {
99
- * console.log(player.level); // 141
100
- * console.log(player.rank); // 'MVP+'
101
- * }).catch(e => {
102
- * console.log(e);
103
- * })
104
- * @example
105
- * // Get player's guild along with player stats
106
- * hypixel.getPlayer('Minikloon', { guild: true }).then(player => {
107
- * console.log(player.guild) // null if player isn't is guild
108
- * console.log(player.guild.name) // Mini Squid
109
- * console.log(player.guild.level) // 110
110
- * }).catch(e => {
111
- * console.log(e);
112
- * });
113
- * @example
114
- * // async/await
115
- * const player = await hypixel.getPlayer('Minikloon').catch(console.log);
116
- * // If player doesn't exist, or if an error occurred ( check console )
117
- * console.log(player); // null
118
- * // Check if player exists
119
- * if(player) console.log(player.uuid); // 20934ef9488c465180a78f861586b4cf
120
- */
121
- /**
122
- * Allows you to get statistics of hypixel guild
123
- * @method
124
- * @name Client#getGuild
125
- * @param {id|name|player} searchParameter Search for guild by id, name or player (if player is in guild)
126
- * @param {string} query Guild ID, Guild name or player uuid/nickname
127
- * @param {MethodOptions} [options={}] Method options
128
- * @return {Promise<Guild>}
129
- * @example
130
- * hypixel.getGuild('name', 'The Foundation').then(guild => {
131
- * console.log(guild.level); // 111
132
- * console.log(guild.id); // '52e5719284ae51ed0c716c69'
133
- * }).catch(e => {
134
- * console.log(e);
135
- * })
136
- */
137
- /**
138
- * Allows you get player's list of friends
139
- * @method
140
- * @name Client#getFriends
141
- * @param {string} query Player nickname or UUID
142
- * @param {MethodOptions} [options={}] Method options
143
- * @return {Promise<Array<Friend>>}
144
- * @example
145
- * hypixel.getFriends('StavZDev').then(friends => {
146
- * console.log(friends[0].friendSinceTimestamp); // 1528363745834
147
- * }).catch(e => {
148
- * console.log(e);
149
- * })
150
- */
151
- /**
152
- * Allows you to get statistics of watchdog, the server anticheat
153
- * @method
154
- * @name Client#getWatchdogStats
155
- * @param {MethodOptions} [options={}] Method options
156
- * @return {Promise<WatchdogStats>}
157
- * @example
158
- * hypixel.getWatchdogStats().then(watchdog => {
159
- * console.log(watchdog.byWatchdogTotal); // 5931897
160
- * }).catch(e => {
161
- * console.log(e);
162
- * })
163
- */
164
- /**
165
- * Allows you to get all active boosters
166
- * @method
167
- * @name Client#getBoosters
168
- * @param {MethodOptions} [options={}] Method options
169
- * @return {Promise<Array<Booster>>}
170
- * @example
171
- * hypixel.getBoosters().then(boosters => {
172
- * console.log(boosters[0].purchaser); // '978ddb705a8e43618e41749178c020b0'
173
- * }).catch(e => {
174
- * console.log(e);
175
- * })
176
- */
177
- /**
178
- * Allows you to get all skyblock profiles of player
179
- * @method
180
- * @name Client#getSkyblockProfiles
181
- * @param {string} query Player nickname or UUID
182
- * @param {SkyblockMethodOptions} [options={}] Method options
183
- * @return {Promise<Array<SkyblockProfile>>}
184
- * @example
185
- * hypixel.getSkyblockProfiles('StavZDev').then(profiles => {
186
- * console.log(profiles[0].members[0].uuid); // '52d9a36f66ce4cdf9a56ad9724ae9fb4'
187
- * }).catch(e => {
188
- * console.log(e);
189
- * })
190
- */
191
- /**
192
- * Allows you to get a player's skyblock member data from all their profiles
193
- * @method
194
- * @name Client#getSkyblockMember
195
- * @param {string} query Player nickname or UUID
196
- * @param {MethodOptions} [options={}] Method options
197
- * @return {Promise<Map<string,SkyblockMember>>}
198
- * @example
199
- * hypixel.getSkyblockMember('StavZDev').then(member => {
200
- * // 'Cucumber' - profile name
201
- * console.log(member.get('Cucumber').uuid); // '52d9a36f66ce4cdf9a56ad9724ae9fb4'
202
- * }).catch(e => {
203
- * console.log(e);
204
- * })
205
- */
206
- /**
207
- * Allows you to get the Hypixel API's Status and past Incidents, no key needed.
208
- * @method
209
- * @name Client#getAPIStatus
210
- * @return {Promise<APIStatus>}
211
- * @example
212
- * hypixel.getAPIStatus().then(status => {
213
- * console.log(status.incidents[0].link); // 'https://status.hypixel.net/incidents/zdd5gppdtcc3'
214
- * }).catch(e => {
215
- * console.log(e);
216
- * })
217
- */
218
- /**
219
- * Gets Key information
220
- * @method
221
- * @name Client#getKeyInfo
222
- * @param {MethodOptions} [options={}] Method options
223
- * @return {Promise<KeyInfo>}
224
- * @example
225
- * hypixel.getKeyInfo().then(keyInfo => {
226
- * console.log(keyInfo.owner); // '52d9a36f66ce4cdf9a56ad9724ae9fb4'
227
- * }).catch(e => {
228
- * console.log(e);
229
- * })
230
- */
231
- /**
232
- * Gets all leaderboards
233
- * @method
234
- * @name Client#getLeaderboards
235
- * @param {MethodOptions} [options={}] Method options
236
- * @return {Promise<{ ARENA: Leaderboard[], COPS_AND_CRIMS: Leaderboard[], WARLORDS: Leaderboard[], BLITZ_SURVIVAL_GAMES: Leaderboard[], UHC: Leaderboard[], WALLS: Leaderboard[], PROTOTYPE: Leaderboard[], PAINTBALL: Leaderboard[], SKYWARS: Leaderboard[], MURDER_MYSTERY: Leaderboard[], SMASH_HEROES: Leaderboard[], DUELS: Leaderboard[], SPEED_UHC: Leaderboard[], TNTGAMES: Leaderboard[], BEDWARS: Leaderboard[], TURBO_KART_RACERS: Leaderboard[], BUILD_BATTLE: Leaderboard[], ARCADE: Leaderboard[], SKYCLASH: Leaderboard[], QUAKECRAFT: Leaderboard[], CRAZY_WALLS: Leaderboard[], MEGA_WALLS: Leaderboard[], VAMPIREZ: Leaderboard[] }>}
237
- * @example
238
- * hypixel.getLeaderboards().then(leaderboards => {
239
- * console.log(leaderboards.ARENA[0].name); // 'Wins'
240
- * }).catch(e => {
241
- * console.log(e);
242
- * })
243
- */
244
- /**
245
- * Sends a STATUS packet to hypixel and parses the return info (a 0x00 and 0x01 packet)
246
- * @method
247
- * @name Client#getServerInfo
248
- * @param {number} [repeats=3] Sends x amount of ping requests and gets the average. Should be between 1 and 10
249
- * @return {Promise<ServerInfo>}
250
- * @example
251
- * hypixel.getServerInfo(3).then(serverInfo =>{
252
- * console.log(serverInfo.ping); // 69
253
- * }).catch(console.log)
254
- */
255
- /**
256
- * Allows you to get recent games of a player
257
- * @method
258
- * @name Client#getRecentGames
259
- * @param {string} query Player nickname or UUID
260
- * @param {MethodOptions} [options={}] Method options
261
- * @return {Promise<RecentGame[]>}
262
- * @example
263
- * hypixel.getRecentGames().then(recentGames =>{
264
- * console.log(recentGames[0].endedTimestamp); // 1609670588789
265
- * })
266
- * .catch(console.log);
267
- */
268
- /**
269
- * Allows you to get the status of a player
270
- * @method
271
- * @name Client#getStatus
272
- * @param {string} query Player nickname or UUID
273
- * @param {MethodOptions} [options={}] Method options
274
- * @return {Promise<Status>}
275
- * @example
276
- * hypixel.getStatus('Stavzdev').then(status =>{
277
- * console.log(status.online); // true
278
- * })
279
- * .catch(console.log);
280
- */
281
- /**
282
- * Allows you to get skyblock auctions
283
- * @method
284
- * @name Client#getSkyblockAuctions
285
- * @param {string|number|number[]} page - "*", a page number, or an array with the start and the end page number ( automatically sorted )
286
- * @param {auctionsOptions} [options={}] Options
287
- * @return {Promise<{info:AuctionInfo,auctions:Auction[]}>}
288
- * @example
289
- * hypixel.getSkyblockAuctions(0).then(auctions =>{
290
- * console.log(auctions[0].item); // Mythic Farmer Boots
291
- * })
292
- * .catch(console.log);
293
- */
294
- /**
295
- * Allows you to get player's skyblock auctions
296
- * @method
297
- * @name Client#getSkyblockAuctionsByPlayer
298
- * @param {string} query - player nickname or uuid
299
- * @param {boolean} [includeItemBytes=false] - include item bytes (optional)
300
- * @param {MethodOptions} [options={}] Options
301
- * @return {Promise<Auction[]>}
302
- * @example
303
- * hypixel.getSkyblockAuctionsByPlayer('hypixel').then(auctions =>{
304
- * console.log(auctions[0].auctionId); // b0491da3e81c43c88fd287ea3b3eacc0
305
- * })
306
- * .catch(console.log);
307
- */
308
- /**
309
- * Allows you to get all ended auctions in around the last 60 seconds
310
- * @method
311
- * @name Client#getEndedSkyblockAuctions
312
- * @param {boolean} [includeItemBytes=false] - include item bytes (optional)
313
- * @param {MethodOptions} [options={}] Options
314
- * @return {Promise<{info:AuctionInfo,auctions:PartialAuction[]}>}
315
- * @example
316
- * hypixel.getEndedSkyblockAuctions().then(ended =>{
317
- * console.log(ended.auctions[0].auctionId); // 0fe7fd132367474e86ff3022b4a84a13
318
- * })
319
- * .catch(console.log);
320
- */
321
- /**
322
- * Allows you to get list of products
323
- * @method
324
- * @name Client#getSkyblockBazaar
325
- * @param {MethodOptions} [options={}] Options
326
- * @return {Promise<Product[]>}
327
- * @example
328
- * hypixel.getSkyblockBazaar().then(products =>{
329
- * console.log(products[0].productId); // INK_SACK:3
330
- * })
331
- * .catch(console.log);
332
- */
333
- /**
334
- * Allows you to get skyblock news
335
- * @method
336
- * @name Client#getSkyblockNews
337
- * @param {MethodOptions} [options={}] Options
338
- * @return {Promise<SkyblockNews[]>}
339
- * @example
340
- * hypixel.getSkyblockNews().then((news) => {
341
- * console.log(news[0].link); // https://hypixel.net/threads/3749492/
342
- * })
343
- * .catch(console.log)
344
- */
345
- /**
346
- * Allows you to get player count along with the player count of each public game
347
- * @method
348
- * @name Client#getGameCounts
349
- * @param {MethodOptions} [options={}] Options
350
- * @return {Promise<GameCounts>}
351
- * @example
352
- * hypixel.getGameCounts().then((gameCounts) => {
353
- * console.log(gameCounts.mainLobby.players);
354
- * })
355
- * .catch(console.log)
356
- */
357
- /**
358
- * Allows you to get Ranked SkyWars data of a player
359
- * @method
360
- * @name Client#getRankedSkyWars
361
- * @param {string} query Player nickname or uuid
362
- * @param {MethodOptions} [options={}] Options
363
- * @return {Promise<SkyWarsRanked>}
364
- * @example
365
- * hypixel.getRankedSkyWars('gypu').then((ranked) => {
366
- * console.log(ranked.position); // 4
367
- * }).catch(console.log);
368
- * @example
369
- * // if player has no stats for current ranked season
370
- * hypixel.getRankedSkyWars('StavZDev').then((ranked) => {
371
- * console.log(ranked); // null
372
- * }).catch(console.log) // throws 404 error;
373
- */
374
- /**
375
- * Delete x (by default all) cache entries
376
- * @param {?number} amount Amount of cache to delete
377
- * @return {Promise<void|boolean[]>}
378
- */
379
- sweepCache (amount) {
380
- return requests.sweepCache(amount);
381
- }
382
- }
383
- /**
384
- * @typedef {object} ClientOptions
385
- * @prop {boolean} [cache=false] Enable/Disable request caching.
386
- * @prop {number} [cacheTime=60] Amount of time in seconds to cache the requests.
387
- * @prop {AUTO|HARD|NONE} [rateLimit='AUTO'] Rate limit mode.
388
- * @prop {boolean} [syncWithHeaders=false] Sync with headers rate limit information. Usually not necessary nor recommended ( because of latency )
389
- * @prop {number} [keyLimit=120] Key limit of your key.
390
- * @prop {number} [cacheSize=-1] The amount how many results will be cached. (`-1` for infinity)
391
- * @prop {boolean} [silent=false] Don't automatically put warnings into console.
392
- * @prop {object} [headers={}] Extra Headers ( like User-Agent ) to add to request.
393
- * @prop {boolean} [checkForUpdates=false] Enable/Disable check for new version of hypixel-api-reborn.
394
- */
395
- /**
396
- * @typedef {object} MethodOptions
397
- * @property {boolean} [noCacheCheck=false] Disable/Enable cache checking
398
- * @property {boolean} [noCaching=false] Disable/Enable writing to cache
399
- * @prop {object} [headers={}] Extra Headers ( like User-Agent ) to add to request. Overrides the headers globally provided.
400
- */
401
- /**
402
- * @typedef {object} PlayerMethodOptions
403
- * @property {boolean} [noCacheCheck=false] Disable/Enable cache checking
404
- * @property {boolean} [noCaching=false] Disable/Enable writing to cache
405
- * @property {boolean} [guild=false] Disable/Enable request for player's guild
406
- * @property {boolean} [recentGames=false] Disable/Enable request for player's recent game
407
- * @property {boolean} [currentRankedSW=false] Disable/Enable request for player's current ranked SkyWars rating. Previous ratings will always show mindless of this option.
408
- * @prop {object} [headers={}] Extra Headers ( like User-Agent ) to add to request. Overrides the headers globally provided.
409
- */
410
- /**
411
- * @typedef {object} SkyblockMethodOptions
412
- * @property {?boolean} [noCacheCheck=false] Disable/Enable cache checking
413
- * @property {?boolean} [noCaching=false] Disable/Enable writing to cache
414
- * @property {?boolean} [fetchPlayer=false] Disable/Enable player profile request for each member
415
- * @prop {object} [headers={}] Extra Headers ( like User-Agent ) to add to request. Overrides the headers globally provided.
416
- */
417
- /**
418
- * @typedef {object} auctionsOptions
419
- * @property {boolean} [noCacheCheck=false] Disable/Enable cache checking
420
- * @property {boolean} [noCaching=false] Disable/Enable writing to cache
421
- * @property {boolean} [noInfo=false] If true, result doesn't show Auction Info
422
- * @property {boolean} [noAuctions=false] If true, result doesn't show auctions
423
- * @property {boolean} [raw=false] If true, result will not contain parsed auctions, but will present them as it is received. This can speed up performance in some cases.
424
- * @property {number} [retries=3] Number of times to retry fetching a page before abandoning
425
- * @property {number} [cooldown=100] Cooldown between each fetch, only works if race is unset or false;
426
- * @property {boolean} [race=false] Issues simultaneous requests to the API, instead of requesting then parsing one by one. Can largely increase speed at the cost of hogging bandwidth and memory
427
- * @property {boolean} [includeItemBytes=false] Whether to include item bytes in the result
428
- * @prop {object} [headers={}] Extra Headers ( like User-Agent ) to add to request. Overrides the headers globally provided.
429
- */
430
- module.exports = Client;
1
+ /* eslint-disable require-jsdoc */
2
+ /* eslint-disable max-len */
3
+ const validate = new (require('./Private/validate'))();
4
+ const rateLimit = new (require('./Private/rateLimit'))();
5
+ const Requests = require('./Private/requests');
6
+ const updater = new (require('./Private/updater'))();
7
+ const Errors = require('./Errors');
8
+ const API = require('./API/index');
9
+ const EventEmitter = require('events');
10
+ const clients = [];
11
+ /**
12
+ * Client class
13
+ */
14
+ class Client extends EventEmitter {
15
+ /**
16
+ * @param {string} key API key [(?)](https://stavzdev.is-inside.me/cCMiZdoy.gif)
17
+ * @param {ClientOptions} [options={}] Client options
18
+ */
19
+ constructor(key, options = {}) {
20
+ super();
21
+ this.requests = new Requests(this, options.cacheHandler);
22
+ // eslint-disable-next-line no-console
23
+ if (options && !options.silent) this.on('warn', console.warn);
24
+ // Test to check for multiple instances of client
25
+ if (clients.find((x) => x.key === key)) {
26
+ this.emit('warn', Errors.MULTIPLE_INSTANCES);
27
+ return clients.find((x) => x.key === key);
28
+ }
29
+ validate.validateNodeVersion();
30
+ this.key = validate.validateKey(key);
31
+ this.options = validate.parseOptions(options);
32
+ validate.validateOptions(this.options);
33
+ // eslint-disable-next-line guard-for-in
34
+ for (const func in API) {
35
+ Client.prototype[func] = function (...args) {
36
+ const lastArg = args[args.length - 1];
37
+ return API[func].apply(
38
+ {
39
+ _makeRequest: this._makeRequest.bind(this, { ...(validate.cacheSuboptions(lastArg) ? lastArg : {}) }),
40
+ ...this
41
+ },
42
+ args
43
+ );
44
+ };
45
+
46
+ if (this.options.checkForUpdates) {
47
+ updater.checkForUpdates();
48
+ }
49
+ }
50
+ /**
51
+ * All cache entries
52
+ * @type {Map<string,object>}
53
+ */
54
+ this.cache = this.requests.cache;
55
+ clients.push(this);
56
+ rateLimit.init(this.getPlayer('52d9a36f66ce4cdf9a56ad9724ae9fb4'), this.options, this).then(() => this.emit('ready'));
57
+ }
58
+ /**
59
+ * Private function - make request
60
+ * @param {MethodOptions} options Options for request
61
+ * @param {string} url Endpoint URL to request
62
+ * @param {boolean} [useRateLimitManager=true] Use rate limit
63
+ * @returns {Promise<Object>} Response
64
+ * @private
65
+ */
66
+ async _makeRequest(options, url, useRateLimitManager = true) {
67
+ if (!url) return;
68
+ if (url !== '/key' && !options.noCacheCheck && (await this.requests.cache.has(url))) return Object.assign(await this.requests.cache.get(url), { raw: !!options.raw });
69
+ if (useRateLimitManager) await rateLimit.rateLimitManager();
70
+ this.emit('outgoingRequest', url, { ...options, headers: { ...options.headers, ...this.options.headers } });
71
+ const result = await this.requests.request.call(this.requests, url, { ...options, headers: { ...options.headers, ...this.options.headers } });
72
+ if (this.options.syncWithHeaders) rateLimit.sync(result._headers);
73
+ return result;
74
+ }
75
+ /**
76
+ * Emitted when rate limiter is ready. ( You don't have to wait for this event to emit UNLESS you are planning to do data scraping which means spamming requests )
77
+ * @event
78
+ * @name Client#ready
79
+ * @example
80
+ * // This example gets player's uuid.
81
+ * hypixel.once('ready',()=>{
82
+ * hypixel.getPlayer('stavzdev')
83
+ * .then(player => player.uuid)
84
+ * .catch(console.log);
85
+ * })
86
+ */
87
+ /**
88
+ * Emitted when a request is going to be sent
89
+ * @event
90
+ * @name Client#outgoingRequest
91
+ * @param {string} url URL
92
+ * @param {object} [options] Options, if any
93
+ */
94
+ /**
95
+ * Emitted when there is a warning.
96
+ * @event
97
+ * @name Client#warn
98
+ * @param {string} error Warning Message
99
+ */
100
+ /**
101
+ * Allows you to get statistics of player
102
+ * @method
103
+ * @name Client#getPlayer
104
+ * @param {string} query Player nickname or UUID
105
+ * @param {PlayerMethodOptions} [options={}] Method options
106
+ * @return {Promise<Player>}
107
+ * @example
108
+ * hypixel.getPlayer('StavZDev').then(player => {
109
+ * console.log(player.level); // 141
110
+ * console.log(player.rank); // 'MVP+'
111
+ * }).catch(e => {
112
+ * console.log(e);
113
+ * })
114
+ * @example
115
+ * // Get player's guild along with player stats
116
+ * hypixel.getPlayer('Minikloon', { guild: true }).then(player => {
117
+ * console.log(player.guild) // null if player isn't is guild
118
+ * console.log(player.guild.name) // Mini Squid
119
+ * console.log(player.guild.level) // 110
120
+ * }).catch(e => {
121
+ * console.log(e);
122
+ * });
123
+ * @example
124
+ * // async/await
125
+ * const player = await hypixel.getPlayer('Minikloon').catch(console.log);
126
+ * // If player doesn't exist, or if an error occurred ( check console )
127
+ * console.log(player); // null
128
+ * // Check if player exists
129
+ * if(player) console.log(player.uuid); // 20934ef9488c465180a78f861586b4cf
130
+ */
131
+ /**
132
+ * Allows you to get statistics of hypixel guild
133
+ * @method
134
+ * @name Client#getGuild
135
+ * @param {id|name|player} searchParameter Search for guild by id, name or player (if player is in guild)
136
+ * @param {string} query Guild ID, Guild name or player uuid/nickname
137
+ * @param {MethodOptions} [options={}] Method options
138
+ * @return {Promise<Guild>}
139
+ * @example
140
+ * hypixel.getGuild('name', 'The Foundation').then(guild => {
141
+ * console.log(guild.level); // 111
142
+ * console.log(guild.id); // '52e5719284ae51ed0c716c69'
143
+ * }).catch(e => {
144
+ * console.log(e);
145
+ * })
146
+ */
147
+ /**
148
+ * Allows you to get statistics of watchdog, the server anticheat
149
+ * @method
150
+ * @name Client#getWatchdogStats
151
+ * @param {MethodOptions} [options={}] Method options
152
+ * @return {Promise<WatchdogStats>}
153
+ * @example
154
+ * hypixel.getWatchdogStats().then(watchdog => {
155
+ * console.log(watchdog.byWatchdogTotal); // 5931897
156
+ * }).catch(e => {
157
+ * console.log(e);
158
+ * })
159
+ */
160
+ /**
161
+ * Allows you to get all active boosters
162
+ * @method
163
+ * @name Client#getBoosters
164
+ * @param {MethodOptions} [options={}] Method options
165
+ * @return {Promise<Array<Booster>>}
166
+ * @example
167
+ * hypixel.getBoosters().then(boosters => {
168
+ * console.log(boosters[0].purchaser); // '978ddb705a8e43618e41749178c020b0'
169
+ * }).catch(e => {
170
+ * console.log(e);
171
+ * })
172
+ */
173
+ /**
174
+ * Allows you to get all skyblock profiles of player
175
+ * @method
176
+ * @name Client#getSkyblockProfiles
177
+ * @param {string} query Player nickname or UUID
178
+ * @param {SkyblockMethodOptions} [options={}] Method options
179
+ * @return {Promise<Array<SkyblockProfile>>}
180
+ * @example
181
+ * hypixel.getSkyblockProfiles('StavZDev').then(profiles => {
182
+ * console.log(profiles[0].members[0].uuid); // '52d9a36f66ce4cdf9a56ad9724ae9fb4'
183
+ * }).catch(e => {
184
+ * console.log(e);
185
+ * })
186
+ */
187
+ /**
188
+ * Allows you to get a player's skyblock member data from all their profiles
189
+ * @method
190
+ * @name Client#getSkyblockMember
191
+ * @param {string} query Player nickname or UUID
192
+ * @param {MethodOptions} [options={}] Method options
193
+ * @return {Promise<Map<string,SkyblockMember>>}
194
+ * @example
195
+ * hypixel.getSkyblockMember('StavZDev').then(member => {
196
+ * // 'Cucumber' - profile name
197
+ * console.log(member.get('Cucumber').uuid); // '52d9a36f66ce4cdf9a56ad9724ae9fb4'
198
+ * }).catch(e => {
199
+ * console.log(e);
200
+ * })
201
+ */
202
+ /**
203
+ * Allows you to get the Hypixel API's Status and past Incidents, no key needed.
204
+ * @method
205
+ * @name Client#getAPIStatus
206
+ * @return {Promise<APIStatus>}
207
+ * @example
208
+ * hypixel.getAPIStatus().then(status => {
209
+ * console.log(status.incidents[0].link); // 'https://status.hypixel.net/incidents/zdd5gppdtcc3'
210
+ * }).catch(e => {
211
+ * console.log(e);
212
+ * })
213
+ */
214
+ /**
215
+ * Gets all leaderboards
216
+ * @method
217
+ * @name Client#getLeaderboards
218
+ * @param {MethodOptions} [options={}] Method options
219
+ * @return {Promise<{ ARENA: Leaderboard[], COPS_AND_CRIMS: Leaderboard[], WARLORDS: Leaderboard[], BLITZ_SURVIVAL_GAMES: Leaderboard[], UHC: Leaderboard[], WALLS: Leaderboard[], PROTOTYPE: Leaderboard[], PAINTBALL: Leaderboard[], SKYWARS: Leaderboard[], MURDER_MYSTERY: Leaderboard[], SMASH_HEROES: Leaderboard[], DUELS: Leaderboard[], SPEED_UHC: Leaderboard[], TNTGAMES: Leaderboard[], BEDWARS: Leaderboard[], TURBO_KART_RACERS: Leaderboard[], BUILD_BATTLE: Leaderboard[], ARCADE: Leaderboard[], SKYCLASH: Leaderboard[], QUAKECRAFT: Leaderboard[], CRAZY_WALLS: Leaderboard[], MEGA_WALLS: Leaderboard[], VAMPIREZ: Leaderboard[] }>}
220
+ * @example
221
+ * hypixel.getLeaderboards().then(leaderboards => {
222
+ * console.log(leaderboards.ARENA[0].name); // 'Wins'
223
+ * }).catch(e => {
224
+ * console.log(e);
225
+ * })
226
+ */
227
+ /**
228
+ * Sends a STATUS packet to hypixel and parses the return info (a 0x00 and 0x01 packet)
229
+ * @method
230
+ * @name Client#getServerInfo
231
+ * @param {number} [repeats=3] Sends x amount of ping requests and gets the average. Should be between 1 and 10
232
+ * @return {Promise<ServerInfo>}
233
+ * @example
234
+ * hypixel.getServerInfo(3).then(serverInfo =>{
235
+ * console.log(serverInfo.ping); // 69
236
+ * }).catch(console.log)
237
+ */
238
+ /**
239
+ * Allows you to get recent games of a player
240
+ * @method
241
+ * @name Client#getRecentGames
242
+ * @param {string} query Player nickname or UUID
243
+ * @param {MethodOptions} [options={}] Method options
244
+ * @return {Promise<RecentGame[]>}
245
+ * @example
246
+ * hypixel.getRecentGames().then(recentGames =>{
247
+ * console.log(recentGames[0].endedTimestamp); // 1609670588789
248
+ * })
249
+ * .catch(console.log);
250
+ */
251
+ /**
252
+ * Allows you to get the status of a player
253
+ * @method
254
+ * @name Client#getStatus
255
+ * @param {string} query Player nickname or UUID
256
+ * @param {MethodOptions} [options={}] Method options
257
+ * @return {Promise<Status>}
258
+ * @example
259
+ * hypixel.getStatus('Stavzdev').then(status =>{
260
+ * console.log(status.online); // true
261
+ * })
262
+ * .catch(console.log);
263
+ */
264
+ /**
265
+ * Allows you to get skyblock auctions
266
+ * @method
267
+ * @name Client#getSkyblockAuctions
268
+ * @param {string|number|number[]} page - "*", a page number, or an array with the start and the end page number ( automatically sorted )
269
+ * @param {auctionsOptions} [options={}] Options
270
+ * @return {Promise<{info:AuctionInfo,auctions:Auction[]}>}
271
+ * @example
272
+ * hypixel.getSkyblockAuctions(0).then(auctions =>{
273
+ * console.log(auctions[0].item); // Mythic Farmer Boots
274
+ * })
275
+ * .catch(console.log);
276
+ */
277
+ /**
278
+ * Allows you to get player's skyblock auctions
279
+ * @method
280
+ * @name Client#getSkyblockAuctionsByPlayer
281
+ * @param {string} query - player nickname or uuid
282
+ * @param {boolean} [includeItemBytes=false] - include item bytes (optional)
283
+ * @param {MethodOptions} [options={}] Options
284
+ * @return {Promise<Auction[]>}
285
+ * @example
286
+ * hypixel.getSkyblockAuctionsByPlayer('hypixel').then(auctions =>{
287
+ * console.log(auctions[0].auctionId); // b0491da3e81c43c88fd287ea3b3eacc0
288
+ * })
289
+ * .catch(console.log);
290
+ */
291
+ /**
292
+ * Allows you to get all ended auctions in around the last 60 seconds
293
+ * @method
294
+ * @name Client#getEndedSkyblockAuctions
295
+ * @param {boolean} [includeItemBytes=false] - include item bytes (optional)
296
+ * @param {MethodOptions} [options={}] Options
297
+ * @return {Promise<{info:AuctionInfo,auctions:PartialAuction[]}>}
298
+ * @example
299
+ * hypixel.getEndedSkyblockAuctions().then(ended =>{
300
+ * console.log(ended.auctions[0].auctionId); // 0fe7fd132367474e86ff3022b4a84a13
301
+ * })
302
+ * .catch(console.log);
303
+ */
304
+ /**
305
+ * Allows you to get list of products
306
+ * @method
307
+ * @name Client#getSkyblockBazaar
308
+ * @param {MethodOptions} [options={}] Options
309
+ * @return {Promise<Product[]>}
310
+ * @example
311
+ * hypixel.getSkyblockBazaar().then(products =>{
312
+ * console.log(products[0].productId); // INK_SACK:3
313
+ * })
314
+ * .catch(console.log);
315
+ */
316
+ /**
317
+ * Allows you to get skyblock news
318
+ * @method
319
+ * @name Client#getSkyblockNews
320
+ * @param {MethodOptions} [options={}] Options
321
+ * @return {Promise<SkyblockNews[]>}
322
+ * @example
323
+ * hypixel.getSkyblockNews().then((news) => {
324
+ * console.log(news[0].link); // https://hypixel.net/threads/3749492/
325
+ * })
326
+ * .catch(console.log)
327
+ */
328
+ /**
329
+ * Allows you to get player count along with the player count of each public game
330
+ * @method
331
+ * @name Client#getGameCounts
332
+ * @param {MethodOptions} [options={}] Options
333
+ * @return {Promise<GameCounts>}
334
+ * @example
335
+ * hypixel.getGameCounts().then((gameCounts) => {
336
+ * console.log(gameCounts.mainLobby.players);
337
+ * })
338
+ * .catch(console.log)
339
+ */
340
+ /**
341
+ * Delete x (by default all) cache entries
342
+ * @param {?number} amount Amount of cache to delete
343
+ * @return {Promise<void|boolean[]>}
344
+ */
345
+ sweepCache(amount) {
346
+ return this.requests.sweepCache(amount);
347
+ }
348
+ }
349
+ /**
350
+ * @typedef {object} ClientOptions
351
+ * @prop {boolean} [cache=false] Enable/Disable request caching.
352
+ * @prop {number} [cacheTime=60] Amount of time in seconds to cache the requests.
353
+ * @prop {CacheHandler} [cacheHandler] Custom Cache Handler
354
+ * @prop {AUTO|HARD|NONE} [rateLimit='AUTO'] Rate limit mode.
355
+ * @prop {boolean} [syncWithHeaders=false] Sync with headers rate limit information. Usually not necessary nor recommended ( because of latency )
356
+ * @prop {number} [keyLimit=120] Key limit of your key.
357
+ * @prop {number} [cacheSize=-1] The amount how many results will be cached. (`-1` for infinity)
358
+ * @prop {boolean} [silent=false] Don't automatically put warnings into console.
359
+ * @prop {object} [headers={}] Extra Headers ( like User-Agent ) to add to request.
360
+ * @prop {boolean} [checkForUpdates=false] Enable/Disable check for new version of hypixel-api-reborn.
361
+ */
362
+ const defaultCache = require('./Private/defaultCache.js');
363
+ /**
364
+ * @typedef {defaultCache} Cache
365
+ */
366
+ /**
367
+ * @typedef {Cache} CacheHandler
368
+ */
369
+ /**
370
+ * @typedef {object} MethodOptions
371
+ * @property {boolean} [raw=false] Raw data
372
+ * @property {boolean} [noCacheCheck=false] Disable/Enable cache checking
373
+ * @property {boolean} [noCaching=false] Disable/Enable writing to cache
374
+ * @prop {object} [headers={}] Extra Headers ( like User-Agent ) to add to request. Overrides the headers globally provided.
375
+ */
376
+ /**
377
+ * @typedef {object} PlayerMethodOptions
378
+ * @property {boolean} [raw=false] Raw data
379
+ * @property {boolean} [noCacheCheck=false] Disable/Enable cache checking
380
+ * @property {boolean} [noCaching=false] Disable/Enable writing to cache
381
+ * @property {boolean} [guild=false] Disable/Enable request for player's guild
382
+ * @property {boolean} [recentGames=false] Disable/Enable request for player's recent game
383
+ * @prop {object} [headers={}] Extra Headers ( like User-Agent ) to add to request. Overrides the headers globally provided.
384
+ */
385
+ /**
386
+ * @typedef {object} SkyblockMethodOptions
387
+ * @property {boolean} [raw=false] Raw data
388
+ * @property {?boolean} [noCacheCheck=false] Disable/Enable cache checking
389
+ * @property {?boolean} [noCaching=false] Disable/Enable writing to cache
390
+ * @property {?boolean} [fetchPlayer=false] Disable/Enable player profile request for each member
391
+ * @prop {object} [headers={}] Extra Headers ( like User-Agent ) to add to request. Overrides the headers globally provided.
392
+ */
393
+ /**
394
+ * @typedef {object} auctionsOptions
395
+ * @property {boolean} [noCacheCheck=false] Disable/Enable cache checking
396
+ * @property {boolean} [noCaching=false] Disable/Enable writing to cache
397
+ * @property {boolean} [noInfo=false] If true, result doesn't show Auction Info
398
+ * @property {boolean} [noAuctions=false] If true, result doesn't show auctions
399
+ * @property {boolean} [raw=false] If true, result will not contain parsed auctions, but will present them as it is received. This can speed up performance in some cases.
400
+ * @property {number} [retries=3] Number of times to retry fetching a page before abandoning
401
+ * @property {number} [cooldown=100] Cooldown between each fetch, only works if race is unset or false;
402
+ * @property {boolean} [race=false] Issues simultaneous requests to the API, instead of requesting then parsing one by one. Can largely increase speed at the cost of hogging bandwidth and memory
403
+ * @property {boolean} [includeItemBytes=false] Whether to include item bytes in the result
404
+ * @prop {object} [headers={}] Extra Headers ( like User-Agent ) to add to request. Overrides the headers globally provided.
405
+ */
406
+ module.exports = Client;