Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(commands): some caveats related to guild cache in user app interactions #1262

Merged
merged 4 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion disnake/ext/commands/cooldowns.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def get_key(self, msg: Message) -> Any:
elif self is BucketType.role:
# if author is not a Member we are in a private-channel context; returning its id
# yields the same result as for a guild with only the @everyone role
return (msg.author.top_role if isinstance(msg.author, Member) else msg.channel).id
return (
msg.author.top_role if msg.guild and isinstance(msg.author, Member) else msg.channel
).id

def __call__(self, msg: Message) -> Any:
return self.get_key(msg)
Expand Down
10 changes: 8 additions & 2 deletions disnake/ext/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2426,11 +2426,14 @@ def dm_only() -> Callable[[T], T]:
This check raises a special exception, :exc:`.PrivateMessageOnly`
that is inherited from :exc:`.CheckFailure`.

.. note::
For application commands, consider setting the allowed :ref:`contexts <app_command_contexts>` instead.

.. versionadded:: 1.1
"""

def predicate(ctx: AnyContext) -> bool:
if ctx.guild is not None:
if (ctx.guild if isinstance(ctx, Context) else ctx.guild_id) is not None:
raise PrivateMessageOnly
return True

Expand All @@ -2444,10 +2447,13 @@ def guild_only() -> Callable[[T], T]:

This check raises a special exception, :exc:`.NoPrivateMessage`
that is inherited from :exc:`.CheckFailure`.

.. note::
For application commands, consider setting the allowed :ref:`contexts <app_command_contexts>` instead.
"""

def predicate(ctx: AnyContext) -> bool:
if ctx.guild is None:
if (ctx.guild if isinstance(ctx, Context) else ctx.guild_id) is None:
raise NoPrivateMessage
return True

Expand Down
7 changes: 7 additions & 0 deletions disnake/interactions/application_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ class ApplicationCommandInteraction(Interaction[ClientT]):

author: Union[:class:`User`, :class:`Member`]
The user or member that sent the interaction.

.. note::
In scenarios where an interaction occurs in a guild but :attr:`.guild` is unavailable,
such as with user-installed applications in guilds, some attributes of :class:`Member`\\s
that depend on the guild/role cache will not work due to an API limitation.
This includes :attr:`~Member.roles`, :attr:`~Member.top_role`, :attr:`~Member.role_icon`,
and :attr:`~Member.guild_permissions`.
locale: :class:`Locale`
The selected language of the interaction's author.

Expand Down
7 changes: 7 additions & 0 deletions disnake/interactions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ class Interaction(Generic[ClientT]):

author: Union[:class:`User`, :class:`Member`]
The user or member that sent the interaction.

.. note::
In scenarios where an interaction occurs in a guild but :attr:`.guild` is unavailable,
such as with user-installed applications in guilds, some attributes of :class:`Member`\\s
that depend on the guild/role cache will not work due to an API limitation.
This includes :attr:`~Member.roles`, :attr:`~Member.top_role`, :attr:`~Member.role_icon`,
and :attr:`~Member.guild_permissions`.
locale: :class:`Locale`
The selected language of the interaction's author.

Expand Down
7 changes: 7 additions & 0 deletions disnake/interactions/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ class MessageInteraction(Interaction[ClientT]):

author: Union[:class:`User`, :class:`Member`]
The user or member that sent the interaction.

.. note::
In scenarios where an interaction occurs in a guild but :attr:`.guild` is unavailable,
such as with user-installed applications in guilds, some attributes of :class:`Member`\\s
that depend on the guild/role cache will not work due to an API limitation.
This includes :attr:`~Member.roles`, :attr:`~Member.top_role`, :attr:`~Member.role_icon`,
and :attr:`~Member.guild_permissions`.
locale: :class:`Locale`
The selected language of the interaction's author.

Expand Down
7 changes: 7 additions & 0 deletions disnake/interactions/modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ class ModalInteraction(Interaction[ClientT]):

author: Union[:class:`User`, :class:`Member`]
The user or member that sent the interaction.

.. note::
In scenarios where an interaction occurs in a guild but :attr:`.guild` is unavailable,
such as with user-installed applications in guilds, some attributes of :class:`Member`\\s
that depend on the guild/role cache will not work due to an API limitation.
This includes :attr:`~Member.roles`, :attr:`~Member.top_role`, :attr:`~Member.role_icon`,
and :attr:`~Member.guild_permissions`.
locale: :class:`Locale`
The selected language of the interaction's author.

Expand Down
Loading