Skip to content

Commit

Permalink
Fix KeyError when removing a non-existent command/plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
norinorin authored and tandemdude committed Jun 5, 2021
1 parent 85c17d9 commit d77e737
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lightbulb/command_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 +473,21 @@ def remove_command(self, name: str) -> typing.Optional[str]:
Returns:
Optional[ :obj:`str` ]: Name of the command that was removed.
"""
command = self._commands.pop(name)
command = self._commands.pop(name, None)

if command is None:
return None

self.commands.remove(command)

if command is not None:
keys_to_remove = [command.name, *command._aliases]
keys_to_remove.remove(name)
for key in keys_to_remove:
self._commands.pop(key)
_LOGGER.debug("command removed: %s", command.name)
return command.name if command is not None else None

return command.name

def remove_plugin(self, name: str) -> typing.Optional[str]:
"""
Expand All @@ -493,7 +499,11 @@ def remove_plugin(self, name: str) -> typing.Optional[str]:
Returns:
Optional[ :obj:`str` ]: Name of the plugin that was removed.
"""
plugin = self.plugins.pop(name)
plugin = self.plugins.pop(name, None)

if plugin is None:
return None

plugin.plugin_remove()

if plugin is not None:
Expand All @@ -507,7 +517,8 @@ def remove_plugin(self, name: str) -> typing.Optional[str]:
_LOGGER.debug("listener removed: %s (%s)", callback.__name__, listener.event_type.__name__)

_LOGGER.debug("plugin removed: %s", plugin.name)
return plugin.name if plugin is not None else None

return plugin.name

def load_extension(self, extension: str) -> None:
"""
Expand Down

0 comments on commit d77e737

Please sign in to comment.