Skip to content
This repository was archived by the owner on Mar 13, 2023. It is now read-only.

fix: Fix localisation syncing #614

Merged
merged 5 commits into from
Aug 27, 2022
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
13 changes: 10 additions & 3 deletions naff/api/http/http_requests/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,29 @@ async def delete_application_command(
)

async def get_application_commands(
self, application_id: "Snowflake_Type", guild_id: "Snowflake_Type"
self, application_id: "Snowflake_Type", guild_id: "Snowflake_Type", with_localisations: bool = True
) -> List[discord_typings.ApplicationCommandData]:
"""
Get all application commands for this application from discord.

Args:
application_id: the what application to query
guild_id: specify a guild to get commands from
with_localisations: whether to include all localisations in the response

Returns:
Application command data

"""
if guild_id == GLOBAL_SCOPE:
return await self.request(Route("GET", f"/applications/{application_id}/commands"))
return await self.request(Route("GET", f"/applications/{application_id}/guilds/{guild_id}/commands"))
return await self.request(
Route("GET", f"/applications/{application_id}/commands"),
params={"with_localizations": int(with_localisations)},
)
return await self.request(
Route("GET", f"/applications/{application_id}/guilds/{guild_id}/commands"),
params={"with_localizations": int(with_localisations)},
)

async def overwrite_application_commands(
self, app_id: "Snowflake_Type", data: List[Dict], guild_id: "Snowflake_Type" = None
Expand Down
17 changes: 11 additions & 6 deletions naff/models/naff/application_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,8 +1051,8 @@ def _compare_commands(local_cmd: dict, remote_cmd: dict) -> bool:
"description": ("description", ""),
"default_member_permissions": ("default_member_permissions", None),
"dm_permission": ("dm_permission", True),
"name_localized": ("name_localizations", {}),
"description_localized": ("description_localizations", {}),
"name_localized": ("name_localizations", None),
"description_localized": ("description_localizations", None),
}

for local_name, comparison_data in lookup.items():
Expand All @@ -1068,12 +1068,15 @@ def _compare_options(local_opt_list: dict, remote_opt_list: dict) -> bool:
"description": ("description", ""),
"required": ("required", False),
"autocomplete": ("autocomplete", False),
"name_localized": ("name_localizations", {}),
"description_localized": ("description_localizations", {}),
"name_localized": ("name_localizations", None),
"description_localized": ("description_localizations", None),
"choices": ("choices", []),
"max_value": ("max_value", None),
"min_value": ("min_value", None),
}
post_process: Dict[str, Callable] = {
"choices": lambda l: [d | {"name_localizations": {}} if len(d) == 2 else d for d in l],
}

if local_opt_list != remote_opt_list:
if len(local_opt_list) != len(remote_opt_list):
Expand All @@ -1084,14 +1087,16 @@ def _compare_options(local_opt_list: dict, remote_opt_list: dict) -> bool:

if local_option["type"] == remote_option["type"]:
if local_option["type"] in (OptionTypes.SUB_COMMAND_GROUP, OptionTypes.SUB_COMMAND):
if not _compare_commands(local_option, remote_option) or _compare_options(
if not _compare_commands(local_option, remote_option) or not _compare_options(
local_option.get("options", []), remote_option.get("options", [])
):
return False
else:
for local_name, comparison_data in options_lookup.items():
remote_name, default_value = comparison_data
if local_option.get(local_name, default_value) != remote_option.get(remote_name, default_value):
if local_option.get(local_name, default_value) != post_process.get(remote_name, lambda l: l)(
remote_option.get(remote_name, default_value)
):
return False

else:
Expand Down
3 changes: 3 additions & 0 deletions naff/models/naff/localisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ def to_locale_dict(self) -> dict:
if "locale-code" in attr.metadata:
if val := getattr(self, attr.name):
data[attr.metadata["locale-code"]] = val

if not data:
data = None # handle discord being stupid
return data


Expand Down