Use non-rate-limited API and improve some stuff

This commit is contained in:
2024-04-15 21:15:35 -05:00
parent 14d57a4561
commit 214790809d
3 changed files with 102 additions and 53 deletions

@ -4,6 +4,7 @@ const fs = require('node:fs');
const noblox = require("noblox.js");
const axios = require("axios").default;
const { submissions, commands, cookies } = require("../config/config.js");
const { AssetType, getAssetInfo } = require("../common.js");
async function robloxUserFromDiscord(id) {
if (isNaN(id)) return undefined;
@ -28,7 +29,7 @@ async function robloxUsernameFromId(id) {
async function execute(interaction) {
const userId = await robloxUserFromDiscord(interaction.user.id);
if (!userId) {
const msg = "You don't have a Roblox account linked with your Discord account. Use !link with rbhop dog to link your account.";
const msg = "You don't have a Roblox account linked with your Discord account. Use !link with the rbhop dog bot to link your account.";
await interaction.reply({content: msg, ephemeral: true});
return;
}
@ -48,33 +49,25 @@ async function execute(interaction) {
await noblox.setCookie(cookies[game]);
// Check that the bot owns this model
if (!(await noblox.getOwnership((await noblox.getCurrentUser()).UserID, id, "Asset"))) {
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 noblox.getProductInfo(id);
if (info.AssetTypeId != 10) {
await interaction.reply({content: `(id: ${id}) is not a valid model ID.`, ephemeral: true});
return;
}
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.`;
await interaction.reply({content: msg, 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});
return;
}
console.log(error);
await interaction.reply({content: `There is a problem with this asset ID (id: ${id}).`, ephemeral: true});
const assetInfo = await getAssetInfo(id);
if (assetInfo.creatorId != userId) {
const assetUsernamePromise = robloxUsernameFromId(assetInfo.creatorId);
const interactionUsernamePromise = robloxUsernameFromId(userId);
const assetUsername = await assetUsernamePromise;
const interactionUsername = await interactionUsernamePromise;
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;
}
// Shouldn't really be possible but who knows...
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;
}
@ -86,7 +79,7 @@ async function execute(interaction) {
const rid = record[0];
const rtimestamp = record[1];
if (id == rid) {
await interaction.reply({content: `Tried to submit map (id: ${id}) that already exists!`, ephemeral: true});
await interaction.reply({content: `This map (id: ${id}) was already submitted on <t:${rtimestamp}:d>.`, ephemeral: true});
return;
}
s += `${rid},${rtimestamp}\n`;