Merge pull request 'add wrapper around deprecated API' (#3) from fix/wrapper-around-api into master
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #3
This commit is contained in:
commit
f6600f3f74
@ -48,7 +48,7 @@ async function execute(interaction) {
|
|||||||
|
|
||||||
const id = interaction.options.getInteger("asset_id");
|
const id = interaction.options.getInteger("asset_id");
|
||||||
try {
|
try {
|
||||||
const info = await noblox.getProductInfo(id);
|
const info = await getProductInfo(id);
|
||||||
if (info.AssetTypeId != 10) {
|
if (info.AssetTypeId != 10) {
|
||||||
await interaction.reply({content: `(id: ${id}) is not a valid model ID.`, ephemeral: true});
|
await interaction.reply({content: `(id: ${id}) is not a valid model ID.`, ephemeral: true});
|
||||||
return;
|
return;
|
||||||
@ -104,4 +104,37 @@ module.exports = {
|
|||||||
.setRequired(true))
|
.setRequired(true))
|
||||||
,
|
,
|
||||||
execute
|
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);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
@ -14,10 +14,10 @@ async function execute(interaction) {
|
|||||||
}
|
}
|
||||||
const id = interaction.options.getInteger("asset_id");
|
const id = interaction.options.getInteger("asset_id");
|
||||||
await noblox.setCookie(cookie).then(async () => {
|
await noblox.setCookie(cookie).then(async () => {
|
||||||
|
let info;
|
||||||
// validate that this is a model
|
// validate that this is a model
|
||||||
try {
|
try {
|
||||||
const info = await noblox.getProductInfo(id);
|
info = await getProductInfo(id);
|
||||||
if (info.AssetTypeId != 10) {
|
if (info.AssetTypeId != 10) {
|
||||||
await interaction.reply({content: `(id: ${id}) is not a valid model ID.`, ephemeral: true});
|
await interaction.reply({content: `(id: ${id}) is not a valid model ID.`, ephemeral: true});
|
||||||
return;
|
return;
|
||||||
@ -28,7 +28,7 @@ async function execute(interaction) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
noblox.buy(id, 0).then(async () => {
|
buy(undefined, await noblox.getGeneralToken(), info, 0).then(async () => {
|
||||||
await interaction.reply(
|
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
|
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
|
||||||
@ -66,4 +66,89 @@ module.exports = {
|
|||||||
.setRequired(true))
|
.setRequired(true))
|
||||||
,
|
,
|
||||||
execute
|
execute
|
||||||
};
|
};
|
||||||
|
|
||||||
|
async function buy (jar, token, product, price) {
|
||||||
|
const robux = product.PriceInRobux || 0
|
||||||
|
const productId = product.ProductId
|
||||||
|
if (price) {
|
||||||
|
if (typeof price === 'number') {
|
||||||
|
if (robux !== price) {
|
||||||
|
throw new Error('Price requirement not met. Requested price: ' + price + ' Actual price: ' + robux)
|
||||||
|
}
|
||||||
|
} else if (typeof price === 'object') {
|
||||||
|
const high = price.high
|
||||||
|
const low = price.low
|
||||||
|
if (high) {
|
||||||
|
if (robux > high) {
|
||||||
|
throw new Error('Price requirement not met. Requested price: <=' + high + ' Actual price: ' + robux)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (low) {
|
||||||
|
if (robux < low) {
|
||||||
|
throw new Error('Price requirement not met. Requested price: >=' + low + ' Actual price: ' + robux)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const httpOpt = {
|
||||||
|
url: '//economy.roblox.com/v1/purchases/products/' + productId,
|
||||||
|
options: {
|
||||||
|
method: 'POST',
|
||||||
|
jar: jar,
|
||||||
|
headers: {
|
||||||
|
'X-CSRF-TOKEN': token
|
||||||
|
},
|
||||||
|
json: {
|
||||||
|
expectedCurrency: 1,
|
||||||
|
expectedPrice: robux,
|
||||||
|
expectedSellerId: product.Creator.Id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const json = await noblox.http(httpOpt);
|
||||||
|
let err = json.errorMsg;
|
||||||
|
if (json.reason === 'InsufficientFunds') {
|
||||||
|
err = 'You need ' + json.shortfallPrice + ' more robux to purchase this item.';
|
||||||
|
} else if (json.errorMsg) {
|
||||||
|
err = json.errorMsg;
|
||||||
|
}
|
||||||
|
if (!err) {
|
||||||
|
return { productId, price: robux };
|
||||||
|
} else {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user