diff --git a/index.d.ts b/index.d.ts index 22081e9..37d0952 100644 --- a/index.d.ts +++ b/index.d.ts @@ -62,6 +62,12 @@ declare namespace WAWebJS { /** Get all current contact instances */ getContacts(): Promise + + /** Get the country code of a WhatsApp ID. (154185968@c.us) => (1) */ + getCountryCode(number: string): Promise + + /** Get the formatted number of a WhatsApp ID. (12345678901@c.us) => (+1 (234) 5678-901) */ + getFormattedNumber(number: string): Promise /** Get all current Labels */ getLabels(): Promise @@ -786,9 +792,16 @@ declare namespace WAWebJS { * Will return null when getting chat for currently logged in user. */ getChat: () => Promise, - + + /** Returns the contact's countrycode, (1541859685@c.us) => (1) */ + getCountryCode(): Promise, + + /** Returns the contact's formatted phone number, (12345678901@c.us) => (+1 (234) 5678-901) */ + getFormattedNumber(): Promise, + /** Blocks this contact from WhatsApp */ block: () => Promise, + /** Unlocks this contact from WhatsApp */ unblock: () => Promise, diff --git a/src/Client.js b/src/Client.js index afb8b2f..2720c1c 100644 --- a/src/Client.js +++ b/src/Client.js @@ -840,10 +840,7 @@ class Client extends EventEmitter { * @returns {Promise} */ async getNumberId(number) { - if(!number.endsWith('@c.us')) { - number += '@c.us'; - } - + if (!number.endsWith('@c.us')) number += '@c.us'; try { return await this.pupPage.evaluate(async numberId => { return window.WWebJS.getNumberId(numberId); @@ -853,6 +850,32 @@ class Client extends EventEmitter { } } + /** + * Get the formatted number of a WhatsApp ID. + * @param {string} number Number or ID + * @returns {Promise} + */ + async getFormattedNumber(number) { + if(!number.endsWith('@s.whatsapp.net')) number = number.replace('c.us', 's.whatsapp.net'); + + return await this.pupPage.evaluate(async numberId => { + return window.NumberInfo.formattedPhoneNumber(numberId); + }, number); + } + + /** + * Get the country code of a WhatsApp ID. + * @param {string} number Number or ID + * @returns {Promise} + */ + async getCountryCode(number) { + number = number.replace(' ', '').replace('+', '').replace('@c.us', ''); + + return await this.pupPage.evaluate(async numberId => { + return window.NumberInfo.findCC(numberId); + }, number); + } + /** * Create a new group * @param {string} name group title diff --git a/src/structures/Contact.js b/src/structures/Contact.js index 09d721c..8444840 100644 --- a/src/structures/Contact.js +++ b/src/structures/Contact.js @@ -108,7 +108,7 @@ class Contact extends Base { * @type {boolean} */ this.isBlocked = data.isBlocked; - + return super._patch(data); } @@ -120,6 +120,22 @@ class Contact extends Base { return await this.client.getProfilePicUrl(this.id._serialized); } + /** + * Returns the contact's formatted phone number, (12345678901@c.us) => (+1 (234) 5678-901) + * @returns {Promise} + */ + async getFormattedNumber() { + return await this.client.getFormattedNumber(this.id._serialized); + } + + /** + * Returns the contact's countrycode, (1541859685@c.us) => (1) + * @returns {Promise} + */ + async getCountryCode() { + return await this.client.getCountryCode(this.id._serialized); + } + /** * Returns the Chat that corresponds to this Contact. * Will return null when getting chat for currently logged in user. @@ -178,4 +194,4 @@ class Contact extends Base { } -module.exports = Contact; \ No newline at end of file +module.exports = Contact; diff --git a/src/util/Injected.js b/src/util/Injected.js index b4328dd..5e1ae7c 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -21,6 +21,7 @@ exports.ExposeStore = (moduleRaidStr) => { window.Store.MediaPrep = window.mR.findModule('MediaPrep')[0]; window.Store.MediaObject = window.mR.findModule('getOrCreateMediaObject')[0]; window.Store.MediaUpload = window.mR.findModule('uploadMedia')[0]; + window.Store.NumberInfo = window.mR.findModule('formatNumber')[0]; window.Store.Cmd = window.mR.findModule('Cmd')[0].default; window.Store.MediaTypes = window.mR.findModule('msgToMediaType')[0]; window.Store.VCard = window.mR.findModule('vcardFromContactModel')[0];