Update get_roles.ts

This commit is contained in:
ITOH
2021-05-03 22:58:15 +02:00
parent 889d03066a
commit 61c61dabdb

View File

@@ -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<Role[]>(
@@ -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;
}