Bot checks inventory when taking/submitting
This commit is contained in:
@ -3,7 +3,7 @@ const { parse } = require("csv-parse/sync");
|
||||
const fs = require('node:fs');
|
||||
const noblox = require("noblox.js");
|
||||
const axios = require("axios").default;
|
||||
const { submissions, commands } = require("../config/config.js");
|
||||
const { submissions, commands, cookies } = require("../config/config.js");
|
||||
|
||||
async function robloxUserFromDiscord(id) {
|
||||
if (isNaN(id)) return undefined;
|
||||
@ -45,8 +45,17 @@ async function execute(interaction) {
|
||||
}
|
||||
|
||||
const id = interaction.options.getInteger("asset_id");
|
||||
await noblox.setCookie(cookies[game]);
|
||||
|
||||
// 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.`;
|
||||
await interaction.reply({content: msg, ephemeral: true});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const info = await getProductInfo(id);
|
||||
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;
|
||||
@ -54,11 +63,16 @@ async function execute(interaction) {
|
||||
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.`
|
||||
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
|
||||
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});
|
||||
return;
|
||||
@ -84,7 +98,6 @@ async function execute(interaction) {
|
||||
fs.writeFileSync(fname, s);
|
||||
|
||||
await interaction.reply(`Map (id: ${id}) successfully submitted.`);
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
@ -102,37 +115,4 @@ module.exports = {
|
||||
.setRequired(true))
|
||||
,
|
||||
execute
|
||||
};
|
||||
|
||||
function getProductInfo (asset) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const httpOpt = {
|
||||
url: `//economy.roblox.com/v2/assets/${asset}/details`,
|
||||
options: {
|
||||
resolveWithFullResponse: true,
|
||||
method: 'GET'
|
||||
}
|
||||
}
|
||||
try {
|
||||
const res = await noblox.http(httpOpt);
|
||||
if (res.statusCode === 200) {
|
||||
resolve(JSON.parse(res.body));
|
||||
} else {
|
||||
// Sourced from: https://stackoverflow.com/a/32278428
|
||||
const isAnObject = (val_1) => !!(val_1 instanceof Array || val_1 instanceof Object);
|
||||
|
||||
const body = isAnObject(res.body) ? JSON.parse(res.body) : {};
|
||||
if (body.errors && body.errors.length > 0) {
|
||||
const errors = body.errors.map((e) => {
|
||||
return e.message;
|
||||
});
|
||||
reject(new Error(`${res.statusCode} ${errors.join(', ')}`));
|
||||
} else {
|
||||
reject(new Error(`${res.statusCode} ${res.body}`));
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
return reject(error);
|
||||
}
|
||||
})
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user