From d8a0890e26b4e4231f66535870b48c401f09a80e Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 8 Mar 2023 12:37:40 -0800 Subject: [PATCH] use dictionary instead of hardcoded if-else for each game --- commands/submissions.js | 18 ++++++++++++------ commands/submit.js | 18 ++++++++++++------ commands/take.js | 18 ++++++++++++------ 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/commands/submissions.js b/commands/submissions.js index 567e47f..4faf47d 100644 --- a/commands/submissions.js +++ b/commands/submissions.js @@ -1,15 +1,21 @@ const { SlashCommandBuilder } = require('@discordjs/builders'); const { MessageAttachment } = require("discord.js"); const fs = require('node:fs'); +const gameDict = { + bhop: "files/bhop_submissions.csv", + surf: "files/surf_submissions.csv", + deathrun: "files/deathrun_submissions.csv", +}; +var commandChoices = []; +for (const [game, file] of Object.entries(gameDict)) { + commandChoices.push({name: game, value: game}) +} 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 { + let fname = gameDict[game]; + if (fname === undefined) { await interaction.reply({content: "Invalid game specified!", ephemeral: true}); return; } @@ -33,7 +39,7 @@ module.exports = { option.setName("game") .setDescription("Select the maptest game") .setRequired(true) - .addChoices({name: "bhop", value: "bhop"}, {name: "surf", value: "surf"}, {name: "deathrun", value: "deathrun"})) + .addChoices(commandChoices)) , execute }; \ No newline at end of file diff --git a/commands/submit.js b/commands/submit.js index aecfd87..029db72 100644 --- a/commands/submit.js +++ b/commands/submit.js @@ -3,6 +3,15 @@ const { parse } = require("csv-parse/sync"); const fs = require('node:fs'); const noblox = require("noblox.js"); const axios = require("axios").default; +const gameDict = { + bhop: "files/bhop_submissions.csv", + surf: "files/surf_submissions.csv", + deathrun: "files/deathrun_submissions.csv", +}; +var commandChoices = []; +for (const [game, file] of Object.entries(gameDict)) { + commandChoices.push({name: game, value: game}) +} async function robloxUserFromDiscord(id) { if (isNaN(id)) return undefined; @@ -33,11 +42,8 @@ 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 { + let fname = gameDict[game]; + if (fname === undefined) { await interaction.reply({content: "Invalid game specified!", ephemeral: true}); return; } @@ -97,7 +103,7 @@ module.exports = { option.setName("game") .setDescription("Select the maptest game") .setRequired(true) - .addChoices({name: "bhop", value: "bhop"}, {name: "surf", value: "surf"}, {name: "deathrun", value: "deathrun"})) + .addChoices(commandChoices)) .addIntegerOption(option => option.setName("asset_id") .setDescription("The asset ID of the model") diff --git a/commands/take.js b/commands/take.js index 98795ef..a730c86 100644 --- a/commands/take.js +++ b/commands/take.js @@ -1,14 +1,20 @@ const { SlashCommandBuilder } = require('@discordjs/builders'); const noblox = require("noblox.js"); const { bhopCookie, surfCookie, deathrunCookie } = require("../config/config.json"); +const cookieDict = { + bhop: bhopCookie, + surf: surfCookie, + deathrun: deathrunCookie, +}; +var commandChoices = []; +for (const [game, file] of Object.entries(gameDict)) { + commandChoices.push({name: game, value: game}) +} 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 { + let cookie = cookieDict[game]; + if (cookie === undefined) { await interaction.reply({content: "Invalid game specified!", ephemeral: true}); return; } @@ -59,7 +65,7 @@ module.exports = { option.setName("game") .setDescription("Select the maptest game") .setRequired(true) - .addChoices({name: "bhop", value: "bhop"}, {name: "surf", value: "surf"}, {name: "deathrun", value: "deathrun"})) + .addChoices(commandChoices)) .addIntegerOption(option => option.setName("asset_id") .setDescription("The asset ID of the model")