Skip to content

Commit

Permalink
Merge pull request #829 from krittick/revert-817
Browse files Browse the repository at this point in the history
Revert #817 due to high-severity prefix command errors introduced by its changes
  • Loading branch information
krittick authored Jan 22, 2022
2 parents 63fb2b0 + 054350b commit d2d8042
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 33 deletions.
12 changes: 1 addition & 11 deletions discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ def __init__(self, *args, **kwargs) -> None:
def pending_application_commands(self):
return self._pending_application_commands

@property
def all_commands(self):
return self._application_commands

@property
def commands(self) -> List[Union[ApplicationCommand, Any]]:
commands = self.application_commands
Expand Down Expand Up @@ -153,15 +149,9 @@ def remove_application_command(
Returns
--------
Optional[:class:`.ApplicationCommand`]
The command that was removed. If the command is not valid then
The command that was removed. If the name is not valid then
``None`` is returned instead.
"""
if command.id is None:
try:
index = self._pending_application_commands.index(command)
except ValueError:
return None
return self._pending_application_commands.pop(index)
return self._application_commands.pop(command.id)

@property
Expand Down
37 changes: 15 additions & 22 deletions discord/ext/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,32 +1159,25 @@ class GroupMixin(Generic[CogT]):
Attributes
-----------
prefixed_commands: :class:`dict`
all_commands: :class:`dict`
A mapping of command name to :class:`.Command`
objects.
case_insensitive: :class:`bool`
Whether the commands should be case insensitive. Defaults to ``False``.
"""
def __init__(self, *args: Any, **kwargs: Any) -> None:
case_insensitive = kwargs.get('case_insensitive', False)
self.prefixed_commands: Dict[str, Command[CogT, Any, Any]] = _CaseInsensitiveDict() if case_insensitive else {}
self.all_commands: Dict[str, Command[CogT, Any, Any]] = _CaseInsensitiveDict() if case_insensitive else {}
self.case_insensitive: bool = case_insensitive
super().__init__(*args, **kwargs)

@property
def all_commands(self):
# merge app and prefixed commands
if hasattr(self, "_application_commands"):
return {**self._application_commands, **self.prefixed_commands}
return self.prefixed_commands

@property
def commands(self) -> Set[Command[CogT, Any, Any]]:
"""Set[:class:`.Command`]: A unique set of commands without aliases that are registered."""
return set(self.all_commands.values())

def recursively_remove_all_commands(self) -> None:
for command in self.prefixed_commands.copy().values():
for command in self.all_commands.copy().values():
if isinstance(command, GroupMixin):
command.recursively_remove_all_commands()
self.remove_command(command.name)
Expand Down Expand Up @@ -1217,15 +1210,15 @@ def add_command(self, command: Command[CogT, Any, Any]) -> None:
if isinstance(self, Command):
command.parent = self

if command.name in self.prefixed_commands:
if command.name in self.all_commands:
raise CommandRegistrationError(command.name)

self.prefixed_commands[command.name] = command
self.all_commands[command.name] = command
for alias in command.aliases:
if alias in self.prefixed_commands:
if alias in self.all_commands:
self.remove_command(command.name)
raise CommandRegistrationError(alias, alias_conflict=True)
self.prefixed_commands[alias] = command
self.all_commands[alias] = command

def remove_command(self, name: str) -> Optional[Command[CogT, Any, Any]]:
"""Remove a :class:`.Command` from the internal list
Expand All @@ -1244,7 +1237,7 @@ def remove_command(self, name: str) -> Optional[Command[CogT, Any, Any]]:
The command that was removed. If the name is not valid then
``None`` is returned instead.
"""
command = self.prefixed_commands.pop(name, None)
command = self.all_commands.pop(name, None)

# does not exist
if command is None:
Expand All @@ -1256,12 +1249,12 @@ def remove_command(self, name: str) -> Optional[Command[CogT, Any, Any]]:

# we're not removing the alias so let's delete the rest of them.
for alias in command.aliases:
cmd = self.prefixed_commands.pop(alias, None)
cmd = self.all_commands.pop(alias, None)
# in the case of a CommandRegistrationError, an alias might conflict
# with an already existing command. If this is the case, we want to
# make sure the pre-existing command is not removed.
if cmd is not None and cmd != command:
self.prefixed_commands[alias] = cmd
self.all_commands[alias] = cmd
return command

def walk_commands(self) -> Generator[Command[CogT, Any, Any], None, None]:
Expand Down Expand Up @@ -1303,18 +1296,18 @@ def get_command(self, name: str) -> Optional[Command[CogT, Any, Any]]:

# fast path, no space in name.
if ' ' not in name:
return self.prefixed_commands.get(name)
return self.all_commands.get(name)

names = name.split()
if not names:
return None
obj = self.prefixed_commands.get(names[0])
obj = self.all_commands.get(names[0])
if not isinstance(obj, GroupMixin):
return obj

for name in names[1:]:
try:
obj = obj.prefixed_commands[name] # type: ignore
obj = obj.all_commands[name] # type: ignore
except (AttributeError, KeyError):
return None

Expand Down Expand Up @@ -1470,7 +1463,7 @@ async def invoke(self, ctx: Context) -> None:

if trigger:
ctx.subcommand_passed = trigger
ctx.invoked_subcommand = self.prefixed_commands.get(trigger, None)
ctx.invoked_subcommand = self.all_commands.get(trigger, None)

if early_invoke:
injected = hooked_wrapped_callback(self, ctx, self.callback)
Expand Down Expand Up @@ -1504,7 +1497,7 @@ async def reinvoke(self, ctx: Context, *, call_hooks: bool = False) -> None:

if trigger:
ctx.subcommand_passed = trigger
ctx.invoked_subcommand = self.prefixed_commands.get(trigger, None)
ctx.invoked_subcommand = self.all_commands.get(trigger, None)

if early_invoke:
try:
Expand Down

0 comments on commit d2d8042

Please sign in to comment.