From 5b6cb6ece6ce998c37e4c21b26b4e2291ffa1254 Mon Sep 17 00:00:00 2001 From: MARCROCK22 Date: Wed, 21 Aug 2024 15:13:48 +0000 Subject: [PATCH] feat: fetch guild role --- src/api/Routes/guilds.ts | 2 ++ src/common/shorters/roles.ts | 16 ++++++++++++++++ src/structures/GuildRole.ts | 4 ++++ src/types/rest/guild.ts | 5 +++++ 4 files changed, 27 insertions(+) diff --git a/src/api/Routes/guilds.ts b/src/api/Routes/guilds.ts index 2c9f9cd..acd3488 100644 --- a/src/api/Routes/guilds.ts +++ b/src/api/Routes/guilds.ts @@ -34,6 +34,7 @@ import type { RESTGetAPIGuildPruneCountResult, RESTGetAPIGuildQuery, RESTGetAPIGuildResult, + RESTGetAPIGuildRoleResult, RESTGetAPIGuildRolesResult, RESTGetAPIGuildScheduledEventQuery, RESTGetAPIGuildScheduledEventResult, @@ -219,6 +220,7 @@ export interface GuildRoutes { ( id: string, ): { + get(args?: RestArguments): Promise; patch( args: RestArguments, ): Promise; diff --git a/src/common/shorters/roles.ts b/src/common/shorters/roles.ts index 89f37fd..291e545 100644 --- a/src/common/shorters/roles.ts +++ b/src/common/shorters/roles.ts @@ -21,6 +21,22 @@ export class RoleShorter extends BaseShorter { return Transformers.GuildRole(this.client, res, guildId); } + async fetch(guildId: string, roleId: string, force = false) { + const role = await this.raw(guildId, roleId, force); + return Transformers.GuildRole(this.client, role, guildId); + } + + async raw(guildId: string, roleId: string, force = false) { + let role: APIRole | undefined; + if (!force) { + role = await this.client.cache.roles?.raw(roleId); + if (role) return role; + } + role = await this.client.proxy.guilds(guildId).roles(roleId).get(); + await this.client.cache.roles?.set(roleId, guildId, role); + return role; + } + /** * Retrieves a list of roles in the guild. * @param guildId The ID of the guild. diff --git a/src/structures/GuildRole.ts b/src/structures/GuildRole.ts index 4254a86..60c2887 100644 --- a/src/structures/GuildRole.ts +++ b/src/structures/GuildRole.ts @@ -27,6 +27,10 @@ export class GuildRole extends DiscordBase { return this.client.guilds.fetch(this.guildId, force); } + fetch(force = false) { + return this.client.roles.fetch(this.guildId, this.id, force); + } + edit(body: RESTPatchAPIGuildRoleJSONBody, reason?: string) { return this.client.roles.create(this.guildId, body, reason); } diff --git a/src/types/rest/guild.ts b/src/types/rest/guild.ts index c41603e..9f0a49b 100644 --- a/src/types/rest/guild.ts +++ b/src/types/rest/guild.ts @@ -637,6 +637,11 @@ export interface RESTPostAPIGuildBulkBanResult { */ export type RESTGetAPIGuildRolesResult = APIRole[]; +/** + * https://discord.com/developers/docs/resources/guild#get-guild-role + */ +export type RESTGetAPIGuildRoleResult = APIRole; + /** * https://discord.com/developers/docs/resources/guild#create-guild-role */