From ff9ae1b3e63d3e692e027c4202722a9e5e57b5fb Mon Sep 17 00:00:00 2001 From: commonly-ts Date: Sun, 15 Dec 2024 03:39:00 +1300 Subject: [PATCH 1/8] Create new function --- lib/games/sendUserNotification.js | 69 +++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 lib/games/sendUserNotification.js diff --git a/lib/games/sendUserNotification.js b/lib/games/sendUserNotification.js new file mode 100644 index 000000000..102be4fa4 --- /dev/null +++ b/lib/games/sendUserNotification.js @@ -0,0 +1,69 @@ +const http = require("../util/http.js").func; + +exports.required = ["universeId", "userId", "assetId", "parameters"]; +exports.optional = ["jar"]; + +// Docs +/** + * ☁️ Send a universe notification to a user. + * @category Game + * @alias sendUserNotification + * @param {number} universeId - The id of the universe. + * @param {number} userId - The id of the target player. + * @param {string} assetId - The asset id of the notification. + * @param {UserNotificationPayloadParameters} parameters - The notification parameters. + * @returns {Promise} + * @example const noblox = require("noblox.js") + * // Set API key + * const parameters = { + * myMessage: { stringValue: "Hello world!" } + * } + * const assetId = "774d62e5-3414-b84a-89dd-77d96f1a3d33" + * noblox.sendUserNotification(4434277335, 1210019099, assetId, parameters) + **/ + +function sendUserNotification(universeId, userId, assetId, parameters, jar) { + return new Promise((resolve, reject) => { + const httpOpt = { + url: `//apis.roblox.com/cloud/v2/users/${userId}/notifications`, + options: { + json: true, + resolveWithFullResponse: true, + jar, + method: "POST", + body: { + source: { + universe: `universes/${universeId}`, + }, + payload: { + type: "MOMENT", + messageId: assetId, + parameters: parameters, + }, + }, + headers: { + "Content-Type": "application/json", + }, + }, + }; + + return http(httpOpt) + .then(function (res) { + if (res.statusCode === 200) { + resolve(true); + } else { + if (typeof res.body === "string") { + reject(new Error(`[${res.statusCode}] ${res.statusMessage} ${res.body}`)); + } else { + const data = Object.assign(res.body); + reject(new Error(`[${res.statusCode}] ${data.Error} ${data.Message}`)); + } + } + }) + .catch(reject); + }); +} + +exports.func = function (args) { + return sendUserNotification(args.universeId, args.userId, args.assetId, args.parameters, args.jar); +}; From a83f415b8862d506df1db99b1d7d3169f5565fe1 Mon Sep 17 00:00:00 2001 From: commonly-ts Date: Sun, 15 Dec 2024 03:39:16 +1300 Subject: [PATCH 2/8] Updated and added typings --- lib/index.js | 1 + typings/index.d.ts | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 68c2b4bf2..ada7a079e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -98,6 +98,7 @@ noblox.getGameSocialLinks = require('./games/getGameSocialLinks.js') noblox.getGroupGames = require('./games/getGroupGames.js') noblox.getPlaceInfo = require('./games/getPlaceInfo.js') noblox.getUniverseInfo = require('./games/getUniverseInfo.js') +noblox.sendUserNotification = require('./games/sendUserNotification.js') noblox.updateDeveloperProduct = require('./games/updateDeveloperProduct.js') noblox.changeRank = require('./groups/changeRank.js') noblox.deleteWallPost = require('./groups/deleteWallPost.js') diff --git a/typings/index.d.ts b/typings/index.d.ts index 83d4c203b..b6eac2fd2 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -11,6 +11,7 @@ declare module "noblox.js" { */ interface CookieJar { session?: string; + apiKey?: string; } /** @@ -583,6 +584,12 @@ declare module "noblox.js" { } /// Game + type UserNotificationPayloadParameter = { stringValue: string } | { int64Value: number }; + + interface UserNotificationPayloadParameters { + [key: string]: UserNotificationPayloadParameter + } + interface GameInstance { id: string; maxPlayers: number; @@ -1918,6 +1925,11 @@ declare module "noblox.js" { */ function publishToTopic(universeId: number, topic: string, data: (Object | string), jar?: CookieJar): Promise; + /** +* ☁️ Send a universe notification to a user. +*/ + function sendUserNotification(universeId: number, userId: number, assetId: string, parameters: UserNotificationPayloadParameters, jar?: CookieJar): Promise; + /** * 🔐 Returns information about the place(s) in question, such as name, description, etc. */ @@ -2243,7 +2255,7 @@ declare module "noblox.js" { * 🔐 Get the current authenticated user. */ function getAuthenticatedUser(jar?: CookieJar): Promise - + /** * 🔐 Gets the current user logged into `jar` and returns an `option` if specified or all options if not. */ From ddf1a83e361e067ec754d0a1966a1d92f21730e8 Mon Sep 17 00:00:00 2001 From: commonly-ts Date: Sun, 15 Dec 2024 03:43:02 +1300 Subject: [PATCH 3/8] Fixed linting --- lib/games/sendUserNotification.js | 36 +++++++++++++++---------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/games/sendUserNotification.js b/lib/games/sendUserNotification.js index 102be4fa4..81f6bb7dc 100644 --- a/lib/games/sendUserNotification.js +++ b/lib/games/sendUserNotification.js @@ -1,7 +1,7 @@ -const http = require("../util/http.js").func; +const http = require('../util/http.js').func; -exports.required = ["universeId", "userId", "assetId", "parameters"]; -exports.optional = ["jar"]; +exports.required = ['universeId', 'userId', 'assetId', 'parameters']; +exports.optional = ['jar']; // Docs /** @@ -23,47 +23,47 @@ exports.optional = ["jar"]; **/ function sendUserNotification(universeId, userId, assetId, parameters, jar) { - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { const httpOpt = { url: `//apis.roblox.com/cloud/v2/users/${userId}/notifications`, options: { json: true, resolveWithFullResponse: true, jar, - method: "POST", + method: 'POST', body: { source: { universe: `universes/${universeId}`, }, payload: { - type: "MOMENT", + type: 'MOMENT', messageId: assetId, - parameters: parameters, + parameters, }, }, headers: { - "Content-Type": "application/json", + 'Content-Type': 'application/json', }, }, - }; + } return http(httpOpt) .then(function (res) { if (res.statusCode === 200) { - resolve(true); + resolve(true) } else { - if (typeof res.body === "string") { - reject(new Error(`[${res.statusCode}] ${res.statusMessage} ${res.body}`)); + if (typeof res.body === 'string') { + reject(new Error(`[${res.statusCode}] ${res.statusMessage} ${res.body}`)) } else { - const data = Object.assign(res.body); - reject(new Error(`[${res.statusCode}] ${data.Error} ${data.Message}`)); + const data = Object.assign(res.body) + reject(new Error(`[${res.statusCode}] ${data.Error} ${data.Message}`)) } } }) - .catch(reject); - }); + .catch(reject) + }) } exports.func = function (args) { - return sendUserNotification(args.universeId, args.userId, args.assetId, args.parameters, args.jar); -}; + return sendUserNotification(args.universeId, args.userId, args.assetId, args.parameters, args.jar) +} From 8ad2c03a42bf3da476ecdeac16dc2b3524af712c Mon Sep 17 00:00:00 2001 From: commonly-ts Date: Sun, 15 Dec 2024 03:49:03 +1300 Subject: [PATCH 4/8] Fixed linting electric boogaloo linting better be happy now... --- lib/games/sendUserNotification.js | 82 +++++++++++++++---------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/lib/games/sendUserNotification.js b/lib/games/sendUserNotification.js index 81f6bb7dc..a2505044b 100644 --- a/lib/games/sendUserNotification.js +++ b/lib/games/sendUserNotification.js @@ -1,7 +1,7 @@ -const http = require('../util/http.js').func; +const http = require('../util/http.js').func -exports.required = ['universeId', 'userId', 'assetId', 'parameters']; -exports.optional = ['jar']; +exports.required = ['universeId', 'userId', 'assetId', 'parameters'] +exports.optional = ['jar'] // Docs /** @@ -24,46 +24,46 @@ exports.optional = ['jar']; function sendUserNotification(universeId, userId, assetId, parameters, jar) { return new Promise((resolve, reject) => { - const httpOpt = { - url: `//apis.roblox.com/cloud/v2/users/${userId}/notifications`, - options: { - json: true, - resolveWithFullResponse: true, - jar, - method: 'POST', - body: { - source: { - universe: `universes/${universeId}`, - }, - payload: { - type: 'MOMENT', - messageId: assetId, - parameters, - }, - }, - headers: { - 'Content-Type': 'application/json', - }, - }, - } + const httpOpt = { + url: `//apis.roblox.com/cloud/v2/users/${userId}/notifications`, + options: { + json: true, + resolveWithFullResponse: true, + jar, + method: 'POST', + body: { + source: { + universe: `universes/${universeId}`, + }, + payload: { + type: 'MOMENT', + messageId: assetId, + parameters, + }, + }, + headers: { + 'Content-Type': 'application/json', + }, + }, + } - return http(httpOpt) - .then(function (res) { - if (res.statusCode === 200) { - resolve(true) - } else { - if (typeof res.body === 'string') { - reject(new Error(`[${res.statusCode}] ${res.statusMessage} ${res.body}`)) - } else { - const data = Object.assign(res.body) - reject(new Error(`[${res.statusCode}] ${data.Error} ${data.Message}`)) - } - } - }) - .catch(reject) - }) + return http(httpOpt) + .then(function (res) { + if (res.statusCode === 200) { + resolve(true) + } else { + if (typeof res.body === 'string') { + reject(new Error(`[${res.statusCode}] ${res.statusMessage} ${res.body}`)) + } else { + const data = Object.assign(res.body) + reject(new Error(`[${res.statusCode}] ${data.Error} ${data.Message}`)) + } + } + }) + .catch(reject) + }) } exports.func = function (args) { - return sendUserNotification(args.universeId, args.userId, args.assetId, args.parameters, args.jar) + return sendUserNotification(args.universeId, args.userId, args.assetId, args.parameters, args.jar) } From 6f388f123426aca569addd869506d899954f3fd0 Mon Sep 17 00:00:00 2001 From: commonly-ts Date: Sun, 15 Dec 2024 03:51:24 +1300 Subject: [PATCH 5/8] Linting final touches of grace --- lib/games/sendUserNotification.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/games/sendUserNotification.js b/lib/games/sendUserNotification.js index a2505044b..8466047d0 100644 --- a/lib/games/sendUserNotification.js +++ b/lib/games/sendUserNotification.js @@ -22,7 +22,7 @@ exports.optional = ['jar'] * noblox.sendUserNotification(4434277335, 1210019099, assetId, parameters) **/ -function sendUserNotification(universeId, userId, assetId, parameters, jar) { +function sendUserNotification (universeId, userId, assetId, parameters, jar) { return new Promise((resolve, reject) => { const httpOpt = { url: `//apis.roblox.com/cloud/v2/users/${userId}/notifications`, @@ -33,22 +33,22 @@ function sendUserNotification(universeId, userId, assetId, parameters, jar) { method: 'POST', body: { source: { - universe: `universes/${universeId}`, + universe: `universes/${universeId}` }, payload: { type: 'MOMENT', messageId: assetId, - parameters, + parameters }, }, headers: { - 'Content-Type': 'application/json', - }, - }, + 'Content-Type': 'application/json' + } + } } return http(httpOpt) - .then(function (res) { + .then(function (res) { if (res.statusCode === 200) { resolve(true) } else { From b15d0e22088b4d5e7142cd732bd84708150f7bb0 Mon Sep 17 00:00:00 2001 From: commonly-ts Date: Sun, 15 Dec 2024 03:53:01 +1300 Subject: [PATCH 6/8] Linting obliteration of an unworthy comma --- lib/games/sendUserNotification.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/games/sendUserNotification.js b/lib/games/sendUserNotification.js index 8466047d0..2f9e69745 100644 --- a/lib/games/sendUserNotification.js +++ b/lib/games/sendUserNotification.js @@ -39,7 +39,7 @@ function sendUserNotification (universeId, userId, assetId, parameters, jar) { type: 'MOMENT', messageId: assetId, parameters - }, + } }, headers: { 'Content-Type': 'application/json' From 943a292eab40b0e0d64e7d70e3d216175688980d Mon Sep 17 00:00:00 2001 From: commonly-ts Date: Wed, 18 Dec 2024 13:54:34 +1300 Subject: [PATCH 7/8] Added typings to jsDocs --- typings/jsDocs.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/typings/jsDocs.ts b/typings/jsDocs.ts index eeab7e74a..2ed5f22c5 100644 --- a/typings/jsDocs.ts +++ b/typings/jsDocs.ts @@ -729,6 +729,18 @@ type OnUserTypingChatEvent = { } /// Game +/** + * @typedef + */ +type UserNotificationPayloadParameter = { stringValue: string } | { int64Value: number }; + +/** + * @typedef + */ +type UserNotificationPayloadParameters = { + [key: string]: UserNotificationPayloadParameter +} + /** * @typedef */ From 197de2508245be8af936a86dd03e336ad27e7d78 Mon Sep 17 00:00:00 2001 From: Regalijan <72576136+Regalijan@users.noreply.github.com> Date: Tue, 17 Dec 2024 20:57:24 -0500 Subject: [PATCH 8/8] Fix error handling --- lib/games/sendUserNotification.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/games/sendUserNotification.js b/lib/games/sendUserNotification.js index 2f9e69745..45dec5bd3 100644 --- a/lib/games/sendUserNotification.js +++ b/lib/games/sendUserNotification.js @@ -56,7 +56,7 @@ function sendUserNotification (universeId, userId, assetId, parameters, jar) { reject(new Error(`[${res.statusCode}] ${res.statusMessage} ${res.body}`)) } else { const data = Object.assign(res.body) - reject(new Error(`[${res.statusCode}] ${data.Error} ${data.Message}`)) + reject(new Error(`[${res.statusCode}] ${data.code} ${data.message}`)) } } })