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: Specify default mention behaviour for all outgoing messages when constructing the client:
```python ```python
from disagreement.models import AllowedMentions
client = disagreement.Client( client = disagreement.Client(
token=token, token=token,
allowed_mentions={"parse": [], "replied_user": False}, allowed_mentions=AllowedMentions.none().to_dict(),
) )
``` ```

View File

@ -624,14 +624,31 @@ class File:
self.data = data self.data = data
class AllowedMentions: class AllowedMentions:
"""Represents allowed mentions for a message or interaction response.""" """Represents allowed mentions for a message or interaction response."""
def __init__(self, data: Dict[str, Any]): def __init__(self, data: Dict[str, Any]):
self.parse: List[str] = data.get("parse", []) self.parse: List[str] = data.get("parse", [])
self.roles: List[str] = data.get("roles", []) self.roles: List[str] = data.get("roles", [])
self.users: List[str] = data.get("users", []) self.users: List[str] = data.get("users", [])
self.replied_user: bool = data.get("replied_user", False) 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]: def to_dict(self) -> Dict[str, Any]:
payload: Dict[str, Any] = {"parse": self.parse} payload: Dict[str, Any] = {"parse": self.parse}

View File

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

View File

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