107 lines
6.6 KiB
JavaScript
107 lines
6.6 KiB
JavaScript
module.exports = (interaction, client, bvs) => {
|
|
|
|
const member = interaction.guild.members.cache.get(interaction.user.id);
|
|
|
|
if (!member) {
|
|
interaction.reply({ content: `Could not find member with ID ${user.id}.`, ephemeral: true });
|
|
return;
|
|
}
|
|
|
|
if (!member.premiumSince) {
|
|
return interaction.reply({ content: `You must be boosting the server to use this command.`, ephemeral: true });
|
|
}
|
|
|
|
const choice = interaction.options.getNumber('choice');
|
|
if (!choice) {
|
|
return interaction.reply({ content: `You must provide a choice from the latest run of \`/searchnumbers\`. Use \`/searchnumbers\` to see available DIDs and their corresponding choices.`, ephemeral: true });
|
|
}
|
|
|
|
if (global.purchaseConfirmations && global.purchaseConfirmations[interaction.user.id]) {
|
|
if (choice === 1) {
|
|
// proceed with purchase
|
|
const didToPurchase = global.purchaseConfirmations[interaction.user.id];
|
|
delete global.purchaseConfirmations[interaction.user.id];
|
|
delete global.tempPurchasableDIDs[interaction.user.id];
|
|
interaction.deferReply({ ephemeral: true }).then(() => {
|
|
// bvs.purchaseDID(didToPurchase, interaction.user.id).then(() => {
|
|
// interaction.editReply({ content: `Successfully purchased DID \`${bvs.formatPhoneNumber(didToPurchase)}\`. It may take a few minutes for the purchase to fully process. Please allow up to 5 hours for the number to become active. If you are having issues, please contact support!`, ephemeral: true });
|
|
// }).catch(error => {
|
|
// console.error("Error purchasing DID:", error);
|
|
// interaction.editReply({ content: `There was an error purchasing the DID. Please try again later.`, ephemeral: true });
|
|
// });
|
|
|
|
// DB Stuff for FreePBX
|
|
// check 'discord_users' table to get the users extension
|
|
global.db.query("SELECT extension FROM discord_users WHERE discordId = ?", [interaction.user.id]).then(results => {
|
|
if (results.length === 0) {
|
|
return interaction.editReply({ content: `You somehow got this far and don't actually have an extension. Go make one, then try again.`, ephemeral: true });
|
|
} else {
|
|
// Buy the thing
|
|
bvs.purchaseDID(didToPurchase, interaction.user.id).then(() => {
|
|
console.log(`Successfully purchased DID ${didToPurchase} for user ${interaction.user.id}`);
|
|
const extension = results[0].extension;
|
|
// insert into incoming, set extension = "+{did}", destination = from-did-direct,{extension},1, description = "NitroDID For {displayName} ({discordId})"
|
|
global.db.query("INSERT INTO incoming (extension, destination, description, pricid, fanswer, cidnum, pmmaxretries, ringing, pmminlength, alertinfo, delay_answer, grppre, privacyman, reversal) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", [`+1${didToPurchase}`, `from-did-direct,${extension},1`, `NitroDID For ${interaction.user.displayName} (${interaction.user.id})`, '', '', '', '3', '', '10', '', 0, '', 0, '']).then(() => {
|
|
interaction.editReply({ content: `Successfully purchased DID \`${bvs.formatPhoneNumber(didToPurchase)}\`. It may take a few minutes for the purchase to fully process. Please allow up to 5 hours for the number to become active. If you are having issues, please contact support!`, ephemeral: true });
|
|
// Run `fwconsole reload` on host
|
|
const { exec } = require("child_process");
|
|
exec("fwconsole reload", (error, stdout, stderr) => {
|
|
if (error) {
|
|
console.error("Error reloading FreePBX:", error);
|
|
return;
|
|
}
|
|
if (stderr) {
|
|
console.error("Error output from reloading FreePBX:", stderr);
|
|
return;
|
|
}
|
|
console.log("Successfully reloaded FreePBX:", stdout);
|
|
});
|
|
}).catch(error => {
|
|
console.error("Error inserting into database:", error);
|
|
return interaction.editReply({ content: `There was an error setting up your DID in the database. Please contact support!`, ephemeral: true });
|
|
});
|
|
}).catch(error => {
|
|
console.error("Error purchasing DID:", error);
|
|
return interaction.editReply({ content: `There was an error purchasing the DID. Please try again later.`, ephemeral: true });
|
|
});
|
|
}
|
|
}).catch(error => {
|
|
console.error("Database error:", error);
|
|
return interaction.editReply({ content: `There was a database error. Please try again later.`, ephemeral: true });
|
|
});
|
|
});
|
|
// todo: impl actually buying did
|
|
} else {
|
|
delete global.purchaseConfirmations[interaction.user.id];
|
|
return interaction.reply({ content: `Cancelled. You can either choose a different DID from the latest run of \`/searchnumbers\` or start a new search by running \`/searchnumbers\` again.`, ephemeral: true });
|
|
}
|
|
} else {
|
|
|
|
if (!global.tempPurchasableDIDs || !global.tempPurchasableDIDs[interaction.user.id]) {
|
|
return interaction.reply({ content: `You must provide a choice from the latest run of \`/searchnumbers\`. Use \`/searchnumbers\` to see available DIDs and their corresponding choices.`, ephemeral: true });
|
|
}
|
|
|
|
// Check that the member doesn't already have a number.
|
|
bvs.getPremiumDIDs().then(dids => {
|
|
const userDIDs = dids.filter(did => did.userId === interaction.user.id);
|
|
if (userDIDs.length === 0) {
|
|
const dids = global.tempPurchasableDIDs[interaction.user.id];
|
|
const index = parseInt(choice) - 1;
|
|
if (isNaN(index) || index < 0 || index >= dids.length) {
|
|
return interaction.reply({ content: `Invalid choice. You must provide a choice from the latest run of \`/searchnumbers\`. Use \`/searchnumbers\` to see available DIDs and their corresponding choices.`, ephemeral: true });
|
|
}
|
|
|
|
const didToPurchase = dids[index];
|
|
// Store the choice in a global temp variable with the user id so the user can confirm their purchase by running the command again with the same choice. This is a bit janky but it works for now.
|
|
global.purchaseConfirmations = global.purchaseConfirmations || {};
|
|
global.purchaseConfirmations[interaction.user.id] = didToPurchase;
|
|
return interaction.reply({ content: `You have selected DID \`${bvs.formatPhoneNumber(didToPurchase)}\` for purchase. If you want to proceed with purchasing this DID, run \`/getnumber\` with choice \`1\`. If you want to cancel, run the command again with a different choice or run \`/searchnumbers\` again to start a new search.`, ephemeral: true });
|
|
} else {
|
|
interaction.reply({ content: `Your DID is \`${bvs.formatPhoneNumber(userDIDs[0].did)}\``, ephemeral: true });
|
|
}
|
|
}).catch(error => {
|
|
console.error("Error fetching premium DIDs:", error);
|
|
interaction.reply({ content: `There was an error fetching your DID. Please try again later.`, ephemeral: true });
|
|
});
|
|
}
|
|
} |