hypixel-api-reborn 10.0.0 → 11.0.1

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 -8
  5. package/src/API/getBoosters.js +6 -6
  6. package/src/API/getGameCounts.js +6 -6
  7. package/src/API/getGuild.js +18 -18
  8. package/src/API/getLeaderboards.js +13 -13
  9. package/src/API/getPlayer.js +22 -27
  10. package/src/API/getRecentGames.js +18 -18
  11. package/src/API/getServerInfo.js +66 -77
  12. package/src/API/getStatus.js +8 -8
  13. package/src/API/getWatchdogStats.js +6 -6
  14. package/src/API/index.js +19 -23
  15. package/src/API/skyblock/getEndedSkyblockAuctions.js +10 -10
  16. package/src/API/skyblock/getSkyblockAuctions.js +64 -64
  17. package/src/API/skyblock/getSkyblockAuctionsByPlayer.js +11 -11
  18. package/src/API/skyblock/getSkyblockBazaar.js +10 -10
  19. package/src/API/skyblock/getSkyblockMember.js +28 -25
  20. package/src/API/skyblock/getSkyblockNews.js +6 -6
  21. package/src/API/skyblock/getSkyblockProfiles.js +44 -41
  22. package/src/Client.js +406 -450
  23. package/src/Errors.js +44 -39
  24. package/src/Private/defaultCache.js +77 -78
  25. package/src/Private/rateLimit.js +79 -98
  26. package/src/Private/requests.js +65 -54
  27. package/src/Private/updater.js +45 -45
  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 -351
  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 -630
  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 -176
  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 -449
  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 -130
  79. package/src/structures/SkyBlock/SkyblockMember.js +820 -687
  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 -809
  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 -13
  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 -8
  95. package/src/utils/romanize.js +11 -13
  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 -2644
  100. package/src/API/getFriends.js +0 -14
  101. package/src/API/getKeyInfo.js +0 -10
  102. package/src/API/getRankedSkyWars.js +0 -11
  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,450 +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 = 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
