From f8057b01cb321d468c61b04227b9589fb4dcded1 Mon Sep 17 00:00:00 2001 From: Florian Date: Sat, 18 Aug 2018 13:57:58 +0200 Subject: [PATCH] feat: add guild.fetchVanityCode() (#2732) * Error for guild.fetchVanityURL feature not enabled * added GET/guilds/{guild.id}/vanity-url endpoint * fix: code conventions * adopted suggestion * Changed error message according to change request * Renamed method to indicate that only the code is fetched, not the entire url. * Update Guild.js --- src/errors/Messages.js | 2 ++ src/structures/Guild.js | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/errors/Messages.js b/src/errors/Messages.js index 86bd54cd1..f9ff8be6e 100644 --- a/src/errors/Messages.js +++ b/src/errors/Messages.js @@ -97,6 +97,8 @@ const Messages = { EMOJI_MANAGED: 'Emoji is managed and has no Author.', REACTION_RESOLVE_USER: 'Couldn\'t resolve the user ID to remove from the reaction.', + + VANITY_URL: 'This guild does not have the VANITY_URL feature enabled.', }; for (const [name, message] of Object.entries(Messages)) register(name, message); diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 423083e93..d80620364 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -434,6 +434,26 @@ 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} + * @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('VANITY_URL')); + } + return this.client.api.guilds(this.id, 'vanity-url').get() + .then(res => res.code); + } + /** * Fetches all webhooks for the guild. * @returns {Promise>}