Add Guild.me property (#90)
Some checks failed
Deploy MkDocs / deploy (push) Has been cancelled

This commit is contained in:
Slipstream 2025-06-15 18:13:06 -06:00 committed by GitHub
parent 095e7e7192
commit 98afb89629
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 5 deletions

View File

@ -13,6 +13,7 @@ A Python library for interacting with the Discord API, with a focus on bot devel
- Message component helpers
- `Message.jump_url` property for quick links to messages
- Built-in caching layer
- `Guild.me` property to access the bot's member object
- Experimental voice support
- Helpful error handling utilities

View File

@ -1284,6 +1284,15 @@ class Guild:
def get_role(self, role_id: str) -> Optional[Role]:
return next((role for role in self.roles if role.id == role_id), None)
@property
def me(self) -> Optional[Member]:
"""The member object for the connected bot in this guild, if present."""
client_user = getattr(self._client, "user", None)
if not client_user:
return None
return self.get_member(client_user.id)
def __repr__(self) -> str:
return f"<Guild id='{self.id}' name='{self.name}'>"

View File

@ -13,6 +13,15 @@ if member:
print(member.display_name)
```
To access the bot's own member object, use the ``Guild.me`` property. It returns
``None`` if the bot is not in the guild or its user data hasn't been loaded:
```python
bot_member = guild.me
if bot_member:
print(bot_member.joined_at)
```
The cache can be cleared manually if needed:
```python