Add link/unlink commands for staff

This commit is contained in:
Christopher Cookman 2025-06-20 03:05:24 -06:00
parent a7d4a91936
commit 1253de3bb4
3 changed files with 79 additions and 0 deletions

View file

@ -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
}
]
}
]
}

View file

@ -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.");

View file

@ -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;
}
}