refactor: Command Auto Syncing - #2990
Conversation
|
Thanks for opening this pull request! This pull request can be checked-out with: git fetch origin pull/2990/head:pr-2990
git checkout pr-2990This pull request can be installed with: pip install git+https://github.com/Pycord-Development/pycord@refs/pull/2990/head |
|
@Icebluewolf Feel free to mark this as ready for review once you think it is ready, note the linter failing in the gh actions |
|
Do you think that #1745 could be addressed here as well ? |
|
I updated the gist in the description to include a test file I do expect to do some cleanup tasks as well |
|
I have not done extensive testing with sub commands, sub sub commands, and if Discord gives extra information that we dont care about (aka not implemented) in the payload. |
Signed-off-by: Paillat <jeremiecotti@ik.me>
| def _check_command(cmd: ApplicationCommand, match: Mapping[str, Any]) -> bool: | ||
| """Returns True If Commands Are Equivalent""" | ||
| if isinstance(cmd, SlashCommandGroup): | ||
| if len(cmd.subcommands) != len(match.get("options", [])): | ||
| return True | ||
| return False | ||
| for i, subcommand in enumerate(cmd.subcommands): | ||
| match_ = next( | ||
| ( | ||
| data | ||
| for data in match["options"] | ||
| if data["name"] == subcommand.name | ||
| ), | ||
| MISSING, | ||
| match_ = find( | ||
| lambda x: x["name"] == subcommand.name, match["options"] | ||
| ) | ||
| if match_ is not MISSING and _check_command(subcommand, match_): | ||
| return True | ||
| if match_ is None: | ||
| return False | ||
| elif not _check_command(subcommand, match_): | ||
| return False | ||
| else: | ||
| return True | ||
| else: | ||
| as_dict = cmd.to_dict() | ||
| to_check = { | ||
| "nsfw": None, | ||
| "default_member_permissions": None, | ||
| "name": None, | ||
| "description": None, | ||
| "name_localizations": None, | ||
| "description_localizations": None, | ||
| "options": [ | ||
| "type", | ||
| "name", | ||
| "description", | ||
| "autocomplete", | ||
| "choices", | ||
| "name_localizations", | ||
| "description_localizations", | ||
| ], | ||
| "contexts": None, | ||
| "integration_types": None, | ||
| } | ||
| for check, value in to_check.items(): | ||
| if type(to_check[check]) == list: | ||
| # We need to do some falsy conversion here | ||
| # The API considers False (autocomplete) and [] (choices) to be falsy values | ||
| falsy_vals = (False, []) | ||
| for opt in value: | ||
| cmd_vals = ( | ||
| [val.get(opt, MISSING) for val in as_dict[check]] | ||
| if check in as_dict | ||
| else [] | ||
| ) | ||
| for i, val in enumerate(cmd_vals): | ||
| if val in falsy_vals: | ||
| cmd_vals[i] = MISSING | ||
| if match.get( | ||
| check, MISSING | ||
| ) is not MISSING and cmd_vals != [ | ||
| val.get(opt, MISSING) for val in match[check] | ||
| ]: | ||
| # We have a difference | ||
| return True | ||
| elif (attr := getattr(cmd, check, None)) != ( | ||
| found := match.get(check) | ||
| ): | ||
| # We might have a difference | ||
| if "localizations" in check and bool(attr) == bool(found): | ||
| # unlike other attrs, localizations are MISSING by default | ||
| continue | ||
| elif ( | ||
| check == "default_permission" | ||
| and attr is True | ||
| and found is None | ||
| ): | ||
| # This is a special case | ||
| # TODO: Remove for perms v2 | ||
| continue | ||
| return True | ||
| return False | ||
| if cmd.parent is None: | ||
| return _compare_defaults( | ||
| cmd.to_dict(), match, updated_command_defaults | ||
| ) | ||
| else: | ||
| return _compare_defaults(cmd.to_dict(), match, SUBCOMMAND_DEFAULTS) |
There was a problem hiding this comment.
This seems to skip comparing the group's own top-level attributes (e.g. description) in _check_command's SlashCommandGroup branch. It only checks subcommand count and recurses into subcommands. Is this intended?
Summary
This is ready but needs lots of testing.
I also need to check what the behavior is for undocumented fields in the payload from discord that may be added in the future. I believe the best option is to ignore those fields.
This gist (https://gist.github.com/Icebluewolf/1842ea5f7234ba40e5df18191bb135a0) contains 2 files. The first is where I developed the fix so that I did not have to make real API calls and is here for reference if someone wants to do the same. The second is unit tests for the new system. Both are very rough and messy sorry 🙃. This change needs to be tested extensively, so if someone sees test cases I missed or more test cases that should be added please let me know.
AI Disclosure: Single token auto complete. No LLM usage
This PR attempts to fix the long running issue with the application command auto-syncing system. Currently the system is over eager and preforms upserts on commands that do not need to be updated. The main culprit is incorrect default value checking. This PR implements a more robust and easily extendable comparison system for commands.
Information
examples, ...).
Checklist
type: ignorecomments were used, a comment is also left explaining why.