2022-05-12 19:56:35 +00:00
const { SlashCommandBuilder } = require ( '@discordjs/builders' ) ;
const noblox = require ( "noblox.js" ) ;
2024-04-24 06:28:07 +00:00
const { cookies , commands , gamePlaces } = require ( "../config/config.js" ) ;
2024-08-28 04:11:28 +00:00
const { getAssetInfo , buyModel , validateMapAsset , getValidationMessage } = require ( "../common.js" ) ;
2022-05-12 19:56:35 +00:00
2024-04-24 06:28:07 +00:00
/ * *
* @ param { import ( 'discord.js' ) . ChatInputCommandInteraction } interaction
* /
2022-05-13 17:01:16 +00:00
async function execute ( interaction ) {
2024-04-24 06:28:07 +00:00
const game = interaction . options . getString ( "game" , true ) ;
2023-03-08 20:37:40 +00:00
const cookie = cookies [ game ] ;
if ( cookie === undefined ) {
2024-04-25 17:33:55 +00:00
await interaction . editReply ( "🚫 Invalid game specified!" ) ;
2022-05-13 17:01:16 +00:00
return ;
}
2024-04-15 08:13:32 +00:00
2024-04-24 06:28:07 +00:00
const id = interaction . options . getInteger ( "asset_id" , true ) ;
2024-08-28 02:28:33 +00:00
noblox . setCookie ( cookie , false ) ;
2024-04-25 17:33:55 +00:00
2024-04-29 20:38:51 +00:00
let alreadyOwned ;
2024-04-25 17:33:55 +00:00
try {
2024-04-29 20:38:51 +00:00
// Check if the bot already owns this asset
2024-08-28 04:11:28 +00:00
const robloxUser = await noblox . getAuthenticatedUser ( ) ;
alreadyOwned = await noblox . getOwnership ( robloxUser . id , id , "Asset" ) ;
2024-04-25 17:33:55 +00:00
} catch ( error ) {
if ( error . message !== "400 The specified Asset does not exist!" ) {
throw error ;
}
2024-08-22 03:48:30 +00:00
await interaction . editReply ( ` 🚫 This asset does not exist (id: \` ${ id } \` ). ` ) ;
2024-04-15 08:13:32 +00:00
return ;
}
2024-04-25 17:33:55 +00:00
2024-04-15 08:13:32 +00:00
// Validate that this is a model
2024-04-16 02:15:35 +00:00
const assetInfo = await getAssetInfo ( id ) ;
2024-08-27 02:38:17 +00:00
if ( assetInfo . status === 404 ) {
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. ` ) ;
2024-04-16 02:15:35 +00:00
return ;
}
2024-08-27 02:38:17 +00:00
if ( assetInfo . status < 200 || assetInfo . status >= 300 ) {
await interaction . editReply ( ` 🚫 Something unexpected went wrong trying to retrive info about the model (id: \` ${ id } \` ). ` ) ;
return ;
}
if ( assetInfo . forSale === false ) {
2024-08-22 03:48:30 +00:00
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). ` ) ;
2024-04-16 02:15:35 +00:00
return ;
}
2024-08-22 03:48:30 +00:00
if ( ! assetInfo . isModel ) {
2024-08-27 02:38:17 +00:00
await interaction . editReply ( ` 🚫 This asset (id: \` ${ id } \` ) is not a model. Your map must be a model. ` ) ;
2024-04-16 02:15:35 +00:00
return ;
}
2024-08-21 21:55:26 +00:00
if ( ! assetInfo . isFree ) {
2024-08-22 03:48:30 +00:00
await interaction . editReply ( ` 🚫 This [model](<https://create.roblox.com/store/asset/ ${ id } />) (id: \` ${ id } \` ) is not free. Please change the price to be free. ` ) ;
2024-04-15 08:13:32 +00:00
return ;
}
2022-05-13 17:01:16 +00:00
2024-04-24 16:03:25 +00:00
// Kick off the buy request
2024-04-29 20:38:51 +00:00
let buyPromise ;
if ( ! alreadyOwned ) {
2024-08-22 03:48:30 +00:00
buyPromise = buyModel ( id ) ;
2024-04-29 20:38:51 +00:00
}
2024-04-16 02:15:35 +00:00
2024-04-24 16:03:25 +00:00
// Validate and send the validation result
const validation = await validateMapAsset ( id , game ) ;
2024-04-24 06:28:07 +00:00
const msg = getValidationMessage ( validation , game , false ) ;
2024-04-24 16:03:25 +00:00
await interaction . editReply ( msg ) ;
2024-04-29 20:38:51 +00:00
2024-04-29 20:42:01 +00:00
if ( alreadyOwned ) {
2024-04-29 20:38:51 +00:00
// Unicode is for the information emoji [i]
await interaction . followUp ( ` \u 2139 \u FE0F The ${ game } maptest bot already has this model, so no action was taken. ` ) ;
return ;
}
2024-04-24 06:28:07 +00:00
2024-04-24 16:03:25 +00:00
// Make sure the buy request is done
2024-08-22 03:48:30 +00:00
const success = await buyPromise ;
if ( ! success ) {
await interaction . followUp ( ` 🚫 Something went wrong when trying to buy the [model](<https://create.roblox.com/store/asset/ ${ id } />) (id: \` ${ id } \` ). ` ) ;
return ;
}
2024-04-24 06:28:07 +00:00
await interaction . followUp (
2022-05-13 17:01:16 +00:00
`
2024-04-24 06:28:07 +00:00
Now that your [ map ( id : $ { id } ) ] ( < https : //create.roblox.com/store/asset/${id}/>) has been taken by the bot you can load it into the [${game} maptest place](<${gamePlaces[game]}>).
To load your map , join the game and do \ ` !map ${ id } \` . If your map successfully loaded, do \` !rtv \` and then choose your map.
Otherwise , you can expand the chat to view the full error message by clicking and dragging on the edge of the chat .
2022-05-13 17:01:16 +00:00
`
2024-04-16 02:15:35 +00:00
) ;
2024-04-15 08:13:32 +00:00
}
2023-03-04 21:09:41 +00:00
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
} ;