Add AllowedMentions helpers and update docs (#73)
Some checks failed
Deploy MkDocs / deploy (push) Has been cancelled

This commit is contained in:
Slipstream 2025-06-11 18:24:09 -06:00 committed by GitHub
parent 96cd3f1714
commit 0a3f680e7f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 36 additions and 9 deletions

View File

@ -118,9 +118,10 @@ session parameter supported by ``aiohttp``.
Specify default mention behaviour for all outgoing messages when constructing the client:
```python
from disagreement.models import AllowedMentions
client = disagreement.Client(
token=token,
allowed_mentions={"parse": [], "replied_user": False},
allowed_mentions=AllowedMentions.none().to_dict(),
)
```

View File

@ -633,6 +633,23 @@ class AllowedMentions:
self.users: List[str] = data.get("users", [])
self.replied_user: bool = data.get("replied_user", False)
@classmethod
def all(cls) -> "AllowedMentions":
"""Return an instance allowing all mention types."""
return cls(
{
"parse": ["users", "roles", "everyone"],
"replied_user": True,
}
)
@classmethod
def none(cls) -> "AllowedMentions":
"""Return an instance disallowing all mentions."""
return cls({"parse": [], "replied_user": False})
def to_dict(self) -> Dict[str, Any]:
payload: Dict[str, Any] = {"parse": self.parse}
if self.roles:

View File

@ -118,15 +118,19 @@ session parameter supported by ``aiohttp``.
Specify default mention behaviour for all outgoing messages when constructing the client:
```python
from disagreement.models import AllowedMentions
client = disagreement.Client(
token=token,
allowed_mentions={"parse": [], "replied_user": False},
allowed_mentions=AllowedMentions.none().to_dict(),
)
```
This dictionary is used whenever ``send_message`` is called without an explicit
``allowed_mentions`` argument.
The :class:`AllowedMentions` class offers ``none()`` and ``all()`` helpers for
quickly generating these configurations.
### Defining Subcommands with `AppCommandGroup`
```python

View File

@ -8,15 +8,20 @@ Use the ``allowed_mentions`` parameter of :class:`disagreement.Client` to set a
default for all messages:
```python
from disagreement.models import AllowedMentions
client = disagreement.Client(
token="YOUR_TOKEN",
allowed_mentions={"parse": [], "replied_user": False},
allowed_mentions=AllowedMentions.none().to_dict(),
)
```
When ``Client.send_message`` is called without an explicit ``allowed_mentions``
argument this value will be used.
``AllowedMentions`` also provides the convenience methods
``AllowedMentions.none()`` and ``AllowedMentions.all()`` to quickly create
common configurations.
## Next Steps
- [Commands](commands.md)