discord-freepbx-manager/interactionHandlers/commands/manage.js

142 lines
6.8 KiB
JavaScript

const pool = global.pool
const fpbx = global.fpbx
const client = global.client
const log = global.log
module.exports = {};
module.exports.execute = async (interaction) => {
const subcommand = interaction.options.getSubcommand();
switch (subcommand) {
case 'create': // Create an extension for a user
await interaction.deferReply({ ephemeral: true });
var forUser = interaction.options.getUser('user');
var newExt = interaction.options.getInteger('extension') ? interaction.options.getInteger('extension') : await fpbx.getNextAvailableExtension();
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;
}
await interaction.editReply({ content: `Creating extension ${newExt} for ${forUser.username}`, ephemeral: true });
fpbx.addExtension(newExt, forUser.username).then(async (res) => {
if (res.addExtension.status != true) {
await interaction.editReply({ content: `Something went wrong :(`, ephemeral: true });
return;
}
await pool.query('INSERT INTO discord_users (discordId, extension) VALUES (?, ?)', [forUser.id, newExt]);
await interaction.editReply({ content: `Extension ${newExt} created! Getting info..`, ephemeral: true });
await fpbx.reload();
const extInfo = await fpbx.getExtension(newExt);
await interaction.editReply({ embeds: [{
title: "Extension Info",
description: `**PBX Address:** \`${process.env.PBX_HOSTNAME}\`\n**Extension:** \`${extInfo.fetchExtension.user.extension}\`\n**Name:** \`${extInfo.fetchExtension.user.name}\`\n**Password:** ||\`${extInfo.fetchExtension.user.extPassword}\`||`,
color: 0x00ff00
}], ephemeral: true })
}).catch(async (error) => {
log.error(error);
await interaction.editReply({ content: 'There was an error while creating the extension!', ephemeral: true });
});
break;
case "delete": // Delete an extension for a user
await interaction.deferReply({ ephemeral: true });
var forUser = interaction.options.getUser('user');
var [lookup] = await pool.query('SELECT * FROM discord_users WHERE discordId = ?', [forUser.id]);
if (!lookup) {
await interaction.editReply({ content: `User does not have an extension!`, ephemeral: true });
return;
}
await interaction.editReply({ content: `Deleting extension ${lookup.extension} for ${forUser.username}`, ephemeral: true });
fpbx.deleteExtension(lookup.extension).then(async (res) => {
console.log(res)
if (res[0].deleteExtension.status != true) {
await interaction.editReply({ content: `Something went wrong :(`, ephemeral: true });
return;
}
await pool.query('DELETE FROM discord_users WHERE discordId = ?', [forUser.id]);
await fpbx.reload();
await interaction.editReply({ content: `Extension ${lookup.extension} deleted!`, ephemeral: true });
}).catch(async (error) => {
log.error(error);
await interaction.editReply({ content: 'There was an error while deleting the extension!', ephemeral: true });
});
break;
case "lookup": // Get user extension info
var forUser = interaction.options.getUser('user');
var [lookup] = await pool.query('SELECT * FROM discord_users WHERE discordId = ?', [forUser.id]);
if (!lookup) {
await interaction.reply({ content: `User does not have an extension!`, ephemeral: true });
return;
}
const extInfo = await fpbx.getExtension(lookup.extension);
return await interaction.reply({
ephemeral: true, embeds: [
{
title: "Extension Info",
description: `**PBX Address:** \`${process.env.PBX_HOSTNAME}\`\n**Extension:** \`${extInfo.fetchExtension.user.extension}\`\n**Name:** \`${extInfo.fetchExtension.user.name}\`\n**Password:** ||\`${extInfo.fetchExtension.user.extPassword}\`||`,
color: 0x00ff00
}
]
});
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;
case "list-orphans": // List all orphaned extensions (extensions without a Discord user linked)
await interaction.deferReply({ ephemeral: true });
const orphans = await pool.query('SELECT id,description FROM asterisk.devices WHERE id NOT IN (SELECT extension FROM asterisk.discord_users)');
if (orphans.length === 0) {
await interaction.editReply({ content: 'No orphaned extensions found!', ephemeral: true });
return;
}
orphans.sort((a, b) => Number(a.id) - Number(b.id));
const orphanList = orphans.map(o => `**Extension:** \`${o.id}\` - **Name:** \`${o.description}\``).join('\n');
await interaction.editReply({
content: `**Orphaned Extensions:**\n${orphanList}`,
ephemeral: true
});
break;
default:
await interaction.reply({ content: 'Unknown subcommand!', ephemeral: true });
break;
}
}