maptest-bot/commands/submit.js

111 lines
4.2 KiB
JavaScript
Raw Normal View History

2022-05-13 17:01:16 +00:00
const { SlashCommandBuilder } = require('@discordjs/builders');
const { parse } = require("csv-parse/sync");
const fs = require('node:fs');
const noblox = require("noblox.js");
2022-10-22 04:04:23 +00:00
const axios = require("axios").default;
const { submissions, commands, cookies } = require("../config/config.js");
const { AssetType, getAssetInfo, SubmissionColumnsString, createSubmissionLine, getSubmissionLine } = require("../common.js");
async function robloxUserFromDiscord(id) {
if (isNaN(id)) return undefined;
try {
2022-10-22 04:04:23 +00:00
const res = await axios.get(`https://api.fiveman1.net/v1/users/${id}`);
return res.data.result.robloxId;
} catch (error) {
return undefined;
}
}
async function robloxUsernameFromId(id) {
if (isNaN(id)) return undefined;
try {
2022-10-22 04:04:23 +00:00
const res = await axios.get(`https://users.roblox.com/v1/users/${id}`);
return res.data.name;
} catch (error) {
return undefined;
}
}
2022-05-13 17:01:16 +00:00
async function execute(interaction) {
2022-10-22 04:04:23 +00:00
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 the rbhop dog bot to link your account.";
await interaction.reply({content: msg, ephemeral: true});
return;
}
2022-05-13 17:01:16 +00:00
const game = interaction.options.getString("game");
const fname = submissions[game];
if (fname === undefined) {
2022-05-13 17:01:16 +00:00
await interaction.reply({content: "Invalid game specified!", ephemeral: true});
return;
}
if (!fs.existsSync(fname)) {
fs.writeFileSync(fname, SubmissionColumnsString);
2022-05-13 17:01:16 +00:00
}
const id = interaction.options.getInteger("asset_id");
await noblox.setCookie(cookies[game]);
// Check that the bot owns this model
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;
}
const assetInfo = await getAssetInfo(id);
2024-04-16 02:17:33 +00:00
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...
2024-04-16 02:17:33 +00:00
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});
2022-05-13 17:01:16 +00:00
return;
}
const csvFile = fs.readFileSync(fname);
const lines = parse(csvFile, {delimiter: ',', fromLine: 2});
2022-05-13 17:01:16 +00:00
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});
2022-05-13 17:01:16 +00:00
return;
}
csvString += createSubmissionLine(line.modelId, line.timestamp, line.userId, line.username);
}
2022-05-13 17:01:16 +00:00
const unixTimestamp = Math.round(+new Date()/1000);
csvString += createSubmissionLine(id, unixTimestamp, userId, await robloxUsernameFromId(userId));
fs.writeFileSync(fname, csvString);
2022-05-13 17:01:16 +00:00
await interaction.reply(`Map (id: ${id}) successfully submitted.`);
}
module.exports = {
data: new SlashCommandBuilder()
.setName('submit')
.setDescription('Submit your map')
.addStringOption(option =>
option.setName("game")
.setDescription("Select the maptest game")
.setRequired(true)
2023-03-19 02:21:59 +00:00
.addChoices(...commands))
2022-05-13 17:01:16 +00:00
.addIntegerOption(option =>
option.setName("asset_id")
.setDescription("The asset ID of the model")
.setRequired(true))
,
execute
};