Skip to content

refactor: use get guild role endpoint #10443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/core/src/api/guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
type RESTGetAPIGuildPruneCountResult,
type RESTGetAPIGuildQuery,
type RESTGetAPIGuildResult,
type RESTGetAPIGuildRoleResult,
type RESTGetAPIGuildRolesResult,
type RESTGetAPIGuildScheduledEventQuery,
type RESTGetAPIGuildScheduledEventResult,
Expand Down Expand Up @@ -397,6 +398,18 @@ export class GuildsAPI {
return this.rest.get(Routes.guildRoles(guildId), { signal }) as Promise<RESTGetAPIGuildRolesResult>;
}

/**
* Get a role in a guild
*
* @see {@link https://discord.com/developers/docs/resources/guild#get-guild-role}
* @param guildId - The id of the guild to fetch the role from
* @param roleId - The id of the role to fetch
* @param options - The options for fetching the guild role
*/
public async getRole(guildId: Snowflake, roleId: Snowflake, { signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.get(Routes.guildRole(guildId, roleId), { signal }) as Promise<RESTGetAPIGuildRoleResult>;
}

/**
* Creates a guild role
*
Expand Down
28 changes: 21 additions & 7 deletions packages/discord.js/src/managers/RoleManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

const process = require('node:process');
const { Collection } = require('@discordjs/collection');
const { Routes } = require('discord-api-types/v10');
const { DiscordAPIError } = require('@discordjs/rest');
const { RESTJSONErrorCodes, Routes } = require('discord-api-types/v10');
const CachedManager = require('./CachedManager');
const { DiscordjsTypeError, ErrorCodes } = require('../errors');
const { Role } = require('../structures/Role');
Expand Down Expand Up @@ -61,16 +62,29 @@ class RoleManager extends CachedManager {
* .catch(console.error);
*/
async fetch(id, { cache = true, force = false } = {}) {
if (id && !force) {
if (!id) {
const data = await this.client.rest.get(Routes.guildRoles(this.guild.id));
const roles = new Collection();
for (const role of data) roles.set(role.id, this._add(role, cache));
return roles;
}

if (!force) {
const existing = this.cache.get(id);
if (existing) return existing;
}

// We cannot fetch a single role, as of this commit's date, Discord API throws with 405
const data = await this.client.rest.get(Routes.guildRoles(this.guild.id));
const roles = new Collection();
for (const role of data) roles.set(role.id, this._add(role, cache));
return id ? roles.get(id) ?? null : roles;
try {
const data = await this.client.rest.get(Routes.guildRole(this.guild.id, id));
return this._add(data, cache);
} catch (error) {
// TODO: Remove this catch in the next major version
if (error instanceof DiscordAPIError && error.code === RESTJSONErrorCodes.UnknownRole) {
return null;
}

throw error;
}
}

/**
Expand Down
Loading