Skip to content

Commit bba0e72

Browse files
refactor: use get guild role endpoint (#10443)
* refactor: use get guild role endpoint * style: import order --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 00accf7 commit bba0e72

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

packages/core/src/api/guild.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
type RESTGetAPIGuildPruneCountResult,
2929
type RESTGetAPIGuildQuery,
3030
type RESTGetAPIGuildResult,
31+
type RESTGetAPIGuildRoleResult,
3132
type RESTGetAPIGuildRolesResult,
3233
type RESTGetAPIGuildScheduledEventQuery,
3334
type RESTGetAPIGuildScheduledEventResult,
@@ -397,6 +398,18 @@ export class GuildsAPI {
397398
return this.rest.get(Routes.guildRoles(guildId), { signal }) as Promise<RESTGetAPIGuildRolesResult>;
398399
}
399400

401+
/**
402+
* Get a role in a guild
403+
*
404+
* @see {@link https://discord.com/developers/docs/resources/guild#get-guild-role}
405+
* @param guildId - The id of the guild to fetch the role from
406+
* @param roleId - The id of the role to fetch
407+
* @param options - The options for fetching the guild role
408+
*/
409+
public async getRole(guildId: Snowflake, roleId: Snowflake, { signal }: Pick<RequestData, 'signal'> = {}) {
410+
return this.rest.get(Routes.guildRole(guildId, roleId), { signal }) as Promise<RESTGetAPIGuildRoleResult>;
411+
}
412+
400413
/**
401414
* Creates a guild role
402415
*

packages/discord.js/src/managers/RoleManager.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const process = require('node:process');
44
const { Collection } = require('@discordjs/collection');
5-
const { Routes } = require('discord-api-types/v10');
5+
const { DiscordAPIError } = require('@discordjs/rest');
6+
const { RESTJSONErrorCodes, Routes } = require('discord-api-types/v10');
67
const CachedManager = require('./CachedManager');
78
const { DiscordjsTypeError, ErrorCodes } = require('../errors');
89
const { Role } = require('../structures/Role');
@@ -61,16 +62,29 @@ class RoleManager extends CachedManager {
6162
* .catch(console.error);
6263
*/
6364
async fetch(id, { cache = true, force = false } = {}) {
64-
if (id && !force) {
65+
if (!id) {
66+
const data = await this.client.rest.get(Routes.guildRoles(this.guild.id));
67+
const roles = new Collection();
68+
for (const role of data) roles.set(role.id, this._add(role, cache));
69+
return roles;
70+
}
71+
72+
if (!force) {
6573
const existing = this.cache.get(id);
6674
if (existing) return existing;
6775
}
6876

69-
// We cannot fetch a single role, as of this commit's date, Discord API throws with 405
70-
const data = await this.client.rest.get(Routes.guildRoles(this.guild.id));
71-
const roles = new Collection();
72-
for (const role of data) roles.set(role.id, this._add(role, cache));
73-
return id ? roles.get(id) ?? null : roles;
77+
try {
78+
const data = await this.client.rest.get(Routes.guildRole(this.guild.id, id));
79+
return this._add(data, cache);
80+
} catch (error) {
81+
// TODO: Remove this catch in the next major version
82+
if (error instanceof DiscordAPIError && error.code === RESTJSONErrorCodes.UnknownRole) {
83+
return null;
84+
}
85+
86+
throw error;
87+
}
7488
}
7589

7690
/**

0 commit comments

Comments
 (0)