diff --git a/lib/friends/getFollowerCount.js b/lib/friends/getFollowerCount.js index cef07304..49cf3cc8 100644 --- a/lib/friends/getFollowerCount.js +++ b/lib/friends/getFollowerCount.js @@ -16,9 +16,9 @@ exports.required = ['userId'] **/ // Define -exports.func = function (userId) { +exports.func = function (args) { const httpOpt = { - url: `//friends.roblox.com/v1/users/${userId}/followers/count`, + url: `//friends.roblox.com/v1/users/${args.userId}/followers/count`, options: { json: true, method: 'GET', @@ -30,7 +30,7 @@ exports.func = function (userId) { if (res.statusCode === 200) { return res.body.count } throw new Error( - `Failed to retrieve follower count: (${res.statusCode}) ${res.body}` + `Failed to retrieve follower count: (${res.statusCode}) ${JSON.stringify(res.body)}` ) }) } diff --git a/lib/friends/getFollowingCount.js b/lib/friends/getFollowingCount.js index bd43cc94..7a454839 100644 --- a/lib/friends/getFollowingCount.js +++ b/lib/friends/getFollowingCount.js @@ -16,9 +16,9 @@ exports.required = ['userId'] **/ // Define -exports.func = function (userId) { +exports.func = function (args) { const httpOpt = { - url: `//friends.roblox.com/v1/users/${userId}/followings/count`, + url: `//friends.roblox.com/v1/users/${args.userId}/followings/count`, options: { json: true, method: 'GET', @@ -30,7 +30,7 @@ exports.func = function (userId) { if (res.statusCode === 200) { return res.body.count } throw new Error( - `Failed to retrieve following count: (${res.statusCode}) ${res.body}` + `Failed to retrieve following count: (${res.statusCode}) ${JSON.stringify(res.body)}` ) }) } diff --git a/lib/friends/getFriendCount.js b/lib/friends/getFriendCount.js index 72ceb1cb..912dd1ff 100644 --- a/lib/friends/getFriendCount.js +++ b/lib/friends/getFriendCount.js @@ -16,9 +16,9 @@ exports.required = ['userId'] **/ // Define -exports.func = function (userId) { +exports.func = function (args) { const httpOpt = { - url: `//friends.roblox.com/v1/users/${userId}/friends/count`, + url: `//friends.roblox.com/v1/users/${args.userId}/friends/count`, options: { json: true, method: 'GET', @@ -30,7 +30,7 @@ exports.func = function (userId) { if (res.statusCode === 200) { return res.body.count } throw new Error( - `Failed to retrieve friend count: (${res.statusCode}) ${res.body}` + `Failed to retrieve friend count: (${res.statusCode}) ${JSON.stringify(res.body)}` ) }) } diff --git a/lib/users/getPlayerInfo.js b/lib/users/getPlayerInfo.js index 4b0c3d6b..3f40c00b 100644 --- a/lib/users/getPlayerInfo.js +++ b/lib/users/getPlayerInfo.js @@ -1,9 +1,9 @@ // Includes +const settings = require('../../settings.json') const getFollowingCount = require('../friends/getFollowingCount.js').func const getFollowerCount = require('../friends/getFollowerCount.js').func const getFriendCount = require('../friends/getFriendCount.js').func const getUserInfo = require('../users/getUserInfo.js').func -const getUsernameHistory = require('../users/getUsernameHistory.js').func // Args exports.required = ['userId'] @@ -15,31 +15,35 @@ exports.required = ['userId'] * @alias getPlayerInfo * @param { number } userId - The id of the user. * @returns {Promise} + * @deprecated `getPlayerInfo()` is deprecated; see `getUserInfo()`, `getFollowerCount()`, `getFollowingCount()`, `getFriendCount()`, `getUsernameHistory()`, `getUserFunds()`, `getPlayerThumbnail()` instead - high rate limits on endpoints such as username history and follower count have made this aggregate function not suitable for most users * @example const noblox = require("noblox.js") * let information = await noblox.getPlayerInfo({userId: 123456}) -**/ + **/ // Define function getPlayerInfo (userId) { return new Promise((resolve, reject) => { const requests = [ - getUserInfo(userId), - getFriendCount(userId), - getFollowingCount(userId), - getFollowerCount(userId), - getUsernameHistory({ userId }) - ].map(promise => promise.then( - val => ({ status: 'fulfilled', value: val }), - rej => ({ status: 'rejected', reason: rej }) - )) + getUserInfo({ userId }), + getFriendCount({ userId }), + + getFollowingCount({ userId }), + getFollowerCount({ userId }) + ].map((promise) => + promise.then( + (val) => ({ status: 'fulfilled', value: val }), + (rej) => ({ status: 'rejected', reason: rej }) + ) + ) Promise.all(requests).then((promiseResponses) => { - const responses = promiseResponses.map(response => response.value) - const usersResponse = responses[0] - const userBody = usersResponse ? usersResponse.body : {} - const failedResponse = promiseResponses.find(presponse => presponse.status === 'rejected') || responses.find(response => !response || (!(response instanceof Array) && (response.statusCode !== 200 || !response.body))) + const responses = promiseResponses.map((response) => response.value) + const userBody = responses[0] + const failedResponses = promiseResponses.filter( + (presponse) => presponse.status === 'rejected' + ) - if (userBody.isBanned) { + if (userBody?.isBanned) { const joinDate = new Date(userBody.created) const blurb = userBody.description const isBanned = userBody.isBanned @@ -53,11 +57,40 @@ function getPlayerInfo (userId) { isBanned, displayName }) - } else if (failedResponse) { - reject(new Error('User does not exist.')) + } else if (failedResponses.length) { + const failureReason = failedResponses + .map((response) => response.reason) + .join('\n') + + if ( + (failureReason.toLowerCase().includes('too many requests') || + failureReason.toLowerCase().includes('(429')) && + settings.show_deprecation_warnings + ) { + console.warn() + console.warn( + '==============================================================================================================================================================================================' + ) + console.warn( + 'DEPRECATION WARNING: getPlayerInfo is an aggregate of multiple endpoints, rate limit changes have made this method unsuitable for many people, please use the individualized endpoints instead' + ) + console.warn( + ' see getUserInfo(), getFollowerCount(), getFollowingCount(), getFriendCount(), getUsernameHistory(), getUserFunds(), getPlayerThumbnail()' + ) + console.warn() + console.warn( + '> Opt out of these warnings using noblox.setOptions({ show_deprecation_warnings: false })' + ) + console.warn( + '==============================================================================================================================================================================================' + ) + console.warn() + } + + const error = failedResponses.map((r) => r.reason).join('\n') + reject(new Error(error)) } else { - const responseBodies = responses.map(res => res.body ?? res) - const oldNames = responses[4].map(nameObject => nameObject.name) || [] + const responseBodies = responses.map((res) => res.body ?? res) const friendCount = responseBodies[1] const followerCount = responseBodies[3] const followingCount = responseBodies[2] @@ -68,7 +101,11 @@ function getPlayerInfo (userId) { const displayName = userBody.displayName const currentTime = new Date() - const age = Math.round(Math.abs((joinDate.getTime() - currentTime.getTime()) / (24 * 60 * 60 * 1000))) + const age = Math.round( + Math.abs( + (joinDate.getTime() - currentTime.getTime()) / (24 * 60 * 60 * 1000) + ) + ) resolve({ username, @@ -79,7 +116,6 @@ function getPlayerInfo (userId) { friendCount, followerCount, followingCount, - oldNames, isBanned }) } diff --git a/lib/users/getUserInfo.js b/lib/users/getUserInfo.js index edb94ab6..df1b35cb 100644 --- a/lib/users/getUserInfo.js +++ b/lib/users/getUserInfo.js @@ -14,9 +14,9 @@ exports.required = ['userId'] **/ // Define -exports.func = function (userId) { +exports.func = function (args) { const httpOpt = { - url: `//users.roblox.com/v1/users/${userId}`, + url: `//users.roblox.com/v1/users/${args.userId}`, options: { json: true, method: 'GET', @@ -25,7 +25,7 @@ exports.func = function (userId) { } return http(httpOpt).then(function (res) { - if (res.statusCode !== 200) { throw new Error(`Failed to fetch user information: ${res.body.errors?.join(', ')}`) } + if (res.statusCode !== 200) { throw new Error(`Failed to fetch user information: ${res.body?.errors?.at(0)?.message}`) } res.body.created = new Date(res.body.created) diff --git a/test/users.test.js b/test/users.test.js index e362f066..dc38466e 100644 --- a/test/users.test.js +++ b/test/users.test.js @@ -53,7 +53,6 @@ describe('Users Methods', () => { friendCount: expect.any(Number), followerCount: expect.any(Number), followingCount: expect.any(Number), - oldNames: expect.any(Array), isBanned: expect.any(Boolean), displayName: expect.any(String) }) diff --git a/typings/index.d.ts b/typings/index.d.ts index 83d4c203..7c33ad95 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1145,7 +1145,6 @@ declare module "noblox.js" { friendCount?: number; followerCount?: number; followingCount?: number; - oldNames?: string[]; isBanned: boolean; } diff --git a/typings/jsDocs.ts b/typings/jsDocs.ts index eeab7e74..68f66011 100644 --- a/typings/jsDocs.ts +++ b/typings/jsDocs.ts @@ -1579,7 +1579,6 @@ type PlayerInfo = { friendCount?: number; followerCount?: number; followingCount?: number; - oldNames?: Array; isBanned: boolean; }