From 8030612e522e56a84f7474420f7219d792075d6f Mon Sep 17 00:00:00 2001 From: Johnson Chen Date: Thu, 4 Jun 2020 21:41:20 +1000 Subject: [PATCH] feat(Guild): add fetchVanityData (#4103) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: deprecate Guild.fetchVanityCode() * feat: add Guild.fetchVanityData() * chore: update typings * fix: remove redundant .then() Co-Authored-By: Antonio Román * chore: fix lint * chore: util.deprecate fetchVanityCode * feat: add VanityData typedef and populate vanityURLUses * chore: update typings * chore: properly deprecate fetchVanityCode * chore: fix jsdoc description for fetchVanityData * feat: make fetchVanityData an async function * chore: update Vanity typedef * docs: update jsdoc * feat: throw vanity url error instead of returning rejected promise Co-Authored-By: Vlad Frangu * docs: disable max-len rule and add info about receiving parameter * fix: throw Error instead of rejecting Promise * revert: revert "fix: throw Error instead of rejecting Promise" This reverts commit 7ffd53eba40ff7261a36372935c3017576518a56. * fix: require DJSError to fix throwing VANITY_URL error * nitpick: re-add TypeError to the import Co-authored-by: Vlad Frangu Co-authored-by: Antonio Román Co-authored-by: Vlad Frangu --- src/structures/Guild.js | 46 +++++++++++++++++++++++++++++++++++++++++ typings/index.d.ts | 2 ++ 2 files changed, 48 insertions(+) diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 05b211479..dea3781c3 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -1,5 +1,6 @@ 'use strict'; +const { deprecate } = require('util'); const Base = require('./Base'); const GuildAuditLogs = require('./GuildAuditLogs'); const GuildPreview = require('./GuildPreview'); @@ -7,6 +8,7 @@ const Integration = require('./Integration'); const Invite = require('./Invite'); const VoiceRegion = require('./VoiceRegion'); const Webhook = require('./Webhook'); +const { Error, TypeError } = require('../errors'); const GuildChannelManager = require('../managers/GuildChannelManager'); const GuildEmojiManager = require('../managers/GuildEmojiManager'); const GuildMemberManager = require('../managers/GuildMemberManager'); @@ -331,6 +333,15 @@ class Guild extends Base { */ this.vanityURLCode = data.vanity_url_code; + /* eslint-disable max-len */ + /** + * The use count of the vanity URL code of the guild, if any + * You will need to fetch the guild using {@link Guild#fetchVanityCode} if you want to receive this parameter + * @type {?number} + */ + this.vanityURLUses = null; + /* eslint-enable max-len */ + /** * The description of the guild, if any * @type {?string} @@ -750,6 +761,7 @@ class Guild extends Base { * Fetches the vanity url invite code to this guild. * Resolves with a string matching the vanity url invite code, not the full url. * @returns {Promise} + * @deprecated * @example * // Fetch invites * guild.fetchVanityCode() @@ -768,6 +780,35 @@ class Guild extends Base { .then(res => res.code); } + /** + * An object containing information about a guild's vanity invite. + * @typedef {Object} Vanity + * @property {?string} code Vanity invite code + * @property {?number} uses How many times this invite has been used + */ + + /** + * Fetches the vanity url invite object to this guild. + * Resolves with an object containing the vanity url invite code and the use count + * @returns {Promise} + * @example + * // Fetch invite data + * guild.fetchVanityData() + * .then(res => { + * console.log(`Vanity URL: https://discord.gg/${res.code} with ${res.uses} uses`); + * }) + * .catch(console.error); + */ + async fetchVanityData() { + if (!this.features.includes('VANITY_URL')) { + throw new Error('VANITY_URL'); + } + const data = await this.client.api.guilds(this.id, 'vanity-url').get(); + this.vanityURLUses = data.uses; + + return data; + } + /** * Fetches all webhooks for the guild. * @returns {Promise>} @@ -1369,4 +1410,9 @@ class Guild extends Base { } } +Guild.prototype.fetchVanityCode = deprecate( + Guild.prototype.fetchVanityCode, + 'Guild#fetchVanityCode: Use fetchVanityData() instead', +); + module.exports = Guild; diff --git a/typings/index.d.ts b/typings/index.d.ts index f9b0552a7..44b10530a 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -638,6 +638,7 @@ declare module 'discord.js' { public systemChannelFlags: Readonly; public systemChannelID: Snowflake | null; public vanityURLCode: string | null; + public vanityURLUses: number | null; public verificationLevel: VerificationLevel; public readonly verified: boolean; public readonly voice: VoiceState | null; @@ -660,6 +661,7 @@ declare module 'discord.js' { public fetchInvites(): Promise>; public fetchPreview(): Promise; public fetchVanityCode(): Promise; + public fetchVanityData(): Promise<{ code: string; uses: number }>; public fetchVoiceRegions(): Promise>; public fetchWebhooks(): Promise>; public iconURL(options?: ImageURLOptions & { dynamic?: boolean }): string | null;