|
2 | 2 |
|
3 | 3 | const process = require('node:process'); |
4 | 4 | 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'); |
6 | 7 | const CachedManager = require('./CachedManager'); |
7 | 8 | const { DiscordjsTypeError, ErrorCodes } = require('../errors'); |
8 | 9 | const { Role } = require('../structures/Role'); |
@@ -61,16 +62,29 @@ class RoleManager extends CachedManager { |
61 | 62 | * .catch(console.error); |
62 | 63 | */ |
63 | 64 | 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) { |
65 | 73 | const existing = this.cache.get(id); |
66 | 74 | if (existing) return existing; |
67 | 75 | } |
68 | 76 |
|
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 | + } |
74 | 88 | } |
75 | 89 |
|
76 | 90 | /** |
|
0 commit comments