From 61c61dabdb6be2f094e4918a03bae425f6941c4e Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Mon, 3 May 2021 22:58:15 +0200 Subject: [PATCH] Update get_roles.ts --- src/helpers/roles/get_roles.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/helpers/roles/get_roles.ts b/src/helpers/roles/get_roles.ts index b65dbf882..a94a7fa84 100644 --- a/src/helpers/roles/get_roles.ts +++ b/src/helpers/roles/get_roles.ts @@ -1,5 +1,8 @@ +import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; +import { structures } from "../../structures/mod.ts"; import { Role } from "../../types/permissions/role.ts"; +import { snowflakeToBigint } from "../../util/bigint.ts"; import { Collection } from "../../util/collection.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; @@ -8,7 +11,7 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts"; * * ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your roles will be cached in your guild.** */ -export async function getRoles(guildId: bigint) { +export async function getRoles(guildId: bigint, addToCache = true) { await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]); const result = await rest.runMethod( @@ -16,9 +19,23 @@ export async function getRoles(guildId: bigint) { endpoints.GUILD_ROLES(guildId), ); - // TODO: addToCache - - return new Collection( - result.map((role) => [role.id, role]), + const roleStructures = await Promise.all( + result.map(async (role) => + await structures.createDiscordenoRole({ role, guildId }) + ), ); + + const roles = new Collection( + roleStructures.map((role) => [role.id, role]), + ); + + if (addToCache) { + const guild = await cacheHandlers.get("guilds", guildId); + if (guild) { + guild.roles = roles; + await cacheHandlers.set("guilds", guild.id, guild); + } + } + + return roleStructures; }