From 314ba69a51010f9d210f0f4d2efddaecfe6159e5 Mon Sep 17 00:00:00 2001 From: Emsar69 Date: Wed, 20 Mar 2024 22:06:23 +0300 Subject: [PATCH] Fixed addDeveloperProduct & checkDeveloperProductName. Added 'addGamepass' function. --- lib/games/addDeveloperProduct.js | 12 ++--- lib/games/addGamepass.js | 64 ++++++++++++++++++++++++++ lib/games/checkDeveloperProductName.js | 18 ++++++-- typings/index.d.ts | 21 +++++---- 4 files changed, 96 insertions(+), 19 deletions(-) create mode 100644 lib/games/addGamepass.js diff --git a/lib/games/addDeveloperProduct.js b/lib/games/addDeveloperProduct.js index b15db2c50..15ee72844 100644 --- a/lib/games/addDeveloperProduct.js +++ b/lib/games/addDeveloperProduct.js @@ -24,13 +24,13 @@ const nextFunction = (jar, token, universeId, name, priceInRobux, description) = return checkProductName({ universeId, productName: name - }).then((res) => { - if (res.Success && res.Message === 'Name available') { + }).then((exists) => { + if (!exists) { return http({ - url: '//apis.roblox.com/developer-products/v1/universes/' + universeId + '/developerproducts?name=' + name + '&description=' + description + '&priceInRobux=' + priceInRobux, + url: `//apis.roblox.com/developer-products/v1/universes/${universeId}/developerproducts?name=${name}&description=${description}&priceInRobux=${priceInRobux}`, options: { method: 'POST', - jar, + jar: jar, headers: { 'X-CSRF-TOKEN': token }, @@ -47,7 +47,7 @@ const nextFunction = (jar, token, universeId, name, priceInRobux, description) = productId: typeof res.body === 'object' ? res.body.id : JSON.parse(res.body).id } } else { - throw new Error('Create product failed, ' + res.statusCode + ' ' + res.statusMessage + '') + throw new Error(`Create product failed, ${res.statusCode} ${res.statusMessage}`) } }) } else { @@ -59,7 +59,7 @@ const nextFunction = (jar, token, universeId, name, priceInRobux, description) = exports.func = (args) => { const jar = args.jar - return getGeneralToken({ jar }).then((xcsrf) => { + return getGeneralToken({ jar: jar }).then((xcsrf) => { return nextFunction(jar, xcsrf, args.universeId, args.name, args.priceInRobux, args.description) }) } diff --git a/lib/games/addGamepass.js b/lib/games/addGamepass.js new file mode 100644 index 000000000..8cfdccb17 --- /dev/null +++ b/lib/games/addGamepass.js @@ -0,0 +1,64 @@ +const http = require('../util/http.js').func +const getGeneralToken = require('../util/getGeneralToken.js').func + +exports.required = ['universeId', 'name', 'priceInRobux'] +exports.optional = ['description', 'jar'] + +// Docs +/** + * 🔐 Create a gamepass. + * @category Game + * @alias addGamepass + * @param {number} universeId - The id of the universe. + * @param {string} name - The name of the gamepass. + * @param {number} priceInRobux - The price of the product. + * @param {string=} description - The description of the gamepass. + * @returns {Promise} + * @example const noblox = require("noblox.js") + * // Login using your cookie + * noblox.addGamepass(1, "A Gamepass", 100, "A cool item.") +**/ + +const nextFunction = (jar, token, universeId, name, priceInRobux, description) => { + const FormData = require('form-data'); + const goofy = new FormData() + goofy.append("Name", name) + goofy.append("Description", description) + goofy.append("UniverseId", universeId) + goofy.append("IsForSale", 'true') + goofy.append("Price", priceInRobux) + return http({ + url: `//apis.roblox.com/game-passes/v1/game-passes`, + options: { + method: 'POST', + jar: jar, + body: goofy, + headers: { + ...goofy.getHeaders(), + 'X-CSRF-TOKEN': token + }, + resolveWithFullResponse: true + } + }).then((res) => { + console.log(res) + if (res.statusCode === 200) { + return { + universeId, + name, + priceInRobux, + description, + gamepassId: typeof res.body === 'object' ? res.body.id : JSON.parse(res.body).id + } + } else { + throw new Error(`Create gamepass failed, ${res.statusCode} ${res.statusMessage}`) + } + }).catch(e => {}) +} + +exports.func = (args) => { + const jar = args.jar + + return getGeneralToken({ jar: jar }).then((xcsrf) => { + return nextFunction(jar, xcsrf, args.universeId, args.name, args.priceInRobux, args.description) + }) +} diff --git a/lib/games/checkDeveloperProductName.js b/lib/games/checkDeveloperProductName.js index 0f270c89b..db3358bf6 100644 --- a/lib/games/checkDeveloperProductName.js +++ b/lib/games/checkDeveloperProductName.js @@ -11,10 +11,10 @@ exports.optional = ['jar', 'productId'] * @param {number} universeId - The id of the universe. * @param {string} productName - The name of the developer product. * @param {number=} productId - The id of the developer product. - * @returns {Promise} + * @returns {Promise} * @example const noblox = require("noblox.js") * // Login using your cookie - * const productInfo = await noblox.checkDeveloperProductName(1, "A Developer Product") + * const exists = await noblox.checkDeveloperProductName(1, "A Developer Product") **/ exports.func = (args) => { @@ -23,17 +23,25 @@ exports.func = (args) => { const productId = parseInt(args.productId) ? parseInt(args.productId) : 0 return http({ - url: '//www.roblox.com/places/check-developerproduct-name?universeId=' + universeId + '&developerProductId=' + productId + '&developerProductName=' + args.productName + '&_=1515792139751', + url: `//apis.roblox.com/developer-products/v1/universes/${universeId}/developerproducts?pageNumber=1&pageSize=2147483647`, options: { method: 'GET', - jar, + jar: jar, resolveWithFullResponse: true } }).then((res) => { if (res.statusCode !== 200) { throw new Error('You are not logged in') } else { - return JSON.parse(res.body) + const data = JSON.parse(res.body) + let exists = false + data.forEach(product => { + if(product.name == args.productName || product.id == productId){ + exists = true + return + } + }) + return exists } }) } diff --git a/typings/index.d.ts b/typings/index.d.ts index cb8fc1392..358968ec9 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -631,13 +631,12 @@ declare module "noblox.js" { productId: string } - interface CheckDeveloperProductNameResult { - Success: boolean; - /** - * When success is true: "Name available" - * When success is false, you can get: "Product name already exists" - */ - Message: string; + interface GamepassAddResult { + universeId: number, + name: string, + priceInRobux: number, + description?: string, + productId: string } interface GamePassData { @@ -1826,12 +1825,18 @@ declare module "noblox.js" { */ function addDeveloperProduct(universeId: number, name: string, priceInRobux: number, description?: string, jar?: CookieJar): Promise; + /** + * 🔐 Adds a gamepass to the specified universe. + * Warning: The `gamepassId` returned by this function does not match the `gamepassId` used by other endpoints. + */ + function addGamepass(universeId: number, name: string, priceInRobux: number, description?: string, jar?: CookieJar): Promise; + /** * 🔐 Checks to see if the provided `produceName` is able to be used on `productId`. * * NOTE: You actually need a valid `productId` and `universeId` otherwise, the http request returns a `404 Not Found` response. */ - function checkDeveloperProductName(universeId: number, productName: string, jar?: CookieJar, productId?: number): Promise; + function checkDeveloperProductName(universeId: number, productName: string, jar?: CookieJar, productId?: number): Promise; /** * 🔐 Configures a game pass with the id `gamePassId` to have a `name`, `description`, `price` in Robux, and `icon` image. If `name` is an empty string, only `price` is changed. Setting `price` to false, 0, or a negative value will place the game pass off-sale.