Skip to content

Commit

Permalink
fix(commands): some caveats related to guild cache in user app intera…
Browse files Browse the repository at this point in the history
…ctions (#1262)
  • Loading branch information
shiftinv authored Dec 28, 2024
1 parent df5e391 commit 14d5b78
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
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

0 comments on commit 14d5b78

Please sign in to comment.