Add default allowed_mentions option (#53)

This commit is contained in:
Slipstream 2025-06-11 14:27:03 -06:00 committed by GitHub
parent d4bf99eac6
commit 6fd1f93bab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 1 deletions

View File

@ -102,6 +102,20 @@ These options are forwarded to ``HTTPClient`` when it creates the underlying
``aiohttp.ClientSession``. You can specify a custom ``connector`` or any other
session parameter supported by ``aiohttp``.
### Default Allowed Mentions
Specify default mention behaviour for all outgoing messages when constructing the client:
```python
client = disagreement.Client(
token=token,
allowed_mentions={"parse": [], "replied_user": False},
)
```
This dictionary is used whenever ``send_message`` is called without an explicit
``allowed_mentions`` argument.
### Defining Subcommands with `AppCommandGroup`
```python

View File

@ -80,6 +80,8 @@ class Client:
command_prefix (Union[str, List[str], Callable[['Client', Message], Union[str, List[str]]]]):
The prefix(es) for commands. Defaults to '!'.
verbose (bool): If True, print raw HTTP and Gateway traffic for debugging.
mention_replies (bool): Whether replies mention the author by default.
allowed_mentions (Optional[Dict[str, Any]]): Default allowed mentions for messages.
http_options (Optional[Dict[str, Any]]): Extra options passed to
:class:`HTTPClient` for creating the internal
:class:`aiohttp.ClientSession`.
@ -98,6 +100,7 @@ class Client:
application_id: Optional[Union[str, int]] = None,
verbose: bool = False,
mention_replies: bool = False,
allowed_mentions: Optional[Dict[str, Any]] = None,
shard_count: Optional[int] = None,
gateway_max_retries: int = 5,
gateway_max_backoff: float = 60.0,
@ -170,6 +173,7 @@ class Client:
# Default whether replies mention the user
self.mention_replies: bool = mention_replies
self.allowed_mentions: Optional[Dict[str, Any]] = allowed_mentions
# Basic signal handling for graceful shutdown
# This might be better handled by the user's application code, but can be a nice default.
@ -1012,7 +1016,7 @@ class Client:
embeds (Optional[List[Embed]]): A list of embeds to send. Cannot be used with `embed`.
Discord supports up to 10 embeds per message.
components (Optional[List[ActionRow]]): A list of ActionRow components to include.
allowed_mentions (Optional[Dict[str, Any]]): Allowed mentions for the message.
allowed_mentions (Optional[Dict[str, Any]]): Allowed mentions for the message. Defaults to :attr:`Client.allowed_mentions`.
message_reference (Optional[Dict[str, Any]]): Message reference for replying.
attachments (Optional[List[Any]]): Attachments to include with the message.
files (Optional[List[Any]]): Files to upload with the message.
@ -1059,6 +1063,9 @@ class Client:
if isinstance(comp, ComponentModel)
]
if allowed_mentions is None:
allowed_mentions = self.allowed_mentions
message_data = await self._http.send_message(
channel_id=channel_id,
content=content,

23
docs/mentions.md Normal file
View File

@ -0,0 +1,23 @@
# Controlling Mentions
The client exposes settings to control how mentions behave in outgoing messages.
## Default Allowed Mentions
Use the ``allowed_mentions`` parameter of :class:`disagreement.Client` to set a
default for all messages:
```python
client = disagreement.Client(
token="YOUR_TOKEN",
allowed_mentions={"parse": [], "replied_user": False},
)
```
When ``Client.send_message`` is called without an explicit ``allowed_mentions``
argument this value will be used.
## Next Steps
- [Commands](commands.md)
- [HTTP Client Options](http_client.md)