Merge pull request 'Use new product/asset API' (#22) from update-product-api into master
All checks were successful
continuous-integration/drone/push Build is passing

Reviewed-on: #22
Reviewed-by: itzaname <itzaname@noreply@itzana.me>
This commit is contained in:
itzaname 2024-08-22 04:00:15 +00:00
commit 4e73ce1330
3 changed files with 78 additions and 48 deletions

View File

@ -4,7 +4,7 @@ const fs = require('node:fs');
const noblox = require("noblox.js");
const axios = require("axios").default;
const { submissions, commands, cookies } = require("../config/config.js");
const { AssetType, getAssetInfo, SubmissionColumnsString, createSubmissionLine, getSubmissionLine, validateMapAsset, getValidationMessage } = require("../common.js");
const { getAssetInfo, SubmissionColumnsString, createSubmissionLine, getSubmissionLine, validateMapAsset, getValidationMessage } = require("../common.js");
async function robloxUserFromDiscord(id) {
if (isNaN(id)) return undefined;
@ -50,12 +50,10 @@ async function execute(interaction) {
const id = interaction.options.getInteger("asset_id", true);
await noblox.setCookie(cookies[game]);
try {
// Check that the bot owns this model
if (!(await noblox.getOwnership(await noblox.getCurrentUser("UserID"), id, "Asset"))) {
const msg = `🚫 The ${game} maptest bot's inventory does not contain this asset (id: ${id}). You must use the /take command first.`;
const msg = `🚫 The ${game} maptest bot's inventory does not contain this asset (\`${id}\`). You must use the /take command first.`;
await interaction.editReply(msg);
return;
}
@ -63,13 +61,13 @@ async function execute(interaction) {
if (error.message !== "400 The specified Asset does not exist!") {
throw error;
}
await interaction.editReply(`🚫 This asset does not exist (id: ${id}).`);
await interaction.editReply(`🚫 This asset does not exist (\`${id}\`).`);
return;
}
const assetInfo = await getAssetInfo(id);
if (assetInfo.typeId !== AssetType.Model) {
await interaction.editReply(`🚫 This asset (id: ${id}) is not a model. Your map must be a model.`);
if (!assetInfo.isModel) {
await interaction.editReply(`🚫 This asset (\`${id}\`) is not a model. Your map must be a model.`);
return;
}
@ -82,7 +80,6 @@ async function execute(interaction) {
await interaction.editReply(msg);
return;
}
const csvFile = fs.readFileSync(fname);
const lines = parse(csvFile, {delimiter: ',', fromLine: 2});

View File

@ -1,7 +1,7 @@
const { SlashCommandBuilder } = require('@discordjs/builders');
const noblox = require("noblox.js");
const { cookies, commands, gamePlaces } = require("../config/config.js");
const { AssetType, getAssetInfo, validateMapAsset, getValidationMessage } = require("../common.js");
const { getAssetInfo, buyModel, validateMapAsset, getValidationMessage } = require("../common.js");
/**
* @param {import('discord.js').ChatInputCommandInteraction} interaction
@ -25,43 +25,34 @@ async function execute(interaction) {
if (error.message !== "400 The specified Asset does not exist!") {
throw error;
}
await interaction.editReply(`🚫 This asset does not exist (id: ${id}).`);
await interaction.editReply(`🚫 This asset does not exist (id: \`${id}\`).`);
return;
}
// Validate that this is a model
const assetInfo = await getAssetInfo(id);
if (assetInfo.status !== 403 && (assetInfo.status < 200 || assetInfo.status > 300)) {
await interaction.editReply(`🚫 This asset may not exist or is not a model (id: ${id}). Your map must be a model.`);
if (assetInfo.status !== 403 && (assetInfo.status < 200 || assetInfo.status >= 300)) {
await interaction.editReply(`🚫 This asset may not exist or is not a model (id: \`${id}\`). Your map must be a model.`);
return;
}
// 403 (Forbidden) means the asset isn't distributed
if (assetInfo.status === 403 || !assetInfo.forSale) {
await interaction.editReply(`🚫 This model (id: ${id}) is off sale. Please configure it to be on sale (Configure -> Distribute on Creator Store). It is also possible that this is not a valid model.`);
if (assetInfo.status === 403 || (assetInfo.forSale === false)) {
await interaction.editReply(`🚫 This [model](<https://create.roblox.com/store/asset/${id}/>) (id: \`${id}\`) is off sale. Please configure it to be on sale (Configure -> Distribute on Creator Store).`);
return;
}
if (assetInfo.typeId !== AssetType.Model) {
await interaction.editReply(`🚫 This asset (id: ${id}) is not a model. Your map must be a model.`);
if (!assetInfo.isModel) {
await interaction.editReply(`🚫 This asset (id: \`${id}\` is not a model. Your map must be a model.`);
return;
}
if (assetInfo.price !== 0) {
await interaction.editReply(`🚫 This model (id: ${id}) is not free. Please change the price to be free.`);
if (!assetInfo.isFree) {
await interaction.editReply(`🚫 This [model](<https://create.roblox.com/store/asset/${id}/>) (id: \`${id}\`) is not free. Please change the price to be free.`);
return;
}
// Make a "fake" product info object for the Noblox buy method
const productInfo = {
PriceInRobux: assetInfo.price,
ProductId: assetInfo.productId,
Creator: {
Id: assetInfo.creatorId
}
};
// Kick off the buy request
let buyPromise;
if (!alreadyOwned) {
buyPromise = noblox.buy({product: productInfo, price: 0});
buyPromise = buyModel(id);
}
// Validate and send the validation result
@ -76,7 +67,12 @@ async function execute(interaction) {
}
// Make sure the buy request is done
await buyPromise;
const success = await buyPromise;
if (!success) {
await interaction.followUp(`🚫 Something went wrong when trying to buy the [model](<https://create.roblox.com/store/asset/${id}/>) (id: \`${id}\`).`);
return;
}
await interaction.followUp(
`
Now that your [map (id: ${id})](<https://create.roblox.com/store/asset/${id}/>) has been taken by the bot you can load it into the [${game} maptest place](<${gamePlaces[game]}>).

View File

@ -1,5 +1,6 @@
const axios = require("axios").default;
const { RobloxFile } = require("rbxm-parser");
const noblox = require("noblox.js");
// https://create.roblox.com/docs/reference/engine/enums/AssetType
const AssetType = {
@ -25,34 +26,70 @@ const AssetType = {
};
async function getAssetInfo(assetId) {
const res = await axios.get("https://apis.roblox.com/toolbox-service/v1/items/details", {
params: {
assetIds: assetId
},
validateStatus: (_status) => true
const jar = noblox.options.jar;
const xcsrf = await noblox.getGeneralToken(jar);
const res = await noblox.http(`https://apis.roblox.com/user/cloud/v2/creator-store-products/PRODUCT_NAMESPACE_CREATOR_MARKETPLACE_ASSET-PRODUCT_TYPE_MODEL-${assetId}`, {
method: "GET",
resolveWithFullResponse: true,
jar: jar,
headers: {
"X-CSRF-TOKEN": xcsrf,
"Content-Type": "application/json"
}
});
if (res.status < 200 || res.status > 300) {
if (res.statusCode < 200 || res.statusCode >= 300) {
return {
status: res.status
status: res.status,
isModel: false
};
}
const data = res.data.data;
const assetInfo = data[0];
const assetInfo = JSON.parse(res.body);
const quantity = assetInfo.purchasePrice.quantity;
const isFree = quantity.significand === 0 && quantity.exponent === 0;
const forSale = assetInfo.published && assetInfo.purchasable;
return {
status: res.status,
id: assetId,
name: assetInfo.asset.name,
typeId: assetInfo.asset.typeId,
creatorId: assetInfo.creator.id,
price: assetInfo.product.price,
productId: assetInfo.product.productId,
forSale: assetInfo.product.isForSaleOrIsPublicDomain
status: res.statusCode,
isModel: true,
id: assetInfo.modelAssetId,
creatorId: +assetInfo.userSeller,
isFree: isFree,
forSale: forSale
};
}
async function buyModel(modelId) {
const reqJson = {
expectedPrice: {currencyCode: "USD", quantity: {significand: 0, exponent: 0}},
productKey: {
productNamespace: "PRODUCT_NAMESPACE_CREATOR_MARKETPLACE_ASSET",
productTargetId: `${modelId}`,
productType: "PRODUCT_TYPE_MODEL"
}
};
const jar = noblox.options.jar;
const xcsrf = await noblox.getGeneralToken(jar);
const res = await noblox.http("https://apis.roblox.com/marketplace-fiat-service/v1/product/purchase", {
method: "POST",
resolveWithFullResponse: true,
jar: jar,
headers: {
"X-CSRF-TOKEN": xcsrf,
"Content-Type": "application/json"
},
body: JSON.stringify(reqJson)
});
const resJson = JSON.parse(res.body);
// Return true if purchased, false otherwise
return res.statusCode >= 200 && res.statusCode < 300 && resJson.purchaseTransactionStatus === "PURCHASE_TRANSACTION_STATUS_SUCCESS";
}
const SubmissionColumnsString = "map_id,unix_timestamp,user_id,username,display_name,creator\n";
const SubmissionColumn = {
@ -311,4 +348,4 @@ function getValidationMessage(validation, game, errorOnFail) {
return msg;
}
module.exports = { AssetType, getAssetInfo, SubmissionColumn, SubmissionColumnsString, getSubmissionLine, createSubmissionLine, validateMapAsset, getValidationMessage, safeCsvFormat };
module.exports = { AssetType, getAssetInfo, buyModel, SubmissionColumn, SubmissionColumnsString, getSubmissionLine, createSubmissionLine, validateMapAsset, getValidationMessage, safeCsvFormat };