bvs-nitro-did/interactions/chatCommand/searchnumbers.js
2026-06-14 12:20:16 -06:00

56 lines
3.1 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 });
}
interaction.deferReply({ ephemeral: true }).then(() => {
bvs.getPremiumDIDs().then(dids => {
const userDIDs = dids.filter(did => did.userId === interaction.user.id);
if (userDIDs.length === 0) {
const areaCode = interaction.options.getNumber('area_code');
const officeCode = interaction.options.getNumber('office_code');
// Clear purchase confirmation and temp purchasable DIDs for the user, since they're starting a new search. This prevents confusion where a user searches for DIDs, then tries to purchase one, but the purchasable DIDs from the previous search are still stored and they accidentally purchase a DID from the previous search results instead of the new ones.
if (global.purchaseConfirmations) {
delete global.purchaseConfirmations[interaction.user.id];
}
if (global.tempPurchasableDIDs) {
delete global.tempPurchasableDIDs[interaction.user.id];
}
// Validate input
if (!/^\d{3}$/.test(areaCode)) {
return interaction.editReply({ content: `Area code must be 3 digits.`, ephemeral: true });
}
if (officeCode && !/^\d{3}$/.test(officeCode)) {
return interaction.editReply({ content: `Office code must be 3 digits.`, ephemeral: true });
}
bvs.searchPurchasableDIDs(`${areaCode}${officeCode || ""}`).then(dids => {
if (dids.length === 0) {
return interaction.editReply({ content: `No results for search query. Try again.`, ephemeral: true });
}
// console.log(dids)
interaction.editReply({ 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.editReply({ content: `There was an error searching for DIDs. Please try again later.`, ephemeral: true });
});
} else {
interaction.editReply({ content: `Your DID is \`${bvs.formatPhoneNumber(userDIDs[0].did)}\``, ephemeral: true });
}
}).catch(error => {
console.error("Error fetching premium DIDs:", error);
interaction.editReply({ content: `There was an error fetching your DID. Please try again later.`, ephemeral: true });
});
})
}