From bfa33ddcc22061c430e85d84775c38cddbbf7c97 Mon Sep 17 00:00:00 2001
From: Quaternions <krakow20@gmail.com>
Date: Wed, 8 Mar 2023 12:37:40 -0800
Subject: [PATCH] use centralized dictionary instead of hardcoded if-else for
 each game

---
 commands/submissions.js | 12 +++++-------
 commands/submit.js      | 12 +++++-------
 commands/take.js        | 13 +++++--------
 config/.gitignore       |  1 +
 config/config.js        | 16 ++++++++++++++++
 5 files changed, 32 insertions(+), 22 deletions(-)
 create mode 100644 config/config.js

diff --git a/commands/submissions.js b/commands/submissions.js
index 567e47f..a03b3c9 100644
--- a/commands/submissions.js
+++ b/commands/submissions.js
@@ -1,15 +1,13 @@
 const { SlashCommandBuilder } = require('@discordjs/builders');
 const { MessageAttachment } = require("discord.js");
 const fs = require('node:fs');
+const { submissions, commands } = require("../config/config.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 {
+    const fname = submissions[game];
+    if (fname === undefined) {
         await interaction.reply({content: "Invalid game specified!", ephemeral: true});
         return;
     }
@@ -33,7 +31,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(commands))
         ,
 	execute
-};
\ No newline at end of file
+};
diff --git a/commands/submit.js b/commands/submit.js
index aecfd87..165f227 100644
--- a/commands/submit.js
+++ b/commands/submit.js
@@ -3,6 +3,7 @@ const { parse } = require("csv-parse/sync");
 const fs = require('node:fs');
 const noblox = require("noblox.js");
 const axios = require("axios").default;
+const { submissions, commands } = require("../config/config.js")
 
 async function robloxUserFromDiscord(id) {
     if (isNaN(id)) return undefined;
@@ -33,11 +34,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 {
+    const fname = submissions[game];
+    if (fname === undefined) {
         await interaction.reply({content: "Invalid game specified!", ephemeral: true});
         return;
     }
@@ -97,7 +95,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(commands))
         .addIntegerOption(option =>
             option.setName("asset_id")
             .setDescription("The asset ID of the model")
@@ -137,4 +135,4 @@ function getProductInfo (asset) {
             return reject(error);
         }
     })
-}
\ No newline at end of file
+}
diff --git a/commands/take.js b/commands/take.js
index 98795ef..430d9a4 100644
--- a/commands/take.js
+++ b/commands/take.js
@@ -1,14 +1,11 @@
 const { SlashCommandBuilder } = require('@discordjs/builders');
 const noblox = require("noblox.js");
-const { bhopCookie, surfCookie, deathrunCookie } = require("../config/config.json");
+const { cookies, commands } = require("../config/config.js")
 
 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 {
+    const cookie = cookies[game];
+    if (cookie === undefined) {
         await interaction.reply({content: "Invalid game specified!", ephemeral: true});
         return;
     }
@@ -59,7 +56,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(commands))
         .addIntegerOption(option =>
             option.setName("asset_id")
             .setDescription("The asset ID of the model")
@@ -151,4 +148,4 @@ function getProductInfo (asset) {
             return reject(error);
         }
     })
-}
\ No newline at end of file
+}
diff --git a/config/.gitignore b/config/.gitignore
index bdf2e35..1b02a15 100644
--- a/config/.gitignore
+++ b/config/.gitignore
@@ -1,3 +1,4 @@
 *
+!config.js
 !example_config.json
 !.gitignore
\ No newline at end of file
diff --git a/config/config.js b/config/config.js
new file mode 100644
index 0000000..0ab159e
--- /dev/null
+++ b/config/config.js
@@ -0,0 +1,16 @@
+const { bhopCookie, surfCookie, deathrunCookie } = require("config.json");
+const cookies = {
+    bhop: bhopCookie,
+    surf: surfCookie,
+    deathrun: deathrunCookie,
+};
+const submissions = {
+    bhop: "files/bhop_submissions.csv",
+    surf: "files/surf_submissions.csv",
+    deathrun: "files/deathrun_submissions.csv",
+};
+const commands = [];
+for (const game of Object.keys(submissions)) {
+    commands.push({name: game, value: game})
+}
+export { submissions, cookies, commands }