Compare commits

...

2 Commits

3 changed files with 39 additions and 18 deletions

@ -4,7 +4,7 @@ const fs = require('node:fs');
const noblox = require("noblox.js"); const noblox = require("noblox.js");
const axios = require("axios").default; const axios = require("axios").default;
const { submissions, commands, cookies } = require("../config/config.js"); const { submissions, commands, cookies } = require("../config/config.js");
const { getAssetInfo, SubmissionColumnsString, createSubmissionLine, getSubmissionLine, validateMapAsset, getValidationMessage } = require("../common.js"); const { getAssetInfo, getCurrentUser, SubmissionColumnsString, createSubmissionLine, getSubmissionLine, validateMapAsset, getValidationMessage } = require("../common.js");
async function robloxUserFromDiscord(id) { async function robloxUserFromDiscord(id) {
if (isNaN(id)) return undefined; if (isNaN(id)) return undefined;
@ -48,12 +48,12 @@ async function execute(interaction) {
} }
const id = interaction.options.getInteger("asset_id", true); const id = interaction.options.getInteger("asset_id", true);
await noblox.setCookie(cookies[game]); noblox.setCookie(cookies[game], false);
try { try {
// Check that the bot owns this model // Check that the bot owns this model
if (!(await noblox.getOwnership(await noblox.getCurrentUser("UserID"), id, "Asset"))) { if (!(await noblox.getOwnership(await getCurrentUser(), id, "Asset"))) {
const msg = `🚫 The ${game} maptest bot's inventory does not contain this asset (\`${id}\`). You must use the /take command first.`; const msg = `🚫 The ${game} maptest bot's inventory does not contain this asset (id: \`${id}\`). You must use the /take command first.`;
await interaction.editReply(msg); await interaction.editReply(msg);
return; return;
} }
@ -61,13 +61,13 @@ async function execute(interaction) {
if (error.message !== "400 The specified Asset does not exist!") { if (error.message !== "400 The specified Asset does not exist!") {
throw error; throw error;
} }
await interaction.editReply(`🚫 This asset does not exist (\`${id}\`).`); await interaction.editReply(`🚫 This asset does not exist (id: \`${id}\`).`);
return; return;
} }
const assetInfo = await getAssetInfo(id); const assetInfo = await getAssetInfo(id);
if (!assetInfo.isModel) { if (!assetInfo.isModel) {
await interaction.editReply(`🚫 This asset (\`${id}\`) is not a model. Your map must be a model.`); await interaction.editReply(`🚫 This asset (id: \`${id}\`) is not a model. Your map must be a model.`);
return; return;
} }
@ -87,7 +87,7 @@ async function execute(interaction) {
for (let lineStr of lines) { for (let lineStr of lines) {
const line = getSubmissionLine(lineStr); const line = getSubmissionLine(lineStr);
if (id === line.modelId) { if (id === line.modelId) {
await interaction.editReply(`🚫 This map (id: ${id}) was already submitted on <t:${line.timestamp}:d>.`); await interaction.editReply(`🚫 This map (id: \`${id}\`) was already submitted on <t:${line.timestamp}:d>.`);
return; return;
} }
} }
@ -112,7 +112,7 @@ async function execute(interaction) {
csvString += createSubmissionLine(id, unixTimestamp, userId, await robloxUsernameFromId(userId), validation.displayName, validation.creator); csvString += createSubmissionLine(id, unixTimestamp, userId, await robloxUsernameFromId(userId), validation.displayName, validation.creator);
fs.writeFileSync(fname, csvString); fs.writeFileSync(fname, csvString);
await interaction.followUp(`Map (id: ${id}) successfully submitted.`); await interaction.followUp(`Map (id: \`${id}\`) successfully submitted.`);
} }
module.exports = { module.exports = {

@ -1,7 +1,7 @@
const { SlashCommandBuilder } = require('@discordjs/builders'); const { SlashCommandBuilder } = require('@discordjs/builders');
const noblox = require("noblox.js"); const noblox = require("noblox.js");
const { cookies, commands, gamePlaces } = require("../config/config.js"); const { cookies, commands, gamePlaces } = require("../config/config.js");
const { getAssetInfo, buyModel, validateMapAsset, getValidationMessage } = require("../common.js"); const { getAssetInfo, buyModel, getCurrentUser, validateMapAsset, getValidationMessage } = require("../common.js");
/** /**
* @param {import('discord.js').ChatInputCommandInteraction} interaction * @param {import('discord.js').ChatInputCommandInteraction} interaction
@ -15,12 +15,12 @@ async function execute(interaction) {
} }
const id = interaction.options.getInteger("asset_id", true); const id = interaction.options.getInteger("asset_id", true);
await noblox.setCookie(cookie); noblox.setCookie(cookie, false);
let alreadyOwned; let alreadyOwned;
try { try {
// Check if the bot already owns this asset // Check if the bot already owns this asset
alreadyOwned = await noblox.getOwnership(await noblox.getCurrentUser("UserID"), id, "Asset"); alreadyOwned = await noblox.getOwnership(await getCurrentUser(), id, "Asset");
} catch (error) { } catch (error) {
if (error.message !== "400 The specified Asset does not exist!") { if (error.message !== "400 The specified Asset does not exist!") {
throw error; throw error;
@ -31,17 +31,20 @@ async function execute(interaction) {
// Validate that this is a model // Validate that this is a model
const assetInfo = await getAssetInfo(id); const assetInfo = await getAssetInfo(id);
if (assetInfo.status !== 403 && (assetInfo.status < 200 || assetInfo.status >= 300)) { if (assetInfo.status === 404) {
await interaction.editReply(`🚫 This asset may not exist or is not a model (id: \`${id}\`). Your map must be a model.`); 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). Alternatively, the provided ID may not actually be a model.`);
return; return;
} }
// 403 (Forbidden) means the asset isn't distributed if (assetInfo.status < 200 || assetInfo.status >= 300) {
if (assetInfo.status === 403 || (assetInfo.forSale === false)) { await interaction.editReply(`🚫 Something unexpected went wrong trying to retrive info about the model (id: \`${id}\`).`);
return;
}
if (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).`); 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; return;
} }
if (!assetInfo.isModel) { if (!assetInfo.isModel) {
await interaction.editReply(`🚫 This asset (id: \`${id}\` is not a model. Your map must be a model.`); await interaction.editReply(`🚫 This asset (id: \`${id}\`) is not a model. Your map must be a model.`);
return; return;
} }
if (!assetInfo.isFree) { if (!assetInfo.isFree) {

@ -41,7 +41,7 @@ async function getAssetInfo(assetId) {
if (res.statusCode < 200 || res.statusCode >= 300) { if (res.statusCode < 200 || res.statusCode >= 300) {
return { return {
status: res.status, status: res.statusCode,
isModel: false isModel: false
}; };
} }
@ -90,6 +90,24 @@ async function buyModel(modelId) {
return res.statusCode >= 200 && res.statusCode < 300 && resJson.purchaseTransactionStatus === "PURCHASE_TRANSACTION_STATUS_SUCCESS"; return res.statusCode >= 200 && res.statusCode < 300 && resJson.purchaseTransactionStatus === "PURCHASE_TRANSACTION_STATUS_SUCCESS";
} }
async function getCurrentUser() {
const jar = noblox.options.jar;
const xcsrf = await noblox.getGeneralToken(jar);
const res = await noblox.http("https://users.roblox.com/v1/users/authenticated", {
method: "GET",
resolveWithFullResponse: true,
jar: jar,
headers: {
"X-CSRF-TOKEN": xcsrf,
"Content-Type": "application/json"
}
});
const resJson = JSON.parse(res.body);
return resJson.id;
}
const SubmissionColumnsString = "map_id,unix_timestamp,user_id,username,display_name,creator\n"; const SubmissionColumnsString = "map_id,unix_timestamp,user_id,username,display_name,creator\n";
const SubmissionColumn = { const SubmissionColumn = {
@ -348,4 +366,4 @@ function getValidationMessage(validation, game, errorOnFail) {
return msg; return msg;
} }
module.exports = { AssetType, getAssetInfo, buyModel, SubmissionColumn, SubmissionColumnsString, getSubmissionLine, createSubmissionLine, validateMapAsset, getValidationMessage, safeCsvFormat }; module.exports = { AssetType, getAssetInfo, buyModel, getCurrentUser, SubmissionColumn, SubmissionColumnsString, getSubmissionLine, createSubmissionLine, validateMapAsset, getValidationMessage, safeCsvFormat };