maptest-bot/commands/take.js

92 lines
3.6 KiB
JavaScript
Raw Normal View History

const { SlashCommandBuilder } = require('@discordjs/builders');
const noblox = require("noblox.js");
const { cookies, commands, gamePlaces } = require("../config/config.js");
const { AssetType, getAssetInfo, validateMapAsset, getValidationMessage } = require("../common.js");
/**
* @param {import('discord.js').ChatInputCommandInteraction} interaction
*/
2022-05-13 17:01:16 +00:00
async function execute(interaction) {
const game = interaction.options.getString("game", true);
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;
}
const id = interaction.options.getInteger("asset_id", true);
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;
}
// Validate that this is a model
const assetInfo = await getAssetInfo(id);
if (assetInfo.status === 404) {
await interaction.reply({content: `This asset may not exist or is not a model (id: ${id}). Your map must be a model.`, ephemeral: true});
return;
}
// 403 (Forbidden) means the asset isn't distributed
if (assetInfo.status === 403 || !assetInfo.forSale) {
await interaction.reply({content: `This model (id: ${id}) is off sale. Please configure it to be on sale (Configure -> Distribute on Creator Store).`, ephemeral: true});
return;
}
if (assetInfo.typeId !== AssetType.Model) {
await interaction.reply({content: `This asset (id: ${id}) is not a model. Your map must be a model.`, ephemeral: true});
return;
}
if (assetInfo.price !== 0) {
await interaction.reply({content: `This model (id: ${id}) is not free. Please change the price to be free.`, ephemeral: true});
return;
}
2022-05-13 17:01:16 +00:00
// Make a "fake" product info object for the Noblox buy method
const productInfo = {
PriceInRobux: assetInfo.price,
ProductId: assetInfo.productId,
Creator: {
Id: assetInfo.creatorId
}
};
2024-04-24 16:03:25 +00:00
// Show "Bot is thinking..."
await interaction.deferReply();
2024-04-24 16:03:25 +00:00
// Kick off the buy request
const buyPromise = noblox.buy({product: productInfo, price: 0});
2024-04-24 16:03:25 +00:00
// Validate and send the validation result
const validation = await validateMapAsset(id, game);
const msg = getValidationMessage(validation, game, false);
2024-04-24 16:03:25 +00:00
await interaction.editReply(msg);
2024-04-24 16:03:25 +00:00
// Make sure the buy request is done
await buyPromise;
2024-04-24 16:03:25 +00:00
await interaction.followUp(
2022-05-13 17:01:16 +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
`
);
}
2023-03-04 21:09:41 +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
};