mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-19 13:03:31 +01:00
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>
This commit is contained in:
@@ -28,6 +28,7 @@ import {
|
|||||||
type RESTGetAPIGuildPruneCountResult,
|
type RESTGetAPIGuildPruneCountResult,
|
||||||
type RESTGetAPIGuildQuery,
|
type RESTGetAPIGuildQuery,
|
||||||
type RESTGetAPIGuildResult,
|
type RESTGetAPIGuildResult,
|
||||||
|
type RESTGetAPIGuildRoleResult,
|
||||||
type RESTGetAPIGuildRolesResult,
|
type RESTGetAPIGuildRolesResult,
|
||||||
type RESTGetAPIGuildScheduledEventQuery,
|
type RESTGetAPIGuildScheduledEventQuery,
|
||||||
type RESTGetAPIGuildScheduledEventResult,
|
type RESTGetAPIGuildScheduledEventResult,
|
||||||
@@ -397,6 +398,18 @@ export class GuildsAPI {
|
|||||||
return this.rest.get(Routes.guildRoles(guildId), { signal }) as Promise<RESTGetAPIGuildRolesResult>;
|
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
|
* Creates a guild role
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
const process = require('node:process');
|
const process = require('node:process');
|
||||||
const { Collection } = require('@discordjs/collection');
|
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 CachedManager = require('./CachedManager');
|
||||||
const { DiscordjsTypeError, ErrorCodes } = require('../errors');
|
const { DiscordjsTypeError, ErrorCodes } = require('../errors');
|
||||||
const { Role } = require('../structures/Role');
|
const { Role } = require('../structures/Role');
|
||||||
@@ -61,16 +62,29 @@ class RoleManager extends CachedManager {
|
|||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
async fetch(id, { cache = true, force = false } = {}) {
|
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);
|
const existing = this.cache.get(id);
|
||||||
if (existing) return existing;
|
if (existing) return existing;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We cannot fetch a single role, as of this commit's date, Discord API throws with 405
|
try {
|
||||||
const data = await this.client.rest.get(Routes.guildRoles(this.guild.id));
|
const data = await this.client.rest.get(Routes.guildRole(this.guild.id, id));
|
||||||
const roles = new Collection();
|
return this._add(data, cache);
|
||||||
for (const role of data) roles.set(role.id, this._add(role, cache));
|
} catch (error) {
|
||||||
return id ? roles.get(id) ?? null : roles;
|
// TODO: Remove this catch in the next major version
|
||||||
|
if (error instanceof DiscordAPIError && error.code === RESTJSONErrorCodes.UnknownRole) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user