Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getPlayerInfo again #850

Merged
merged 12 commits into from
Feb 13, 2025
6 changes: 3 additions & 3 deletions lib/friends/getFollowerCount.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)}`
)
})
}
6 changes: 3 additions & 3 deletions lib/friends/getFollowingCount.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)}`
)
})
}
6 changes: 3 additions & 3 deletions lib/friends/getFriendCount.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)}`
)
})
}
79 changes: 57 additions & 22 deletions lib/users/getPlayerInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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']
Expand All @@ -15,31 +14,35 @@ exports.required = ['userId']
* @alias getPlayerInfo
* @param { number } userId - The id of the user.
* @returns {Promise<PlayerInfo>}
* @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
Expand All @@ -53,11 +56,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]
Expand All @@ -68,7 +100,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,
Expand All @@ -79,7 +115,6 @@ function getPlayerInfo (userId) {
friendCount,
followerCount,
followingCount,
oldNames,
isBanned
})
}
Expand Down
6 changes: 3 additions & 3 deletions lib/users/getUserInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)

Expand Down
1 change: 0 additions & 1 deletion test/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
1 change: 0 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,6 @@ declare module "noblox.js" {
friendCount?: number;
followerCount?: number;
followingCount?: number;
oldNames?: string[];
isBanned: boolean;
}

Expand Down
1 change: 0 additions & 1 deletion typings/jsDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1579,7 +1579,6 @@ type PlayerInfo = {
friendCount?: number;
followerCount?: number;
followingCount?: number;
oldNames?: Array<string>;
isBanned: boolean;
}

Expand Down
Loading