bvs-nitro-did/interactions/chatCommand/searchnumbers.js

25 lines
1.5 KiB
JavaScript

module.exports = (interaction, client, bvs) => {
const areaCode = interaction.options.getNumber('area_code');
const officeCode = interaction.options.getNumber('office_code');
// Validate input
if (!/^\d{3}$/.test(areaCode)) {
return interaction.reply({ content: `Area code must be 3 digits.`, ephemeral: true });
}
if (officeCode && !/^\d{3}$/.test(officeCode)) {
return interaction.reply({ content: `Office code must be 3 digits.`, ephemeral: true });
}
bvs.searchPurchasableDIDs(areaCode + (officeCode || "")).then(dids => {
if (dids.length === 0) {
return interaction.reply({ content: `No results for search query. Try again.`, ephemeral: true });
}
console.log(dids)
interaction.reply({ content: `Found the following DIDs\n\`\`\`\n${dids.map((did, index) => `${index + 1}. ${bvs.formatPhoneNumber(did.TN)}`).join("\n")}\n\`\`\`\nUse \`/getnumber <choice>\` to purchase one of these DIDs! (Choice is one of the numbers listed above by index, not phone number)`, ephemeral: true });
// Store the DIDs in a global temp variable with the user id so the user can purchase by 1, 2, 3, etc. in the /getnumber command. This is a bit janky but it works for now.
global.tempPurchasableDIDs = global.tempPurchasableDIDs || {};
global.tempPurchasableDIDs[interaction.user.id] = dids.map(did => did.TN);
console.log(global.tempPurchasableDIDs)
}).catch(error => {
console.error("Error searching DIDs:", error);
interaction.reply({ content: `There was an error searching for DIDs. Please try again later.`, ephemeral: true });
});
}