maptest-bot/commands/submit.js
fiveman1 630b4f07b9
All checks were successful
continuous-integration/drone/push Build is passing
feature/verify-submit (#1)
Bot will check the roblox account linked to the user's discord account (via verify.eryn.io) when submitting a map. If the user does not own the model they are trying to submit they can't submit it.

Co-authored-by: Carter Penterman <carterpenterman@gmail.com>
Reviewed-on: #1
Co-authored-by: fiveman1 <fiveman1@noreply@itzana.me>
Co-committed-by: fiveman1 <fiveman1@noreply@itzana.me>
2022-07-12 02:09:07 +00:00

107 lines
3.6 KiB
JavaScript

const { SlashCommandBuilder } = require('@discordjs/builders');
const { parse } = require("csv-parse/sync");
const fs = require('node:fs');
const noblox = require("noblox.js");
const axios = require("axios").default
async function robloxUserFromDiscord(id) {
if (isNaN(id)) return undefined;
try {
const res = await axios.get(`https://verify.eryn.io/api/user/${id}`)
return res.data.robloxId
} catch (error) {
return undefined;
}
}
async function robloxUsernameFromId(id) {
if (isNaN(id)) return undefined;
try {
const res = await axios.get(`https://users.roblox.com/v1/users/${id}`)
return res.data.name
} catch (error) {
return undefined;
}
}
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. Visit https://verify.eryn.io/";
await interaction.reply({content: msg, ephemeral: true});
return;
}
const game = interaction.options.getString("game");
let fname;
if (game === "bhop") fname = "files/bhop_submissions.csv";
else if (game === "surf") fname = "files/surf_submissions.csv";
else if (game === "deathrun") fname = "files/deathrun_submissions.csv";
else {
await interaction.reply({content: "Invalid game specified!", ephemeral: true});
return;
}
if (!fs.existsSync(fname)) {
fs.writeFileSync(fname, "id,timestamp\n");
}
const id = interaction.options.getInteger("asset_id");
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) {
console.log(error);
await interaction.reply({content: `There is a problem with this asset ID (id: ${id}).`, ephemeral: true});
return;
}
const csv = fs.readFileSync(fname);
const records = parse(csv, {delimiter: ',', fromLine: 2});
let s = "id,timestamp\n";
for (let record of records) {
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});
return;
}
s += `${rid},${rtimestamp}\n`;
}
const unix = Math.round(+new Date()/1000);
s += `${id},${unix}\n`;
fs.writeFileSync(fname, s);
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)
.addChoices({name: "bhop", value: "bhop"}, {name: "surf", value: "surf"}, {name: "deathrun", value: "deathrun"}))
.addIntegerOption(option =>
option.setName("asset_id")
.setDescription("The asset ID of the model")
.setRequired(true))
,
execute
};