From 6e50de5310997808e757051f8b342f0beb89dac4 Mon Sep 17 00:00:00 2001 From: Carter Penterman Date: Fri, 13 May 2022 12:01:16 -0500 Subject: [PATCH] added submissions --- README.md | 4 +- bot.js | 6 ++- commands/submissions.js | 39 +++++++++++++++++++ commands/submit.js | 73 +++++++++++++++++++++++++++++++++++ commands/take.js | 84 ++++++++++++++++++++++++----------------- files/.gitignore | 2 + package.json | 4 +- 7 files changed, 175 insertions(+), 37 deletions(-) create mode 100644 commands/submissions.js create mode 100644 commands/submit.js create mode 100644 files/.gitignore diff --git a/README.md b/README.md index f500837..0e08b54 100644 --- a/README.md +++ b/README.md @@ -17,4 +17,6 @@ node deploy-commands.js node bot.js ``` -also, the bot requires the bot and applications.commands scope, which can be found in developer portal -> bot -> oauth2 -> url generator -> scopes, simply generate link with the bot and applications.commands scopes checked then add bot to server (can be done if bot already in server). no other permissions are needed. \ No newline at end of file +also, the bot requires the bot and applications.commands scope, which can be found in developer portal -> bot -> oauth2 -> url generator -> scopes, simply generate link with the bot and applications.commands scopes checked then add bot to server (can be done if bot already in server). no other permissions are needed. + +finally, the bhop/surf/deathrun cookies in the config.json require the full ``_|WARNING:-DO-NOT-SHARE-THIS.--Sharing-this-will-allow-someone-to-log-in-as-you-and-to-steal-your-ROBUX-and-items.|_`` text, so include it \ No newline at end of file diff --git a/bot.js b/bot.js index 097d161..ee32b8e 100644 --- a/bot.js +++ b/bot.js @@ -2,7 +2,7 @@ const fs = require('node:fs'); const { Client, Collection, Intents } = require("discord.js"); const {token} = require("./config.json"); -const client = new Client({ intents: [Intents.FLAGS.GUILDS]}); +const client = new Client({intents: [Intents.FLAGS.GUILDS]}); client.commands = new Collection(); const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); @@ -35,6 +35,10 @@ client.on("error", async error => { client.once("ready", () => { console.log("Ready"); + client.user.setActivity({ + name: "use /take and /submit", + type: 0 + }); }); client.login(token); \ No newline at end of file diff --git a/commands/submissions.js b/commands/submissions.js new file mode 100644 index 0000000..567e47f --- /dev/null +++ b/commands/submissions.js @@ -0,0 +1,39 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); +const { MessageAttachment } = require("discord.js"); +const fs = require('node:fs'); + +async function execute(interaction) { + 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)) { + await interaction.reply(`No submissions exist yet for ${game}.`); + return; + } + + const csv = fs.readFileSync(fname); + const file = new MessageAttachment(csv, fname); + await interaction.reply({files: [file]}); + +} + +module.exports = { + data: new SlashCommandBuilder() + .setName('submissions') + .setDescription('View the submissions in .csv format') + .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"})) + , + execute +}; \ No newline at end of file diff --git a/commands/submit.js b/commands/submit.js new file mode 100644 index 0000000..8d98559 --- /dev/null +++ b/commands/submit.js @@ -0,0 +1,73 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); +const { parse } = require("csv-parse/sync"); +const fs = require('node:fs'); +const noblox = require("noblox.js"); + +async function execute(interaction) { + 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; + } + } 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 +}; \ No newline at end of file diff --git a/commands/take.js b/commands/take.js index f592e94..c33b565 100644 --- a/commands/take.js +++ b/commands/take.js @@ -2,6 +2,55 @@ const { SlashCommandBuilder } = require('@discordjs/builders'); const noblox = require("noblox.js"); const { bhopCookie, surfCookie, deathrunCookie } = require("../config.json"); +async function execute(interaction) { + const game = interaction.options.getString("game"); + let cookie; + if (game === "bhop") cookie = bhopCookie; + else if (game === "surf") cookie = surfCookie; + else if (game === "deathrun") cookie = deathrunCookie; + else { + await interaction.reply({content: "Invalid game specified!", ephemeral: true}); + return; + } + const id = interaction.options.getInteger("asset_id"); + await noblox.setCookie(cookie).then(async () => { + + // validate that this is a model + 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; + } + } catch (error) { + console.log(error); + await interaction.reply({content: `There is a problem with this asset ID (id: ${id}).`, ephemeral: true}); + return; + } + + noblox.buy(id, 0).then(async () => { + await interaction.reply( +` +Now that your map (id: ${id}) has been taken by the ${game} maptest bot you can load it into the ${game} maptest place. To load your map, join the game and say +\`\`\` +!map ${id} +\`\`\`Read what it says. If your map successfully loaded type !rtv and then choose your map. +If it did not load successfully, you can expand the chat to view the full error message by clicking and dragging on the edge of the chat. +` + ); + }) + .catch(async (error) => { + if (error.message == "You already own this item.") { + await interaction.reply({content: "The bot has already taken this model!", ephemeral: true}); + } else { + await interaction.reply({content: `An error occured trying to take the model (id: ${id}). Make sure it is uncopylocked!`, ephemeral: true}); + console.log(`Could not take asset ID ${id}: `); + console.log(error); + } + }); + }); +} + module.exports = { data: new SlashCommandBuilder() .setName('take') @@ -16,38 +65,5 @@ module.exports = { .setDescription("The asset ID of the model") .setRequired(true)) , - async execute(interaction) { - const game = interaction.options.getString("game"); - let cookie; - if (game == "bhop") cookie = bhopCookie; - else if (game == "surf") cookie = surfCookie; - else if (game == "deathrun") cookie = deathrunCookie; - else { - await interaction.reply("Invalid game specified!"); - return; - } - const id = interaction.options.getInteger("asset_id"); - await noblox.setCookie(cookie).then(async () => { - noblox.buy(id, 0).then(async () => { - await interaction.reply( -` -Now that your map (id: ${id}) has been taken by the ${game} maptest bot you can load it into the ${game} maptest place. To load your map, join the game and say -\`\`\` -!map ${id} -\`\`\`Read what it says. If your map successfully loaded type !rtv and then choose your map. -If it did not load successfully, you can expand the chat to view the full error message by clicking and dragging on the edge of the chat. -` - ); - }) - .catch(async (error) => { - if (error.message == "You already own this item.") { - await interaction.reply("The bot has already taken this model!"); - } else { - await interaction.reply(`An error occured trying to take the model (id: ${id}). Make sure it is uncopylocked!`); - console.log(`Could not take asset ID ${id}: `); - console.log(error); - } - }); - }); - } + execute }; \ No newline at end of file diff --git a/files/.gitignore b/files/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/files/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/package.json b/package.json index b8be540..3e5a6fd 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,10 @@ "dependencies": { "@discordjs/builders": "^0.13.0", "@discordjs/rest": "^0.4.1", + "csv-parse": "^5.0.4", "discord-api-types": "^0.32.1", "discord.js": "^13.6.0", - "noblox.js": "^4.13.1" + "noblox.js": "^4.13.1", + "node-csv": "^0.1.2" } }