diff --git a/commands.json b/commands.json deleted file mode 100644 index 69f2df3..0000000 --- a/commands.json +++ /dev/null @@ -1,55 +0,0 @@ -[ - { - "name": "whoami", - "description": "Get your extension info if you have one", - "type": 1 - }, - { - "name": "new", - "description": "Get an extension on the LiteNet Phone System", - "type": 1 - }, - { - "name": "delete", - "description": "Remove your extension from the LiteNet Phone System", - "type": 1, - "options": [ - { - "name": "confirm", - "description": "Confirm that you want to delete your extension. THIS CANNOT BE UNDONE!", - "type": 5, - "required": true, - "choices": [ - { - "name": "yes", - "value": "yes" - } - ] - } - ] - }, - { - "name": "list", - "description": "List all extensions on the LiteNet Phone System", - "type": 1 - }, - { - "name": "button", - "description": "Send the get an extension button!", - "type": 1, - "default_member_permissions": 0 - }, - { - "name": "name", - "description": "Change your extension's name (Defaults to your Discord name)", - "type": 1, - "options": [ - { - "name": "name", - "description": "The new name for your extension", - "type": 3, - "required": false - } - ] - } -] diff --git a/index.js b/index.js index ca37193..054f3ca 100644 --- a/index.js +++ b/index.js @@ -267,6 +267,10 @@ const findNextExtension = () => { }); } +// Set up mariadb connection +const mariadb = require('mariadb'); +const pool = mariadb.createPool(config.mariadb); + // Load Discord.js const Discord = require("discord.js"); const { @@ -304,24 +308,108 @@ dcClient.on('ready', async () => { }; sendLog(`${colors.cyan("[INFO]")} Logged in as ${dcClient.user.displayName}!`); - - - // Set up application commands - const commands = require('./commands.json'); - - (async () => { - try { - sendLog(`${colors.cyan("[INFO]")} Started refreshing application (/) commands.`); - await rest.put( - Routes.applicationGuildCommands(dcClient.user.id, config.discord.guildId), { - body: commands - } - ); - sendLog(`${colors.cyan("[INFO]")} Successfully reloaded application (/) commands.`); - } catch (error) { - console.error(`${colors.red("[ERROR]")} ${error}`); + + const pageGroups = require('./pageGroups.json'); + var commands = [ + { + "name": "whoami", + "description": "Get your extension info if you have one", + "type": 1 + }, + { + "name": "new", + "description": "Get an extension on the LiteNet Phone System", + "type": 1 + }, + { + "name": "delete", + "description": "Remove your extension from the LiteNet Phone System", + "type": 1, + "options": [ + { + "name": "confirm", + "description": "Confirm that you want to delete your extension. THIS CANNOT BE UNDONE!", + "type": 5, + "required": true, + "choices": [ + { + "name": "yes", + "value": "yes" + } + ] + } + ] + }, + { + "name": "list", + "description": "List all extensions on the LiteNet Phone System", + "type": 1 + }, + { + "name": "button", + "description": "Send the get an extension button!", + "type": 1, + "default_member_permissions": 0 + }, + { + "name": "name", + "description": "Change your extension's name (Defaults to your Discord name)", + "type": 1, + "options": [ + { + "name": "name", + "description": "The new name for your extension", + "type": 3, + "required": false + } + ] + }, + { + "name": "paging", + "description": "Add/Remove yourself from paging groups", + "type": 1, + "options": [ + { + "name": "method", + "description": "The method to use", + "type": 3, + "required": true, + "choices": [ + { + "name": "add", + "value": "add" + }, + { + "name": "remove", + "value": "remove" + } + ] + }, + { + "name": "group", + "description": "The group to add/remove yourself from", + "type": 3, + "required": true, + "choices": pageGroups + } + ] } - })(); + ]; + + + (async () => { + try { + sendLog(`${colors.cyan("[INFO]")} Started refreshing application (/) commands.`); + await rest.put( + Routes.applicationGuildCommands(dcClient.user.id, config.discord.guildId), { + body: commands + } + ); + sendLog(`${colors.cyan("[INFO]")} Successfully reloaded application (/) commands.`); + } catch (error) { + console.error(`${colors.red("[ERROR]")} ${error}`); + } + })(); // Presence Stuff getExtCount().then((result) => { @@ -740,6 +828,84 @@ dcClient.on('interactionCreate', async interaction => { ephemeral: true }); }); + break; + case "paging": // Add/Remove yourself from paging groups + var conn = await pool.getConnection(); + await interaction.deferReply({ + ephemeral: true + }); + // Get the users extension, if they don't have one, return an ephemeral message saying so + lookupExtension(interaction.user.id, "uid").then((result) => { + if (result.status == "exists") { + // The user has an extension, add/remove them from the paging group + let ext = result.result.fetchExtension.user.extension; + let group = interaction.options.get("group").value; + let method = interaction.options.get("method").value; + switch (method) { + case "add": + // Check the db if they're already in the group + conn.query(`SELECT * FROM paging_groups WHERE ext = ${ext} AND \`page_number\` = ${group}`).then((result) => { + if (result.length == 0) { + // They're not in the group, add them + conn.query(`INSERT INTO paging_groups (\`ext\`, \`page_number\`) VALUES (${ext}, ${group})`).then((result) => { + interaction.editReply({ + content: "Added you to the paging group!", + ephemeral: true + }); + sendLog(`${colors.green("[INFO]")} ${interaction.user.displayName} (${interaction.user.id}) added themselves to paging group ${group}`) + }).catch((error) => { + interaction.editReply(`Error adding you to the paging group: ${error}`); + sendLog(`${colors.red("[ERROR]")} ${error}`); + }); + } else { + // They're already in the group, return an ephemeral message saying so + interaction.editReply({ + content: "You're already in that paging group!", + ephemeral: true + }); + } + }).catch((error) => { + interaction.editReply(`Error adding you to the paging group: ${error}`); + sendLog(`${colors.red("[ERROR]")} ${error}`); + }); + break; + case "remove": + // Check if they're in the group + conn.query(`SELECT * FROM paging_groups WHERE ext = ${ext} AND \`page_number\` = ${group}`).then((result) => { + if (result.length == 0) { + // They're not in the group, return an ephemeral message saying so + interaction.editReply({ + content: "You're not in that paging group!", + ephemeral: true + }); + } else { + // They're in the group, remove them + conn.query(`DELETE FROM paging_groups WHERE ext = ${ext} AND \`page_number\` = ${group}`).then((result) => { + interaction.editReply({ + content: "Removed you from the paging group!", + ephemeral: true + }); + sendLog(`${colors.green("[INFO]")} ${interaction.user.displayName} (${interaction.user.id}) removed themselves from paging group ${group}`) + }).catch((error) => { + interaction.editReply(`Error removing you from the paging group: ${error}`); + sendLog(`${colors.red("[ERROR]")} ${error}`); + }); + } + }).catch((error) => { + interaction.editReply(`Error removing you from the paging group: ${error}`); + sendLog(`${colors.red("[ERROR]")} ${error}`); + }); + break; + } + } + }).catch((error) => { + // The user doesn't have an extension, return an ephemeral message saying so, and how to get one (/new) + interaction.editReply({ + content: "You don't have an extension! Run `/new` to get one!", + ephemeral: true + }); + }) + break; default: break; diff --git a/package-lock.json b/package-lock.json index 07cec84..f43f12a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "colors": "^1.4.0", "discord.js": "^14.7.1", "freepbx-graphql-client": "^0.1.1", + "mariadb": "^3.2.0", "sqlite3": "^5.1.4" } }, @@ -186,6 +187,11 @@ "node": ">= 6" } }, + "node_modules/@types/geojson": { + "version": "7946.0.10", + "resolved": "https://registry.npmjs.org/@types/geojson/-/geojson-7946.0.10.tgz", + "integrity": "sha512-Nmh0K3iWQJzniTuPRcJn5hxXkfB1T1pgB89SBig5PlJQU5yocazeu4jATJlaA0GYFKWMqDdvYemoSnF2pXgLVA==" + }, "node_modules/@types/node": { "version": "20.5.7", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.5.7.tgz", @@ -424,6 +430,14 @@ "resolved": "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz", "integrity": "sha512-bd2L678uiWATM6m5Z1VzNCErI3jiGzt6HGY8OVICs40JQq/HALfbyNJmp0UDakEY4pMMaN0Ly5om/B1VI/+xfQ==" }, + "node_modules/denque": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz", + "integrity": "sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==", + "engines": { + "node": ">=0.10" + } + }, "node_modules/detect-libc": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.2.tgz", @@ -659,7 +673,6 @@ "version": "0.6.3", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", - "optional": true, "dependencies": { "safer-buffer": ">= 2.1.2 < 3.0.0" }, @@ -806,6 +819,34 @@ "node": ">= 10" } }, + "node_modules/mariadb": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/mariadb/-/mariadb-3.2.0.tgz", + "integrity": "sha512-IH2nidQat1IBMxP5gjuNxG6dADtz1PESEC6rKrcATen5v3ngFyZITjehyYiwNfz3zUNQupfYmVntz93M+Pz8pQ==", + "dependencies": { + "@types/geojson": "^7946.0.10", + "@types/node": "^17.0.45", + "denque": "^2.1.0", + "iconv-lite": "^0.6.3", + "lru-cache": "^7.14.0" + }, + "engines": { + "node": ">= 12" + } + }, + "node_modules/mariadb/node_modules/@types/node": { + "version": "17.0.45", + "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.45.tgz", + "integrity": "sha512-w+tIMs3rq2afQdsPJlODhoUEKzFP1ayaoyl1CcnwtIlsVe7K7bA1NGm4s3PraqTLlXnbIN84zuBlxBWo1u9BLw==" + }, + "node_modules/mariadb/node_modules/lru-cache": { + "version": "7.18.3", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-7.18.3.tgz", + "integrity": "sha512-jumlc0BIUrS3qJGgIkWZsyfAM7NCWiBcCDhnd+3NNM5KbBmLTgHVfWBcg6W+rLUsIpzpERPsvwUP7CckAQSOoA==", + "engines": { + "node": ">=12" + } + }, "node_modules/mime-db": { "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", @@ -1185,8 +1226,7 @@ "node_modules/safer-buffer": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", - "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", - "optional": true + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, "node_modules/semver": { "version": "7.5.4", diff --git a/package.json b/package.json index d2b71ce..ace8544 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "colors": "^1.4.0", "discord.js": "^14.7.1", "freepbx-graphql-client": "^0.1.1", + "mariadb": "^3.2.0", "sqlite3": "^5.1.4" } } diff --git a/pageGroups.json b/pageGroups.json new file mode 100644 index 0000000..4983421 --- /dev/null +++ b/pageGroups.json @@ -0,0 +1,6 @@ +[ + { + "name": "General Paging (700)", + "value": "700" + } +] \ No newline at end of file