diff options
| author | Michael H <[email protected]> | 2021-05-27 22:31:49 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-05-27 22:31:49 -0400 |
| commit | 1bf782fcb5168504ccf487641a3eb79f99bf341f (patch) | |
| tree | 354fd58f15c177099dcd40bed7639f601be9b6e0 | |
| parent | Add warning for comparing with role positioning (diff) | |
| download | discord.py-1bf782fcb5168504ccf487641a3eb79f99bf341f.tar.xz discord.py-1bf782fcb5168504ccf487641a3eb79f99bf341f.zip | |
Add Member.get_role
Adds an efficient way to check if a member has a role by ID.
This is done in a way consistent with the existing user API of the
library.
The more debated Member.has_role_id/has_role is intentionally not
included for review at this time given the heavy bikeshedding of it.
| -rw-r--r-- | discord/member.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/discord/member.py b/discord/member.py index fcb2eef0..f3cead72 100644 --- a/discord/member.py +++ b/discord/member.py @@ -830,3 +830,20 @@ class Member(discord.abc.Messageable, _BaseUser): user_id = self.id for role in roles: await req(guild_id, user_id, role.id, reason=reason) + + def get_role(self, role_id: int) -> Optional[discord.Role]: + """Returns a role with the given ID from roles which the member has. + + .. versionadded:: 2.0 + + Parameters + ----------- + role_id: :class:`int` + The ID to search for. + + Returns + -------- + Optional[:class:`Role`] + The role or ``None`` if not found in the member's roles. + """ + return self.guild.get_role(role_id) if self._roles.has(role_id) else None |