From f58ffe8321d36a66e730f13420e502470a5aa984 Mon Sep 17 00:00:00 2001 From: Slipstream Date: Fri, 13 Jun 2025 22:10:19 -0600 Subject: [PATCH] Apply global allowed_mentions setting (#76) --- README.md | 4 ++-- disagreement/ext/app_commands/context.py | 8 ++++++++ disagreement/ext/commands/core.py | 18 +++++++++--------- disagreement/models.py | 16 ++++++++-------- docs/introduction.md | 4 ++-- docs/mentions.md | 3 ++- 6 files changed, 31 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index d3805f8..7a50c0d 100644 --- a/README.md +++ b/README.md @@ -126,8 +126,8 @@ client = disagreement.Client( ) ``` -This dictionary is used whenever ``send_message`` is called without an explicit -``allowed_mentions`` argument. +This dictionary is used whenever ``send_message`` or helpers like ``Message.reply`` +are called without an explicit ``allowed_mentions`` argument. ### Defining Subcommands with `AppCommandGroup` diff --git a/disagreement/ext/app_commands/context.py b/disagreement/ext/app_commands/context.py index b761ca6..dda82fd 100644 --- a/disagreement/ext/app_commands/context.py +++ b/disagreement/ext/app_commands/context.py @@ -253,6 +253,8 @@ class AppCommandContext: Optional[Message]: The sent message object if a new message was created and not ephemeral. None if the response was ephemeral or an edit to a deferred message. """ + if allowed_mentions is None: + allowed_mentions = getattr(self.bot, "allowed_mentions", None) if not self._responded and self._deferred: # Editing a deferred response # Use edit_original_interaction_response payload: Dict[str, Any] = {} @@ -393,6 +395,9 @@ class AppCommandContext: "Must acknowledge or defer the interaction before sending a followup." ) + if allowed_mentions is None: + allowed_mentions = getattr(self.bot, "allowed_mentions", None) + payload: Dict[str, Any] = {} if content is not None: payload["content"] = content @@ -473,6 +478,9 @@ class AppCommandContext: "Cannot edit response if interaction hasn't been responded to or deferred." ) + if allowed_mentions is None: + allowed_mentions = getattr(self.bot, "allowed_mentions", None) + payload: Dict[str, Any] = {} if content is not None: payload["content"] = content # Use None to clear diff --git a/disagreement/ext/commands/core.py b/disagreement/ext/commands/core.py index 3a31e3b..7dd6c26 100644 --- a/disagreement/ext/commands/core.py +++ b/disagreement/ext/commands/core.py @@ -250,15 +250,15 @@ class CommandContext: client's :attr:`mention_replies` value is used. """ - allowed_mentions = kwargs.pop("allowed_mentions", None) - if mention_author is None: - mention_author = getattr(self.bot, "mention_replies", False) - - if allowed_mentions is None: - allowed_mentions = {"replied_user": mention_author} - else: - allowed_mentions = dict(allowed_mentions) - allowed_mentions.setdefault("replied_user", mention_author) + allowed_mentions = kwargs.pop("allowed_mentions", None) + if mention_author is None: + mention_author = getattr(self.bot, "mention_replies", False) + + if allowed_mentions is None: + allowed_mentions = dict(getattr(self.bot, "allowed_mentions", {}) or {}) + else: + allowed_mentions = dict(allowed_mentions) + allowed_mentions.setdefault("replied_user", mention_author) return await self.bot.send_message( channel_id=self.message.channel_id, diff --git a/disagreement/models.py b/disagreement/models.py index 1bbe2ef..b4502c3 100644 --- a/disagreement/models.py +++ b/disagreement/models.py @@ -194,14 +194,14 @@ class Message: ValueError: If both `embed` and `embeds` are provided. """ # Determine allowed mentions for the reply - if mention_author is None: - mention_author = getattr(self._client, "mention_replies", False) - - if allowed_mentions is None: - allowed_mentions = {"replied_user": mention_author} - else: - allowed_mentions = dict(allowed_mentions) - allowed_mentions.setdefault("replied_user", mention_author) + if mention_author is None: + mention_author = getattr(self._client, "mention_replies", False) + + if allowed_mentions is None: + allowed_mentions = dict(getattr(self._client, "allowed_mentions", {}) or {}) + else: + allowed_mentions = dict(allowed_mentions) + allowed_mentions.setdefault("replied_user", mention_author) # Client.send_message is already updated to handle these parameters return await self._client.send_message( diff --git a/docs/introduction.md b/docs/introduction.md index ffa8fcd..f1e5f4e 100644 --- a/docs/introduction.md +++ b/docs/introduction.md @@ -125,8 +125,8 @@ client = disagreement.Client( ) ``` -This dictionary is used whenever ``send_message`` is called without an explicit -``allowed_mentions`` argument. +This dictionary is used whenever ``send_message`` or helpers like ``Message.reply`` +are called without an explicit ``allowed_mentions`` argument. The :class:`AllowedMentions` class offers ``none()`` and ``all()`` helpers for quickly generating these configurations. diff --git a/docs/mentions.md b/docs/mentions.md index e3db3f5..f1a9357 100644 --- a/docs/mentions.md +++ b/docs/mentions.md @@ -15,7 +15,8 @@ client = disagreement.Client( ) ``` -When ``Client.send_message`` is called without an explicit ``allowed_mentions`` +When ``Client.send_message`` or convenience methods like ``Message.reply`` and +``CommandContext.reply`` are called without an explicit ``allowed_mentions`` argument this value will be used. ``AllowedMentions`` also provides the convenience methods