Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor app command sync and other fixes #666

Conversation

onerandomusername
Copy link
Member

@onerandomusername onerandomusername commented Jul 27, 2022

Summary

Refactors app command sync and lays the groundwork to implement GH-665 (but doesn't do it as of now)

Checklist

I have not extensively tested or caught most cases, but a review (even in this condition) would be appreciated

  • If code changes were made, then they have been tested
    • I have updated the documentation to reflect the changes
    • I have formatted the code properly by running task lint
    • I have type-checked the code by running task pyright
  • This PR fixes an issue
  • This PR adds something new (e.g. new method or parameters)
  • This PR is a breaking change (e.g. methods or parameters removed/renamed)
  • This PR is not a code change (e.g. documentation, README, ...)

@onerandomusername onerandomusername added t: enhancement New feature breaking change Includes breaking changes to code/packaging p: high High priority s: in progress Issue/PR is being worked on t: refactor/typing/lint Refactors, typing changes and/or linting changes t: bugfix labels Jul 27, 2022
@onerandomusername onerandomusername added this to the disnake v2.6 milestone Jul 27, 2022
@onerandomusername onerandomusername changed the base branch from master to chore/rename-command-flags July 27, 2022 19:37
disnake/app_commands.py Outdated Show resolved Hide resolved
disnake/app_commands.py Outdated Show resolved Hide resolved
disnake/app_commands.py Outdated Show resolved Hide resolved
disnake/ext/commands/slash_core.py Outdated Show resolved Hide resolved
@shiftinv
Copy link
Member

Not sure what happened here, merging #667 resulted in GitHub automatically closing this PR, instead of changing the base branch to master like it should've:

If you delete a head branch after its pull request has been merged, GitHub checks for any open pull requests in the same repository that specify the deleted branch as their base branch. GitHub automatically updates any such pull requests, changing their base branch to the merged pull request's base branch.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-branches#working-with-branches

This worked fine in #616 - in any case, GitHub doesn't allow reopening this PR, looks like you may have to recreate it, sorry :/
(Perhaps due to different source repos? The documentation doesn't appear to mention anything about that though.)

@shiftinv shiftinv reopened this Jul 28, 2022
@shiftinv shiftinv changed the base branch from chore/rename-command-flags to master July 28, 2022 13:22
@shiftinv
Copy link
Member

... should be fixed except for merge conflicts, thanks to https://github.com/github/docs/discussions/18311.

Copy link
Member

@EQUENOS EQUENOS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! Here're some comments:

Comment on lines +266 to +258
if disnake.utils.get(
self._all_app_commands,
name=slash_command.name,
type=slash_command.type,
guild_id=guild_id,
):
raise CommandRegistrationError(slash_command.name, guild_id=guild_id)
self._all_app_commands[
AppCommandMetadata(slash_command.name, guild_id, slash_command.type)
] = slash_command
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we construct a named tuple here and use it as a key to check the existence? Like so:

command_metadata = AppCommandMetadata(slash_command.name, guild_id, slash_command.type)
if command_metadata in self._all_app_commands:
    raise CommandRegistrationError(slash_command.name, guild_id=guild_id)
self._all_app_commands[command_metadata] = slash_command

Same for similar samples below

Comment on lines +416 to 398
command: InvokableSlashCommand = self._all_app_commands.pop(meta, None) # type: ignore
if command is None:
return None
return command
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
command: InvokableSlashCommand = self._all_app_commands.pop(meta, None) # type: ignore
if command is None:
return None
return command
return self._all_app_commands.pop(meta, None) # type: ignore

Comment on lines +442 to 420
command: InvokableUserCommand = self._all_app_commands.pop(meta, None) # type: ignore
if command is None:
return None
return command
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion as above. Same for similar samples

disnake/ext/commands/interaction_bot_base.py Outdated Show resolved Hide resolved
disnake/ext/commands/interaction_bot_base.py Outdated Show resolved Hide resolved

all_commands = self._all_app_commands
for api_command in self._connection._global_application_commands.values():
cmdmeta = disnake.utils.get(all_commands, name=api_command.name, guild_id=None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, we can speed this up by constructing meta first and then checking if it's in the dict:

cmdmeta = AppCommandMetadata(api_command.name, api_command.type, None)
cmd = all_commands.get(cmdmeta)
if cmd is None:
    continue

disnake/ext/commands/interaction_bot_base.py Outdated Show resolved Hide resolved
disnake/ext/commands/slash_core.py Outdated Show resolved Hide resolved
@onerandomusername onerandomusername force-pushed the refactor/app-commands branch 3 times, most recently from 18e5271 to d83abf8 Compare August 14, 2022 06:57
@shiftinv shiftinv mentioned this pull request Aug 25, 2022
8 tasks
@shiftinv
Copy link
Member

@onerandomusername Could you resolve the conflicts in this PR?

@shiftinv shiftinv removed this from the disnake v2.8 milestone Feb 5, 2023
res = await self._connection.bulk_overwrite_global_commands(application_commands)

for api_command in res:
cmd = utils.get(application_commands, name=api_command.name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible to have 2 app commands of different types (or guilds) with identical names, so this should be modified

@@ -30,7 +31,7 @@
__all__ = ("InvokableUserCommand", "InvokableMessageCommand", "user_command", "message_command")


class InvokableUserCommand(InvokableApplicationCommand):
class InvokableUserCommand(InvokableApplicationCommand, UserCommand):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of subclassing UserCommand? The chain of super() calls never reaches it (see InvokableApplicationCommand.__init__) and it doesn't seem to make any typing improvements

@shiftinv
Copy link
Member

Since this has become somewhat outdated, and given the number of merge conflicts that have cropped up, I believe this can be closed in favour of #1107 - nonetheless, thanks for the work on this PR c:

@shiftinv shiftinv closed this Sep 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Includes breaking changes to code/packaging do not merge Don't merge. Don't. p: high High priority s: in progress Issue/PR is being worked on t: bugfix t: enhancement New feature t: refactor/typing/lint Refactors, typing changes and/or linting changes
Projects
Status: In Progress
5 participants