diff --git a/commands.js b/commands.js index e7e092f..15060e6 100644 --- a/commands.js +++ b/commands.js @@ -264,6 +264,38 @@ module.exports = [ "required": true } ] + }, + { + "name": "link", + "description": "Link an existing extension to a Discord user", + "type": 1, + "options": [ + { + "name": "user", + "description": "The Discord user to link the extension to", + "type": 6, + "required": true + }, + { + "name": "extension", + "description": "The extension number to link", + "type": 4, + "required": true + } + ] + }, + { + "name": "unlink", + "description": "Unlink an extension from a Discord user", + "type": 1, + "options": [ + { + "name": "extension", + "description": "The extension number to unlink", + "type": 4, + "required": true + } + ] } ] } diff --git a/index.js b/index.js index 28ede42..c378c20 100644 --- a/index.js +++ b/index.js @@ -180,6 +180,9 @@ if (fs.existsSync("./import.json")) { insertData(); } +const findOrphans = async () => { // Find extensions that exist but don't have a linked Discord user + pool.query("SELECT id FROM asterisk.devices WHERE id NOT IN (SELECT extension FROM asterisk.discord_users)") + // Startup require("./migrations")(pool).then(() => { log.success("Database migrations complete."); diff --git a/interactionHandlers/commands/manage.js b/interactionHandlers/commands/manage.js index e2e73aa..c200a2e 100644 --- a/interactionHandlers/commands/manage.js +++ b/interactionHandlers/commands/manage.js @@ -79,5 +79,49 @@ module.exports.execute = async (interaction) => { ] }); break; + case "link": // Link an extension to a user (or at least try to) + await interaction.deferReply({ ephemeral: true }); + var forUser = interaction.options.getUser('user'); + var ext = interaction.options.getInteger('extension'); + if (!ext) { + await interaction.editReply({ content: `You must provide an extension to link!`, ephemeral: true }); + return; + } + var [lookup] = await pool.query('SELECT * FROM discord_users WHERE discordId = ?', [forUser.id]); + if (lookup) { + await interaction.editReply({ content: `User already has an extension, it's ${lookup.extension}!`, ephemeral: true }); + return; + } + var [extLookup] = await pool.query('SELECT * FROM discord_users WHERE extension = ?', [ext]); + if (extLookup) { + await interaction.editReply({ content: `Extension ${ext} is already linked to another user! It's linked to ${extLookup.discordId}`, ephemeral: true }); + return; + } + var [extExists] = await pool.query('SELECT * FROM asterisk.devices WHERE id = ?', [ext]); + if (!extExists) { + await interaction.editReply({ content: `Extension ${ext} does not exist!`, ephemeral: true }); + return; + } + await pool.query('INSERT INTO discord_users (discordId, extension) VALUES (?, ?)', [forUser.id, ext]); + await interaction.editReply({ content: `Extension ${ext} linked to ${forUser.username}!`, ephemeral: true }); + break; + case "unlink": // Unlink an extension from a user + await interaction.deferReply({ ephemeral: true }); + var ext = interaction.options.getInteger('extension'); + if (!ext) { + await interaction.editReply({ content: `You must provide an extension to unlink!`, ephemeral: true }); + return; + } + var [lookup] = await pool.query('SELECT * FROM discord_users WHERE extension = ?', [ext]); + if (!lookup) { + await interaction.editReply({ content: `Extension ${ext} is not linked to any user!`, ephemeral: true }); + return; + } + await pool.query('DELETE FROM discord_users WHERE extension = ?', [ext]); + await interaction.editReply({ content: `Extension ${ext} unlinked from ${lookup.discordId}!`, ephemeral: true }); + break; + default: + await interaction.reply({ content: 'Unknown subcommand!', ephemeral: true }); + break; } } \ No newline at end of file