From 874f1ee9795f1da7e7b24c5e2ba0517c1e656025 Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Wed, 20 Jan 2021 10:46:35 +0100 Subject: [PATCH] feat(handlers): add getEmoji() & getEmojis() (#406) * add(handlers): getEmojis * add(handlers): getEmoji * fix cache * addToCache argument * addtocache default to true * Update guild.ts * remove this * Update src/api/handlers/guild.ts Co-authored-by: Ayyan * Update src/api/handlers/guild.ts Co-authored-by: Ayyan Co-authored-by: Ayyan --- src/api/handlers/guild.ts | 49 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/api/handlers/guild.ts b/src/api/handlers/guild.ts index 6d6458a19..d7dd8343e 100644 --- a/src/api/handlers/guild.ts +++ b/src/api/handlers/guild.ts @@ -16,6 +16,7 @@ import { EditEmojisOptions, EditGuildTemplate, EditIntegrationOptions, + Emoji, Errors, FetchMembersOptions, GetAuditLogsOptions, @@ -349,6 +350,54 @@ export function emojiURL(id: string, animated = false) { return `https://cdn.discordapp.com/emojis/${id}.${animated ? "gif" : "png"}`; } +/** + * Returns a list of emojis for the given guild. + * + * ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()?.emojis + */ +export async function getEmojis(guildID: string, addToCache = true) { + const result = await RequestManager.get( + endpoints.GUILD_EMOJIS(guildID), + ) as Emoji[]; + + if (addToCache) { + const guild = await cacheHandlers.get("guilds", guildID); + if (!guild) throw new Error(Errors.GUILD_NOT_FOUND); + guild.emojis = result; + cacheHandlers.set("guilds", guildID, guild); + } + + return result; +} + +/** + * Returns an emoji for the given guild and emoji ID. + * + * ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()?.emojis + */ +export async function getEmoji( + guildID: string, + emojiID: string, + addToCache = true, +) { + const result = await RequestManager.get( + endpoints.GUILD_EMOJI(guildID, emojiID), + ) as Emoji; + + if (addToCache) { + const guild = await cacheHandlers.get("guilds", guildID); + if (!guild) throw new Error(Errors.GUILD_NOT_FOUND); + guild.emojis.push(result); + cacheHandlers.set( + "guilds", + guildID, + guild, + ); + } + + return result; +} + /** Create a new role for the guild. Requires the MANAGE_ROLES permission. */ export async function createGuildRole( guildID: string,