From 4adf11b7b78e5ae680d117c4c42b5bb854c7d0ad Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Mon, 8 Mar 2021 17:41:46 +0100 Subject: [PATCH] feat(handlers/guild): add getWidget() & getWidgetImageUrl() (#420) * feat(handlers): more get Widget functoins * forgot image jsdoc * only needed inside if * Update mod.ts * move inside if * gonna do that * Update src/api/handlers/guild.ts Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> --- src/api/handlers/guild.ts | 29 +++++++++++++++++++++++++++++ src/api/handlers/mod.ts | 4 ++++ src/types/errors.ts | 1 + 3 files changed, 34 insertions(+) diff --git a/src/api/handlers/guild.ts b/src/api/handlers/guild.ts index e0795a526..d880098a5 100644 --- a/src/api/handlers/guild.ts +++ b/src/api/handlers/guild.ts @@ -729,6 +729,35 @@ export async function editWidget( return result; } +/** Returns the widget for the guild. */ +export async function getWidget(guildID: string, options?: { force: boolean }) { + if (!options?.force) { + const guild = await cacheHandlers.get("guilds", guildID); + if (!guild) throw new Error(Errors.GUILD_NOT_FOUND); + if (!guild?.widgetEnabled) throw new Error(Errors.GUILD_WIDGET_NOT_ENABLED); + } + + return RequestManager.get(`${endpoints.GUILD_WIDGET(guildID)}.json`); +} + +/** Returns the widget image URL for the guild. */ +export async function getWidgetImageUrl( + guildID: string, + options?: { + style?: "shield" | "banner1" | "banner2" | "banner3" | "banner4"; + force?: boolean; + }, +) { + if (!options?.force) { + const guild = await cacheHandlers.get("guilds", guildID); + if (!guild) throw new Error(Errors.GUILD_NOT_FOUND); + if (!guild.widgetEnabled) throw new Error(Errors.GUILD_WIDGET_NOT_ENABLED); + } + + return `${endpoints.GUILD_WIDGET(guildID)}.png?style=${options?.style ?? + "shield"}`; +} + /** Returns the code and uses of the vanity url for this server if it is enabled. Requires the MANAGE_GUILD permission. */ export async function getVanityURL(guildID: string) { const result = await RequestManager.get(endpoints.GUILD_VANITY_URL(guildID)); diff --git a/src/api/handlers/mod.ts b/src/api/handlers/mod.ts index c291ab020..71b569052 100644 --- a/src/api/handlers/mod.ts +++ b/src/api/handlers/mod.ts @@ -65,6 +65,8 @@ import { getVanityURL, getVoiceRegions, getWebhooks, + getWidget, + getWidgetImageUrl, getWidgetSettings, guildBannerURL, guildIconURL, @@ -193,6 +195,8 @@ export let handlers = { getVanityURL, getVoiceRegions, getWebhooks, + getWidget, + getWidgetImageUrl, guildBannerURL, guildIconURL, guildSplashURL, diff --git a/src/types/errors.ts b/src/types/errors.ts index f3895022a..fb719c3c6 100644 --- a/src/types/errors.ts +++ b/src/types/errors.ts @@ -10,6 +10,7 @@ export enum Errors { UPDATES_CHANNEL_CANNOT_BE_DELETED = "UPDATES_CHANNEL_CANNOT_BE_DELETED", // Guild Errors GUILD_NOT_DISCOVERABLE = "GUILD_NOT_DISCOVERABLE", + GUILD_WIDGET_NOT_ENABLED = "GUILD_WIDGET_NOT_ENABLED", GUILD_NOT_FOUND = "GUILD_NOT_FOUND", MEMBER_NOT_FOUND = "MEMBER_NOT_FOUND", PRUNE_MAX_DAYS = "PRUNE_MAX_DAYS",