discord.js icon indicating copy to clipboard operation
discord.js copied to clipboard

`RoleManager#fetch()` may return `null`, but this is inconsistent

Open Astronautilus14 opened this issue 2 years ago • 2 comments

Which package is this bug report for?

discord.js

Issue description

There is an inconsistency with fetching roles with this manager compared to the majority of other managers in discord.js.

Discord's API does not provide a way to fetch a single role... but discord.js does. discord.js fetches all roles and attempts to resolve a role and if it fails, the method returns null.

This is inconsistent as the other managers naturally throw an error. The inconsistency here may lead to an unsuspecting encounter. It is proposed that either a custom discord.js error is thrown instead of returning null or stop the resolving of a singular role.

Code sample

const role = await guild.roles.fetch(roleId); // If the roleId does not exist, role is null

const member = await guild.members.fetch(memberId); // If the memberId does not exist, an error is thrown

Package version

14.8.0

Node.js version

16.18.0

Operating system

Windows 10

Priority this issue should have

Low (slightly annoying)

Which partials do you have configured?

No Partials

Which gateway intents are you subscribing to?

Guilds, GuildMembers, GuildMessages, MessageContent

I have tested this issue on a development release

No

Astronautilus14 avatar Mar 21 '23 14:03 Astronautilus14

Roles does this because there isn't actually an API endpoint for fetching individual roles. It fetches all of the roles, tries to resolve the id, and can return null. We could make this throw an error in the next major.

Members though, no. Never returns undefined. If memberId is defined but not a member of the guild, that fetch will throw an Unknown Member API error. If memberId is undefined, it will fetch all members in the guild as if you didn't pass any parameters.

monbrey avatar Mar 21 '23 14:03 monbrey

I wouldn't call this a bug as there is no unexpected or unintended behaviour... return values are documented, so this shouldn't be a surprise.

Jiralite avatar Mar 21 '23 15:03 Jiralite