- if (this.options.checkForUpdates) {
46
- updater.checkForUpdates();
47
- }
48
- }
49
- /**
50
- * All cache entries
51
- * @type {Map<string,object>}
52
- */
53
- this.cache = this.requests.cache;
54
- clients.push(this);
55
- rateLimit.init(this.getKeyInfo(), this.options, this).then(() => this.emit('ready'));
56
- }
57
- /**
58
- * Private function - make request
59
- * @param {MethodOptions} options Options for request
60
- * @param {string} url Endpoint URL to request
61
- * @param {boolean} [useRateLimitManager=true] Use rate limit
62
- * @returns {Promise<Object>} Response
63
- * @private
64
- */
65
- async _makeRequest (options, url, useRateLimitManager = true) {
66
- if (!url) return;
67
- if (url !== '/key' && !options.noCacheCheck && await this.requests.cache.has(url)) return Object.assign(await this.requests.cache.get(url), { raw: !!options.raw });
68
- if (useRateLimitManager) await rateLimit.rateLimitManager();
69
- this.emit('outgoingRequest', url, { ...options, headers: { ...options.headers, ...this.options.headers } });
70
- const result = await this.requests.request.call(this.requests, url, { ...options, headers: { ...options.headers, ...this.options.headers } });
71
- if (this.options.syncWithHeaders) rateLimit.sync(result._headers);
72
- return result;
73
- }
74
- /**
75
- * Emitted when a request is going to be sent
76
- * @event
77
- * @name Client#outgoingRequest
78
- * @param {string} url URL
79
- * @param {object} [options] Options, if any
80
- */
81
- /**
82
- * 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 )
83
- * @event
84
- * @name Client#ready
85
- * @example
86
- * // This example gets the first 100 friends of a player and gets their stats.
87
- * hypixel.once('ready',()=>{
88
- * hypixel.getFriends('StavZDev')
89
- * .then(friends => friends.map(x=>x.uuid).slice(0, 100).map(hypixel.getPlayer))
90
- * .catch(console.log);
91
- * })
92
- */
93
- /**
94
- * Emitted when there is a warning.
95
- * @event
96
- * @name Client#warn
97
- * @param {string} error Warning Message
98
- */
99
- /**
100
- * Allows you to get statistics of player
101
- * @method
102
- * @name Client#getPlayer
103
- * @param {string} query Player nickname or UUID
104
- * @param {PlayerMethodOptions} [options={}] Method options
105
- * @return {Promise<Player>}
106
- * @example
107
- * hypixel.getPlayer('StavZDev').then(player => {
108
- * console.log(player.level); // 141
109
- * console.log(player.rank); // 'MVP+'
110
- * }).catch(e => {
111
- * console.log(e);
112
- * })
113
- * @example
114
- * // Get player's guild along with player stats
115
- * hypixel.getPlayer('Minikloon', { guild: true }).then(player => {
116
- * console.log(player.guild) // null if player isn't is guild
117
- * console.log(player.guild.name) // Mini Squid
118
- * console.log(player.guild.level) // 110
119
- * }).catch(e => {
120
- * console.log(e);
121
- * });
122
- * @example
123
- * // async/await
124
- * const player = await hypixel.getPlayer('Minikloon').catch(console.log);
125
- * // If player doesn't exist, or if an error occurred ( check console )
126
- * console.log(player); // null
127
- * // Check if player exists
128
- * if(player) console.log(player.uuid); // 20934ef9488c465180a78f861586b4cf
129
- */
130
- /**
131
- * Allows you to get statistics of hypixel guild
132
- * @method
133
- * @name Client#getGuild
134
- * @param {id|name|player} searchParameter Search for guild by id, name or player (if player is in guild)
135
- * @param {string} query Guild ID, Guild name or player uuid/nickname
136
- * @param {MethodOptions} [options={}] Method options
137
- * @return {Promise<Guild>}
138
- * @example
139
- * hypixel.getGuild('name', 'The Foundation').then(guild => {
140
- * console.log(guild.level); // 111
141
- * console.log(guild.id); // '52e5719284ae51ed0c716c69'
142
- * }).catch(e => {
143
- * console.log(e);
144
- * })
145
- */
146
- /**
147
- * Allows you get player's list of friends
148
- * @method
149
- * @name Client#getFriends
150
- * @param {string} query Player nickname or UUID
151
- * @param {MethodOptions} [options={}] Method options
152
- * @return {Promise<Array<Friend>>}
153
- * @example
154
- * hypixel.getFriends('StavZDev').then(friends => {
155
- * console.log(friends[0].friendSinceTimestamp); // 1528363745834
156
- * }).catch(e => {
157
- * console.log(e);
158
- * })
159
- */
160
- /**
161
- * Allows you to get statistics of watchdog, the server anticheat
162
- * @method
163
- * @name Client#getWatchdogStats
164
- * @param {MethodOptions} [options={}] Method options
165
- * @return {Promise<WatchdogStats>}
166
- * @example
167
- * hypixel.getWatchdogStats().then(watchdog => {
168
- * console.log(watchdog.byWatchdogTotal); // 5931897
169
- * }).catch(e => {
170
- * console.log(e);
171
- * })
172
- */
173
- /**
174
- * Allows you to get all active boosters
175
- * @method
176
- * @name Client#getBoosters
177
- * @param {MethodOptions} [options={}] Method options
178
- * @return {Promise<Array<Booster>>}
179
- * @example
180
- * hypixel.getBoosters().then(boosters => {
181
- * console.log(boosters[0].purchaser); // '978ddb705a8e43618e41749178c020b0'
182
- * }).catch(e => {
183
- * console.log(e);
184
- * })
185
- */
186
- /**
187
- * Allows you to get all skyblock profiles of player
188
- * @method
189
- * @name Client#getSkyblockProfiles
190
- * @param {string} query Player nickname or UUID
191
- * @param {SkyblockMethodOptions} [options={}] Method options
192
- * @return {Promise<Array<SkyblockProfile>>}
193
- * @example
194
- * hypixel.getSkyblockProfiles('StavZDev').then(profiles => {
195
- * console.log(profiles[0].members[0].uuid); // '52d9a36f66ce4cdf9a56ad9724ae9fb4'
196
- * }).catch(e => {
197
- * console.log(e);
198
- * })
199
- */
200
- /**
201
- * Allows you to get a player's skyblock member data from all their profiles
202
- * @method
203
- * @name Client#getSkyblockMember
204
- * @param {string} query Player nickname or UUID
205
- * @param {MethodOptions} [options={}] Method options
206
- * @return {Promise<Map<string,SkyblockMember>>}
207
- * @example
208
- * hypixel.getSkyblockMember('StavZDev').then(member => {
209
- * // 'Cucumber' - profile name
210
- * console.log(member.get('Cucumber').uuid); // '52d9a36f66ce4cdf9a56ad9724ae9fb4'
211
- * }).catch(e => {
212
- * console.log(e);
213
- * })
214
- */
215
- /**
216
- * Allows you to get the Hypixel API's Status and past Incidents, no key needed.
217
- * @method
218
- * @name Client#getAPIStatus
219
- * @return {Promise<APIStatus>}
220
- * @example
221
- * hypixel.getAPIStatus().then(status => {
222
- * console.log(status.incidents[0].link); // 'https://status.hypixel.net/incidents/zdd5gppdtcc3'
223
- * }).catch(e => {
224
- * console.log(e);
225
- * })
226
- */
227
- /**
228
- * Gets Key information
229
- * @method
230
- * @name Client#getKeyInfo
231
- * @param {MethodOptions} [options={}] Method options
232
- * @return {Promise<KeyInfo>}
233
- * @example
234
- * hypixel.getKeyInfo().then(keyInfo => {
235
- * console.log(keyInfo.owner); // '52d9a36f66ce4cdf9a56ad9724ae9fb4'
236
- * }).catch(e => {
237
- * console.log(e);
238
- * })
239
- */
240
- /**
241
- * Gets all leaderboards
242
- * @method
243
- * @name Client#getLeaderboards
244
- * @param {MethodOptions} [options={}] Method options
245
- * @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[] }>}
246
- * @example
247
- * hypixel.getLeaderboards().then(leaderboards => {
248
- * console.log(leaderboards.ARENA[0].name); // 'Wins'
249
- * }).catch(e => {
250
- * console.log(e);
251
- * })
252
- */
253
- /**
254
- * Sends a STATUS packet to hypixel and parses the return info (a 0x00 and 0x01 packet)
255
- * @method
256
- * @name Client#getServerInfo
257
- * @param {number} [repeats=3] Sends x amount of ping requests and gets the average. Should be between 1 and 10
258
- * @return {Promise<ServerInfo>}
259
- * @example
260
- * hypixel.getServerInfo(3).then(serverInfo =>{
261
- * console.log(serverInfo.ping); // 69
262
- * }).catch(console.log)
263
- */
264
- /**
265
- * Allows you to get recent games of a player
266
- * @method
267
- * @name Client#getRecentGames
268
- * @param {string} query Player nickname or UUID
269
- * @param {MethodOptions} [options={}] Method options
270
- * @return {Promise<RecentGame[]>}
271
- * @example
272
- * hypixel.getRecentGames().then(recentGames =>{
273
- * console.log(recentGames[0].endedTimestamp); // 1609670588789
274
- * })
275
- * .catch(console.log);
276
- */
277
- /**
278
- * Allows you to get the status of a player
279
- * @method
280
- * @name Client#getStatus
281
- * @param {string} query Player nickname or UUID
282
- * @param {MethodOptions} [options={}] Method options
283
- * @return {Promise<Status>}
284
- * @example
285
- * hypixel.getStatus('Stavzdev').then(status =>{
286
- * console.log(status.online); // true
287
- * })
288
- * .catch(console.log);
289
- */
290
- /**
291
- * Allows you to get skyblock auctions
292
- * @method
293
- * @name Client#getSkyblockAuctions
294
- * @param {string|number|number[]} page - "*", a page number, or an array with the start and the end page number ( automatically sorted )
295
- * @param {auctionsOptions} [options={}] Options
296
- * @return {Promise<{info:AuctionInfo,auctions:Auction[]}>}
297
- * @example
298
- * hypixel.getSkyblockAuctions(0).then(auctions =>{
299
- * console.log(auctions[0].item); // Mythic Farmer Boots
300
- * })
301
- * .catch(console.log);
302
- */
303
- /**
304
- * Allows you to get player's skyblock auctions
305
- * @method
306
- * @name Client#getSkyblockAuctionsByPlayer
307
- * @param {string} query - player nickname or uuid
308
- * @param {boolean} [includeItemBytes=false] - include item bytes (optional)
309
- * @param {MethodOptions} [options={}] Options
310
- * @return {Promise<Auction[]>}
311
- * @example
312
- * hypixel.getSkyblockAuctionsByPlayer('hypixel').then(auctions =>{
313
- * console.log(auctions[0].auctionId); // b0491da3e81c43c88fd287ea3b3eacc0
314
- * })
315
- * .catch(console.log);
316
- */
317
- /**
318
- * Allows you to get all ended auctions in around the last 60 seconds
319
- * @method
320
- * @name Client#getEndedSkyblockAuctions
321
- * @param {boolean} [includeItemBytes=false] - include item bytes (optional)
322
- * @param {MethodOptions} [options={}] Options
323
- * @return {Promise<{info:AuctionInfo,auctions:PartialAuction[]}>}
324
- * @example
325
- * hypixel.getEndedSkyblockAuctions().then(ended =>{
326
- * console.log(ended.auctions[0].auctionId); // 0fe7fd132367474e86ff3022b4a84a13
327
- * })
328
- * .catch(console.log);
329
- */
330
- /**
331
- * Allows you to get list of products
332
- * @method
333
- * @name Client#getSkyblockBazaar
334
- * @param {MethodOptions} [options={}] Options
335
- * @return {Promise<Product[]>}
336
- * @example
337
- * hypixel.getSkyblockBazaar().then(products =>{
338
- * console.log(products[0].productId); // INK_SACK:3
339
- * })
340
- * .catch(console.log);
341
- */
342
- /**
343
- * Allows you to get skyblock news
344
- * @method
345
- * @name Client#getSkyblockNews
346
- * @param {MethodOptions} [options={}] Options
347
- * @return {Promise<SkyblockNews[]>}
348
- * @example
349
- * hypixel.getSkyblockNews().then((news) => {
350
- * console.log(news[0].link); // https://hypixel.net/threads/3749492/
351
- * })
352
- * .catch(console.log)
353
- */
354
- /**
355
- * Allows you to get player count along with the player count of each public game
356
- * @method
357
- * @name Client#getGameCounts
358
- * @param {MethodOptions} [options={}] Options
359
- * @return {Promise<GameCounts>}
360
- * @example
361
- * hypixel.getGameCounts().then((gameCounts) => {
362
- * console.log(gameCounts.mainLobby.players);
363
- * })
364
- * .catch(console.log)
365
- */
366
- /**
367
- * Allows you to get Ranked SkyWars data for current season of a player
368
- * @method
369
- * @name Client#getRankedSkyWars
370
- * @param {string} query Player nickname or uuid
371
- * @param {MethodOptions} [options={}] Options
372
- * @return {Promise<SkyWarsRanked>}
373
- * @example
374
- * hypixel.getRankedSkyWars('gypu').then((ranked) => {
375
- * console.log(ranked.position); // 4
376
- * }).catch(console.log);
377
- * @example
378
- * // if player has no stats for current ranked season
379
- * hypixel.getRankedSkyWars('StavZDev').then((ranked) => {
380
- * console.log(ranked); // null
381
- * }).catch(console.log) // throws 404 error;
382
- */
383
- /**
384
- * Delete x (by default all) cache entries
385
- * @param {?number} amount Amount of cache to delete
386
- * @return {Promise<void|boolean[]>}
387
- */
388
- sweepCache (amount) {
389
- return this.requests.sweepCache(amount);
390
- }
391
- }
392
- /**
393
- * @typedef {object} ClientOptions
394
- * @prop {boolean} [cache=false] Enable/Disable request caching.
395
- * @prop {number} [cacheTime=60] Amount of time in seconds to cache the requests.
396
- * @prop {CacheHandler} [cacheHandler] Custom Cache Handler
397
- * @prop {AUTO|HARD|NONE} [rateLimit='AUTO'] Rate limit mode.
398
- * @prop {boolean} [syncWithHeaders=false] Sync with headers rate limit information. Usually not necessary nor recommended ( because of latency )
399
- * @prop {number} [keyLimit=120] Key limit of your key.
400
- * @prop {number} [cacheSize=-1] The amount how many results will be cached. (`-1` for infinity)
401
- * @prop {boolean} [silent=false] Don't automatically put warnings into console.
402
- * @prop {object} [headers={}] Extra Headers ( like User-Agent ) to add to request.
403
- * @prop {boolean} [checkForUpdates=false] Enable/Disable check for new version of hypixel-api-reborn.
404
- */
405
- const defaultCache = require('./Private/defaultCache.js');
406
- /**
407
- * @typedef {defaultCache} Cache
408
- */
409
- /**
410
- * @typedef {Cache} CacheHandler
411
- */
412
- /**
413
- * @typedef {object} MethodOptions
414
- * @property {boolean} [raw=false] Raw data
415
- * @property {boolean} [noCacheCheck=false] Disable/Enable cache checking
416
- * @property {boolean} [noCaching=false] Disable/Enable writing to cache
417
- * @prop {object} [headers={}] Extra Headers ( like User-Agent ) to add to request. Overrides the headers globally provided.
418
- */
419
- /**
420
- * @typedef {object} PlayerMethodOptions
421
- * @property {boolean} [raw=false] Raw data
422
- * @property {boolean} [noCacheCheck=false] Disable/Enable cache checking
423
- * @property {boolean} [noCaching=false] Disable/Enable writing to cache
424
- * @property {boolean} [guild=false] Disable/Enable request for player's guild
425
- * @property {boolean} [recentGames=false] Disable/Enable request for player's recent game
426
- * @property {boolean} [currentRankedSW=false] Disable/Enable request for player's current ranked SkyWars rating. Previous ratings will always show mindless of this option.
427
- * @prop {object} [headers={}] Extra Headers ( like User-Agent ) to add to request. Overrides the headers globally provided.
428
- */
429
- /**
430
- * @typedef {object} SkyblockMethodOptions
431
- * @property {boolean} [raw=false] Raw data
432
- * @property {?boolean} [noCacheCheck=false] Disable/Enable cache checking
433
- * @property {?boolean} [noCaching=false] Disable/Enable writing to cache
434
- * @property {?boolean} [fetchPlayer=false] Disable/Enable player profile request for each member
435
- * @prop {object} [headers={}] Extra Headers ( like User-Agent ) to add to request. Overrides the headers globally provided.
436
- */
437
- /**
438
- * @typedef {object} auctionsOptions
439
- * @property {boolean} [noCacheCheck=false] Disable/Enable cache checking
440
- * @property {boolean} [noCaching=false] Disable/Enable writing to cache
441
- * @property {boolean} [noInfo=false] If true, result doesn't show Auction Info
442
- * @property {boolean} [noAuctions=false] If true, result doesn't show auctions
443
- * @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.
444
- * @property {number} [retries=3] Number of times to retry fetching a page before abandoning
445
- * @property {number} [cooldown=100] Cooldown between each fetch, only works if race is unset or false;
446
- * @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
447
- * @property {boolean} [includeItemBytes=false] Whether to include item bytes in the result
448
- * @prop {object} [headers={}] Extra Headers ( like User-Agent ) to add to request. Overrides the headers globally provided.
449
- */
450
- 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;