Parse map models and validate them (#13)
The enhancements: - Added map validation that checks for some common errors that maps have - /take: Does map validation but will still take the map even if there are problems - /take: Improved the help text and added links to the maptest places - /submit: Does map validation and will not let you submit if there are problems - /submissions: Now shows the map's display name and creator - Added an ESLint config and tidied some things up - Updated some of the packages Reviewed-on: #13 Co-authored-by: Carter Penterman <carterpenterman@gmail.com> Co-committed-by: Carter Penterman <carterpenterman@gmail.com>
This commit is contained in:
@ -4,14 +4,14 @@ 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, SubmissionColumnsString, createSubmissionLine, getSubmissionLine } = require("../common.js");
|
||||
const { AssetType, getAssetInfo, SubmissionColumnsString, createSubmissionLine, getSubmissionLine, validateMapAsset, getValidationMessage } = require("../common.js");
|
||||
|
||||
async function robloxUserFromDiscord(id) {
|
||||
if (isNaN(id)) return undefined;
|
||||
try {
|
||||
const res = await axios.get(`https://api.fiveman1.net/v1/users/${id}`);
|
||||
return res.data.result.robloxId;
|
||||
} catch (error) {
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
@ -21,11 +21,14 @@ async function robloxUsernameFromId(id) {
|
||||
try {
|
||||
const res = await axios.get(`https://users.roblox.com/v1/users/${id}`);
|
||||
return res.data.name;
|
||||
} catch (error) {
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('discord.js').ChatInputCommandInteraction} interaction
|
||||
*/
|
||||
async function execute(interaction) {
|
||||
const userId = await robloxUserFromDiscord(interaction.user.id);
|
||||
if (!userId) {
|
||||
@ -33,7 +36,7 @@ async function execute(interaction) {
|
||||
await interaction.reply({content: msg, ephemeral: true});
|
||||
return;
|
||||
}
|
||||
const game = interaction.options.getString("game");
|
||||
const game = interaction.options.getString("game", true);
|
||||
|
||||
const fname = submissions[game];
|
||||
if (fname === undefined) {
|
||||
@ -45,7 +48,7 @@ async function execute(interaction) {
|
||||
fs.writeFileSync(fname, SubmissionColumnsString);
|
||||
}
|
||||
|
||||
const id = interaction.options.getInteger("asset_id");
|
||||
const id = interaction.options.getInteger("asset_id", true);
|
||||
await noblox.setCookie(cookies[game]);
|
||||
|
||||
// Check that the bot owns this model
|
||||
@ -74,23 +77,38 @@ async function execute(interaction) {
|
||||
const csvFile = fs.readFileSync(fname);
|
||||
const lines = parse(csvFile, {delimiter: ',', fromLine: 2});
|
||||
|
||||
let csvString = SubmissionColumnsString;
|
||||
for (let lineStr of lines) {
|
||||
const line = getSubmissionLine(lineStr);
|
||||
|
||||
if (id === line.modelId) {
|
||||
await interaction.reply({content: `This map (id: ${id}) was already submitted on <t:${line.timestamp}:d>.`, ephemeral: true});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
csvString += createSubmissionLine(line.modelId, line.timestamp, line.userId, line.username);
|
||||
const mapValidatePromise = validateMapAsset(id, game);
|
||||
|
||||
const message = await interaction.reply("⌛ Validating map...");
|
||||
|
||||
const validation = await mapValidatePromise;
|
||||
const msg = getValidationMessage(validation, game, true);
|
||||
await message.edit(msg);
|
||||
|
||||
if (!validation.valid) {
|
||||
await interaction.followUp("Due to having problems, your map was **NOT submitted**.");
|
||||
return;
|
||||
}
|
||||
|
||||
let csvString = SubmissionColumnsString;
|
||||
for (let lineStr of lines) {
|
||||
const line = getSubmissionLine(lineStr);
|
||||
csvString += createSubmissionLine(line.modelId, line.timestamp, line.userId, line.username, line.displayName, line.creator);
|
||||
}
|
||||
|
||||
const unixTimestamp = Math.round(+new Date()/1000);
|
||||
csvString += createSubmissionLine(id, unixTimestamp, userId, await robloxUsernameFromId(userId));
|
||||
csvString += createSubmissionLine(id, unixTimestamp, userId, await robloxUsernameFromId(userId), validation.displayName, validation.creator);
|
||||
fs.writeFileSync(fname, csvString);
|
||||
|
||||
await interaction.reply(`Map (id: ${id}) successfully submitted.`);
|
||||
await interaction.followUp(`Map (id: ${id}) successfully submitted.`);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
Reference in New Issue
Block a user