From 214790809d89956905f8349a8b402d78a6e84f62 Mon Sep 17 00:00:00 2001 From: Carter Penterman Date: Mon, 15 Apr 2024 21:15:35 -0500 Subject: [PATCH] Use non-rate-limited API and improve some stuff --- commands/submit.js | 43 +++++++++++++++------------------- commands/take.js | 57 +++++++++++++++++++++++----------------------- common.js | 55 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 53 deletions(-) create mode 100644 common.js diff --git a/commands/submit.js b/commands/submit.js index f56e557..2ea388f 100644 --- a/commands/submit.js +++ b/commands/submit.js @@ -4,6 +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 } = require("../common.js"); async function robloxUserFromDiscord(id) { if (isNaN(id)) return undefined; @@ -28,7 +29,7 @@ async function robloxUsernameFromId(id) { async function execute(interaction) { const userId = await robloxUserFromDiscord(interaction.user.id); if (!userId) { - const msg = "You don't have a Roblox account linked with your Discord account. Use !link with rbhop dog to link your account."; + const msg = "You don't have a Roblox account linked with your Discord account. Use !link with the rbhop dog bot to link your account."; await interaction.reply({content: msg, ephemeral: true}); return; } @@ -48,33 +49,25 @@ async function execute(interaction) { await noblox.setCookie(cookies[game]); // Check that the bot owns this model - if (!(await noblox.getOwnership((await noblox.getCurrentUser()).UserID, id, "Asset"))) { + 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.`; await interaction.reply({content: msg, ephemeral: true}); return; } - - try { - const info = await noblox.getProductInfo(id); - if (info.AssetTypeId != 10) { - await interaction.reply({content: `(id: ${id}) is not a valid model ID.`, ephemeral: true}); - return; - } - if (info.Creator.Id != userId) { - const assetUsername = await robloxUsernameFromId(info.Creator.Id); - const interactionUsername = await robloxUsernameFromId(userId); - const msg = `The account linked to your Discord (${interactionUsername}) is not the owner of this model (${assetUsername}), so you cannot submit it.`; - await interaction.reply({content: msg, ephemeral: true}); - return; - } - } catch (error) { - // Roblox only lets you call the product info API like once per 30 seconds for some reason... - if (error.message.startsWith("429")) { - await interaction.reply({content: `The maptest bot is being rate-limited by Roblox, please wait a minute before doing this command again.`, ephemeral: true}); - return; - } - console.log(error); - await interaction.reply({content: `There is a problem with this asset ID (id: ${id}).`, ephemeral: true}); + + const assetInfo = await getAssetInfo(id); + if (assetInfo.creatorId != userId) { + const assetUsernamePromise = robloxUsernameFromId(assetInfo.creatorId); + const interactionUsernamePromise = robloxUsernameFromId(userId); + const assetUsername = await assetUsernamePromise; + const interactionUsername = await interactionUsernamePromise; + const msg = `The account linked to your Discord (${interactionUsername}) is not the owner of this model (${assetUsername}), so you cannot submit it.`; + await interaction.reply({content: msg, ephemeral: true}); + return; + } + // Shouldn't really be possible but who knows... + if (assetInfo.typeId != AssetType.Model) { + await interaction.reply({content: `This asset (id: ${id}) is not a model. Your map must be a model.`, ephemeral: true}); return; } @@ -86,7 +79,7 @@ async function execute(interaction) { const rid = record[0]; const rtimestamp = record[1]; if (id == rid) { - await interaction.reply({content: `Tried to submit map (id: ${id}) that already exists!`, ephemeral: true}); + await interaction.reply({content: `This map (id: ${id}) was already submitted on .`, ephemeral: true}); return; } s += `${rid},${rtimestamp}\n`; diff --git a/commands/take.js b/commands/take.js index 17e715f..cb89604 100644 --- a/commands/take.js +++ b/commands/take.js @@ -1,6 +1,7 @@ const { SlashCommandBuilder } = require('@discordjs/builders'); const noblox = require("noblox.js"); const { cookies, commands } = require("../config/config.js"); +const { AssetType, getAssetInfo } = require("../common.js"); async function execute(interaction) { const game = interaction.options.getString("game"); @@ -18,28 +19,37 @@ async function execute(interaction) { await interaction.reply({content: msg, ephemeral: true}); return; } - let info; // Validate that this is a model - try { - info = await noblox.getProductInfo(id); - if (info.AssetTypeId != 10) { - await interaction.reply({content: `(id: ${id}) is not a valid model ID.`, ephemeral: true}); - return; - } - } catch (error) { - // Roblox only lets you call the product info API like once per 30 seconds for some reason... - if (error.message.startsWith("429")) { - await interaction.reply({content: `The maptest bot is being rate-limited by Roblox, please wait a minute before doing this command again.`, ephemeral: true}); - return; - } - console.log(error); - await interaction.reply({content: `There is a problem with this asset ID (id: ${id}).`, ephemeral: true}); + const assetInfo = await getAssetInfo(id); + if (assetInfo.status === 404) { + await interaction.reply({content: `This asset may not exist or is not a model (id: ${id}). Your map must be a model.`, ephemeral: true}); + return; + } + // 403 (Forbidden) means the asset isn't distributed + if (assetInfo.status === 403 || !assetInfo.forSale) { + await interaction.reply({content: `This model (id: ${id}) is off sale. Please configure it to be on sale (Configure -> Distribute on Creator Store).`, ephemeral: true}); + return; + } + if (assetInfo.typeId !== AssetType.Model) { + await interaction.reply({content: `This asset (id: ${id}) is not a model. Your map must be a model.`, ephemeral: true}); + return; + } + if (assetInfo.price !== 0) { + await interaction.reply({content: `This model (id: ${id}) is not free. Please change the price to be free.`, ephemeral: true}); return; } - try { - await noblox.buy({product: info, price: 0}); - await interaction.reply( + // Make a "fake" product info object for the Noblox buy method + const productInfo = { + PriceInRobux: assetInfo.price, + ProductId: assetInfo.productId, + Creator: { + Id: assetInfo.creatorId + } + } + + await noblox.buy({product: productInfo, price: 0}); + await interaction.reply( ` Now that your map (id: ${id}) has been taken by the ${game} maptest bot you can load it into the ${game} maptest place. To load your map, join the game and say \`\`\` @@ -47,16 +57,7 @@ Now that your map (id: ${id}) has been taken by the ${game} maptest bot you can \`\`\`Read what it says. If your map successfully loaded type !rtv and then choose your map. If it did not load successfully, you can expand the chat to view the full error message by clicking and dragging on the edge of the chat. ` - ); - } catch (error) { - if (error.message == "You already own this item.") { - await interaction.reply({content: "The bot has already taken this model!", ephemeral: true}); - } else { - await interaction.reply({content: `An error occured trying to take the model (id: ${id}). Make sure it is uncopylocked!`, ephemeral: true}); - console.log(`Could not take asset ID ${id}: `); - console.log(error); - } - } + ); } module.exports = { diff --git a/common.js b/common.js new file mode 100644 index 0000000..0134e34 --- /dev/null +++ b/common.js @@ -0,0 +1,55 @@ +const axios = require("axios").default; + +// https://create.roblox.com/docs/reference/engine/enums/AssetType +const AssetType = { + Image: 1, + TShirt: 2, + Audio: 3, + Mesh: 4, + Lua: 5, + Hat: 8, + Place: 9, + Model: 10, + Shirt: 11, + Pants: 12, + Decal: 13, + Head: 17, + Face: 18, + Gear: 19, + Badge: 21, + Animation: 24, + GamePass: 34, + Plugin: 38, + MeshPart: 40 +}; + +async function getAssetInfo(assetId) { + const res = await axios.get("https://apis.roblox.com/toolbox-service/v1/items/details", { + params: { + assetIds: assetId + }, + validateStatus: (status) => status === 403 || status === 404 || (status >= 200 && status < 300) // Allow 403/404 as a valid status (don't throw an error) + }); + + if (res.status < 200 || res.status > 300) { + return { + status: res.status + } + } + + const data = res.data.data; + const assetInfo = data[0]; + + 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 + } +} + +module.exports = { AssetType, getAssetInfo }; \ No newline at end of file