From 1bf782fcb5168504ccf487641a3eb79f99bf341f Mon Sep 17 00:00:00 2001 From: Michael H Date: Thu, 27 May 2021 22:31:49 -0400 Subject: [PATCH] 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. --- discord/member.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/discord/member.py b/discord/member.py index fcb2eef0b..f3cead72c 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