Skip to content

refactor(commands): rewrite app command storage to support multiple per-guild commands with the same name - #1107

Open
EQUENOS wants to merge 78 commits into
masterfrom
refactor/app-command-storage
Open

EQUENOS wants to merge 78 commits into
masterfrom
refactor/app-command-storage

Conversation

@EQUENOS

@EQUENOS EQUENOS commented Sep 18, 2023

Copy link
Copy Markdown
Member

Summary

Currently, disnake.ext.commands framework doesn't allow registering 2 guild commands with the same type and name in different guilds, even though discord API allows to do that (see issue #260). This PR solves this problem.

Consequences

  • all_xxx_commands attributes of InteractionBotBase are now deprecated in favor of all_app_commands
  • bot.add_xxx_command methods are now deprecated in favor of add_app_command
  • bot.remove_xxx_command methods are now deprecated in favor of remove_app_command
  • bot.get_xxx_command now works in O(1) only if guild_id is specified, otherwise it's O(n) where n = len(all_app_commands)
  • bot.get_xxx_command can be ambiguous if guild_id is not specified
  • argument guild_id of bot._ordered_unsynced_commands is now deprecated

Note

The original idea of allowing to insert IDs of app commands to the corresponding invokable objects had to be abandoned. This is due to complications related to copying.

Checklist

  • 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 pdm lint
    • I have type-checked the code by running pdm 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, ...)

@EQUENOS EQUENOS added p: medium Medium priority t: refactor/typing/lint Refactors, typing changes and/or linting changes s: in progress Issue/PR is being worked on labels Sep 18, 2023
@EQUENOS EQUENOS changed the title refactor: Store invokable application commands properly refactor: store invokable application commands properly Sep 18, 2023
@EQUENOS EQUENOS added s: needs review Issue/PR is awaiting reviews t: deprecation Deprecation of existing features t: bugfix and removed s: in progress Issue/PR is being worked on labels Sep 18, 2023
Comment thread disnake/interactions/application_command.py
Comment thread disnake/ext/commands/interaction_bot_base.py Outdated
Comment thread disnake/ext/commands/interaction_bot_base.py Outdated
Comment thread disnake/ext/commands/interaction_bot_base.py Outdated

@shiftinv shiftinv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have a bunch of mostly minor comments, but overall this looks really cool 👀

Comment thread changelog/260.deprecate.0.rst Outdated
Comment thread changelog/260.deprecate.0.rst Outdated
Comment thread disnake/ext/commands/errors.py Outdated
Comment thread disnake/ext/commands/errors.py Outdated
Comment thread disnake/ext/commands/interaction_bot_base.py Outdated
Comment thread disnake/ext/commands/interaction_bot_base.py Outdated
Comment thread disnake/ext/commands/interaction_bot_base.py Outdated
Comment thread disnake/ext/commands/interaction_bot_base.py
Comment thread docs/ext/commands/api/exceptions.rst Outdated
Comment thread disnake/interactions/application_command.py Outdated
@shiftinv

Copy link
Copy Markdown
Member

Haven't tested much yet - not sure what exactly happened, but something somewhere ended up trying to register subcommands as top-level commands: https://gist.github.com/shiftinv/eddfcae9ca1f34790ced4a5288e3691f

@EQUENOS

EQUENOS commented Sep 23, 2023

Copy link
Copy Markdown
Member Author

Haven't tested much yet - not sure what exactly happened, but something somewhere ended up trying to register subcommands as top-level commands: https://gist.github.com/shiftinv/eddfcae9ca1f34790ced4a5288e3691f

Fixed this in 1f10a8d

@shiftinv shiftinv changed the title refactor: store invokable application commands properly refactor(commands): rewrite app command storage to support multiple per-guild commands with the same name Jul 30, 2026
Comment thread disnake/ext/commands/interaction_bot_base.py Outdated
Comment thread disnake/ext/commands/interaction_bot_base.py Outdated
Comment thread disnake/ext/commands/interaction_bot_base.py Outdated
Comment thread disnake/ext/commands/interaction_bot_base.py Outdated
Comment on lines +1662 to 1666
# this happens to always be a slash command, the instance check is
# for `None` and to appease the type-checker
slash_command = self._all_app_commands.get(cmd_index)
if not isinstance(slash_command, InvokableSlashCommand):
return

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

if slash_command is None:
    return
assert isinstance(slash_command, InvokableSlashCommand)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As it stands right now this might be better, though silently returning for unexpected types is a little more future-proof, if another autocomplete-able application command type is added at some point in the future

Comment thread disnake/ext/commands/errors.py Outdated
or :data:`None` if it was a global command.
"""

def __init__(self, cmd_type: ApplicationCommandType, name: str, guild_id: int | None) -> None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This exception has pretty much the same signature as AppCmdIndex, perhaps it could accept the index itself?
It's only use site already does ApplicationCommandRegistrationError(cmd_index.type, cmd_index.name, cmd_index.guild_id)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fair enough, yea. Updated in 769e254. It still ends up unpacking the AppCmdIndex into its constituent parts inside ApplicationCommandRegistrationError (don't want to expose AppCmdIndex too much, though it is documented nonetheless), but this at least simplifies creating it.

Comment thread disnake/ext/commands/interaction_bot_base.py
shiftinv and others added 4 commits August 24, 2026 20:06
Co-authored-by: Eneg <42005170+Enegg@users.noreply.github.com>
Signed-off-by: vi <8530778+shiftinv@users.noreply.github.com>
Comment thread disnake/ext/commands/interaction_bot_base.py Outdated
Comment thread disnake/ext/commands/interaction_bot_base.py
shiftinv and others added 4 commits August 25, 2026 14:38
Co-authored-by: Eneg <42005170+Enegg@users.noreply.github.com>
Signed-off-by: vi <8530778+shiftinv@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p: medium Medium priority s: needs review Issue/PR is awaiting reviews t: bugfix t: deprecation Deprecation of existing features t: refactor/typing/lint Refactors, typing changes and/or linting changes

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

Separate application commands with same name+type but in different guilds don't work

6 participants