Skip to content

Commit

Permalink
fix: don't apply default contexts if dm_permission was set
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftinv committed Dec 25, 2024
1 parent 69ac99a commit df1dfef
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
13 changes: 9 additions & 4 deletions disnake/app_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,10 +675,15 @@ def _install_types_with_default(self) -> Optional[ApplicationInstallTypes]:

@property
def _contexts_with_default(self) -> Optional[InteractionContextTypes]:
# (same logic as `_install_types_with_default`, but without a fallback for contexts)
if self.contexts is None and not isinstance(self, _APIApplicationCommandMixin):
if self._default_contexts is not None:
return self._default_contexts
# (basically the same logic as `_install_types_with_default`, but without a fallback)
if (
self.contexts is None
and not isinstance(self, _APIApplicationCommandMixin)
and self._default_contexts is not None
# only use default if legacy `dm_permission` wasn't set
and self._dm_permission is None
):
return self._default_contexts

return self.contexts

Expand Down
14 changes: 14 additions & 0 deletions tests/ext/commands/test_base_core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# SPDX-License-Identifier: MIT

import warnings

import pytest

import disnake
Expand Down Expand Up @@ -131,6 +133,7 @@ async def c(inter) -> None:
...

assert c.body.to_dict().get("contexts") == [1]
assert "dm_permission" not in c.body.to_dict()

def test_decorator_override(self, bot: commands.InteractionBot) -> None:
@commands.contexts(private_channel=True)
Expand All @@ -147,6 +150,17 @@ async def c(inter: disnake.GuildCommandInteraction) -> None:

assert c.body.to_dict().get("contexts") == [0]

def test_dm_permission(self, bot: commands.InteractionBot) -> None:
with warnings.catch_warnings(category=DeprecationWarning, record=True):

Check failure on line 154 in tests/ext/commands/test_base_core.py

View workflow job for this annotation

GitHub Actions / pyright (3.8, false)

No overloads for "__init__" match the provided arguments   Argument types: (type[DeprecationWarning], Literal[True]) (reportGeneralTypeIssues)

@bot.slash_command(dm_permission=False)
async def c(inter) -> None:
...

# if dm_permission was set, the `contexts` default shouldn't apply
assert c.body.to_dict().get("contexts") is None
assert c.body.to_dict().get("dm_permission") is False


def test_localization_copy() -> None:
class Cog(commands.Cog):
Expand Down

0 comments on commit df1dfef

Please sign in to comment.