From c6201ee41b46b5d50f16f9750a1fd1d725141b02 Mon Sep 17 00:00:00 2001 From: SpaceEEC Date: Thu, 4 Oct 2018 00:31:20 +0200 Subject: [PATCH] backport(Guild): add fetchVanityCode (#2871) --- src/client/rest/RESTMethods.js | 5 +++++ src/structures/Guild.js | 20 ++++++++++++++++++++ src/util/Constants.js | 1 + typings/index.d.ts | 1 + 4 files changed, 27 insertions(+) diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index cbfa661a4..5e78d91a5 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -684,6 +684,11 @@ class RESTMethods { }); } + getGuildVanityCode(guild) { + return this.rest.makeRequest('get', Endpoints.Guild(guild).vanityURL, true) + .then(res => res.code); + } + pruneGuildMembers(guild, days, dry, reason) { return this.rest.makeRequest(dry ? 'get' : diff --git a/src/structures/Guild.js b/src/structures/Guild.js index d10482f75..93ebb8667 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -504,6 +504,26 @@ class Guild { return this.client.rest.methods.getGuildInvites(this); } + /** + * 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} + * @example + * // Fetch invites + * guild.fetchVanityCode() + * .then(code => { + * console.log(`Vanity URL: https://discord.gg/${code}`); + * }) + * .catch(console.error); + */ + fetchVanityCode() { + if (!this.features.includes('VANITY_URL')) { + return Promise.reject(new Error('This guild does not have the VANITY_URL feature enabled.')); + } + return this.client.rest.methods.getGuildVanityCode(this); + } + + /** * Fetch all webhooks for the guild. * @returns {Promise>} diff --git a/src/util/Constants.js b/src/util/Constants.js index 5d9a38594..50572fb19 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -144,6 +144,7 @@ const Endpoints = exports.Endpoints = { roles: `${base}/roles`, emojis: `${base}/emojis`, search: `${base}/messages/search`, + vanityURL: `${base}/vanity-url`, voiceRegions: `${base}/regions`, webhooks: `${base}/webhooks`, ack: `${base}/ack`, diff --git a/typings/index.d.ts b/typings/index.d.ts index de036372e..ac1dc5add 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -537,6 +537,7 @@ declare module 'discord.js' { public fetchInvites(): Promise>; public fetchMember(user: UserResolvable, cache?: boolean): Promise; public fetchMembers(query?: string, limit?: number): Promise; + public fetchVanityCode(): Promise; public fetchVoiceRegions(): Promise>; public fetchWebhooks(): Promise>; public leave(): Promise;