2022-05-12 19:56:35 +00:00
const { SlashCommandBuilder } = require ( '@discordjs/builders' ) ;
const noblox = require ( "noblox.js" ) ;
2023-03-08 20:37:40 +00:00
const { cookies , commands } = require ( "../config/config.js" ) ;
2022-05-12 19:56:35 +00:00
2022-05-13 17:01:16 +00:00
async function execute ( interaction ) {
const game = interaction . options . getString ( "game" ) ;
2023-03-08 20:37:40 +00:00
const cookie = cookies [ game ] ;
if ( cookie === undefined ) {
2022-05-13 17:01:16 +00:00
await interaction . reply ( { content : "Invalid game specified!" , ephemeral : true } ) ;
return ;
}
2024-04-15 08:13:32 +00:00
2022-05-13 17:01:16 +00:00
const id = interaction . options . getInteger ( "asset_id" ) ;
2024-04-15 08:13:32 +00:00
await noblox . setCookie ( cookie ) ;
// Check that the bot doesn't already own this asset
if ( await noblox . getOwnership ( await noblox . getCurrentUser ( "UserID" ) , id , "Asset" ) ) {
const msg = ` The ${ game } maptest bot already has this model (id: ${ id } ) ` ;
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 } ) ;
2022-05-13 17:01:16 +00:00
return ;
}
2024-04-15 08:13:32 +00:00
console . log ( error ) ;
await interaction . reply ( { content : ` There is a problem with this asset ID (id: ${ id } ). ` , ephemeral : true } ) ;
return ;
}
2022-05-13 17:01:16 +00:00
2024-04-15 08:13:32 +00:00
try {
await buy ( info ) ;
await interaction . reply (
2022-05-13 17:01:16 +00:00
`
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
\ ` \` \`
! map $ { id }
\ ` \` \` 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 .
`
2024-04-15 08:13:32 +00:00
) ;
} 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 ) ;
2023-03-04 21:09:41 +00:00
}
}
2024-04-15 08:13:32 +00:00
}
async function buy ( product ) {
const productId = product . ProductId ;
2023-03-04 21:09:41 +00:00
const httpOpt = {
url : '//economy.roblox.com/v1/purchases/products/' + productId ,
options : {
method : 'POST' ,
2024-04-15 08:13:32 +00:00
jar : undefined ,
2023-03-04 21:09:41 +00:00
headers : {
2024-04-15 08:13:32 +00:00
'X-CSRF-TOKEN' : await noblox . getGeneralToken ( )
2023-03-04 21:09:41 +00:00
} ,
json : {
expectedCurrency : 1 ,
2024-04-15 08:13:32 +00:00
expectedPrice : 0 ,
2023-03-04 21:09:41 +00:00
expectedSellerId : product . Creator . Id
}
}
2024-04-15 08:13:32 +00:00
} ;
2023-03-04 21:09:41 +00:00
const json = await noblox . http ( httpOpt ) ;
2024-04-15 08:13:32 +00:00
console . log ( json )
let err = json . errorMsg
2023-03-04 21:09:41 +00:00
if ( json . reason === 'InsufficientFunds' ) {
err = 'You need ' + json . shortfallPrice + ' more robux to purchase this item.' ;
} else if ( json . errorMsg ) {
err = json . errorMsg ;
}
if ( ! err ) {
2024-04-15 08:13:32 +00:00
return productId ;
2023-03-04 21:09:41 +00:00
} else {
throw new Error ( err ) ;
}
}
2024-04-15 08:13:32 +00:00
module . exports = {
data : new SlashCommandBuilder ( )
. setName ( 'take' )
. setDescription ( 'Takes an asset ID' )
. addStringOption ( option =>
option . setName ( "game" )
. setDescription ( "Select the maptest game" )
. setRequired ( true )
. addChoices ( ... commands ) )
. addIntegerOption ( option =>
option . setName ( "asset_id" )
. setDescription ( "The asset ID of the model" )
. setRequired ( true ) )
,
execute
} ;