Add Member.top_role property (#17)

This commit is contained in:
Slipstream 2025-06-10 15:45:08 -06:00 committed by GitHub
parent 1c2241c9c4
commit 4c203f6792
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,6 +5,7 @@ Data models for Discord objects.
"""
import json
import asyncio
from typing import Optional, TYPE_CHECKING, List, Dict, Any, Union
from .errors import DisagreementException, HTTPException
@ -532,6 +533,34 @@ class Member(User): # Member inherits from User
reason=reason,
)
@property
def top_role(self) -> Optional["Role"]:
"""Return the member's highest role from the guild cache."""
if not self.guild_id or not self._client:
return None
guild = self._client.get_guild(self.guild_id)
if not guild:
return None
if not guild.roles and hasattr(self._client, "fetch_roles"):
try:
self._client.loop.run_until_complete(
self._client.fetch_roles(self.guild_id)
)
except RuntimeError:
future = asyncio.run_coroutine_threadsafe(
self._client.fetch_roles(self.guild_id), self._client.loop
)
future.result()
role_objects = [r for r in guild.roles if r.id in self.roles]
if not role_objects:
return None
return max(role_objects, key=lambda r: r.position)
class PartialEmoji:
"""Represents a partial emoji, often used in components or reactions.