From 5fd71b1f7b35051a0877aef5ac64022b84e4430a Mon Sep 17 00:00:00 2001 From: Soheab_ <33902984+Soheab@users.noreply.github.com> Date: Wed, 25 Jun 2025 17:26:02 +0200 Subject: [PATCH] Fix cases where contexts/integration_types are empty --- discord/app_commands/models.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/discord/app_commands/models.py b/discord/app_commands/models.py index 704905d75..0d77559a4 100644 --- a/discord/app_commands/models.py +++ b/discord/app_commands/models.py @@ -246,7 +246,7 @@ class AppCommand(Hashable): self.description_localizations: Dict[Locale, str] = _to_locale_dict(data.get('description_localizations') or {}) def to_dict(self) -> ApplicationCommandPayload: - return { + res = { 'id': self.id, 'type': self.type.value, 'application_id': self.application_id, @@ -254,10 +254,16 @@ class AppCommand(Hashable): 'description': self.description, 'name_localizations': {str(k): v for k, v in self.name_localizations.items()}, 'description_localizations': {str(k): v for k, v in self.description_localizations.items()}, - 'contexts': self.allowed_contexts.to_array() if self.allowed_contexts is not None else None, - 'integration_types': self.allowed_installs.to_array() if self.allowed_installs is not None else None, 'options': [opt.to_dict() for opt in self.options], - } # type: ignore # Type checker does not understand this literal. + } + contexts = self.allowed_contexts.to_array() if self.allowed_contexts is not None else None + integration_types = self.allowed_installs.to_array() if self.allowed_installs is not None else None + if contexts: + res['contexts'] = contexts + if integration_types: + res['integration_types'] = integration_types + + return res # type: ignore # Type checker does not understand this literal. def __str__(self) -> str: return self.name @@ -401,6 +407,8 @@ class AppCommand(Hashable): Syncs the application command to Discord. + .. versionadded:: 2.6 + Raises ------- HTTPException