Fixed addcommand conflict messaging for non-custom commands.
CI / Formatting (push) Successful in 5s
CI / Linting (push) Successful in 5s
CI / Tests (Python 3.12) (push) Successful in 2m57s
CI / Tests (Python 3.13) (push) Successful in 2m56s
CI / Tests (Python 3.14) (push) Successful in 2m49s
CI / Type Checking (push) Successful in 9s
CI / Spelling (push) Successful in 4s

This commit is contained in:
2026-05-11 12:05:36 -04:00
parent 06b4a0bb9c
commit 62c5cd0ba5
3 changed files with 60 additions and 2 deletions
@@ -74,6 +74,17 @@ async def addcommand(ctx: CommandContext) -> None:
f"Use '{prefix}editcommand' to modify it." f"Use '{prefix}editcommand' to modify it."
) )
return return
except NotCustomCommandError:
await ctx.owncast_client.send_message(
"That command is not a custom command and cannot be modified."
)
return
except InactiveCommandError as e:
await ctx.owncast_client.send_message(
f"Custom command {prefix}{e.name} is disabled because another module "
f"registered the same name. Delete it or resolve the conflict to re-enable."
)
return
await ctx.owncast_client.send_message(f"Command {prefix}{cmd.name} created.") await ctx.owncast_client.send_message(f"Command {prefix}{cmd.name} created.")
@@ -156,11 +156,19 @@ class CommandManager:
:param response: Response template. :param response: Response template.
:return: Snapshot of the created command. :return: Snapshot of the created command.
:raises InvalidNameError: If name is invalid. :raises InvalidNameError: If name is invalid.
:raises CommandAlreadyExistsError: If name conflicts with any command. :raises CommandAlreadyExistsError: If name conflicts with a custom command.
:raises NotCustomCommandError: If name conflicts with a non-custom command.
:raises InactiveCommandError: If name conflicts with an inactive custom command.
""" """
if not NAME_RE.match(name): if not NAME_RE.match(name):
raise InvalidNameError(name) raise InvalidNameError(name)
if self._commands.exists(name): if self._commands.exists(name):
try:
command = await self._repo.get(name)
except CommandNotFoundError as err:
raise NotCustomCommandError(name) from err
if self._commands.get(command.name) is None:
raise InactiveCommandError(command.name)
raise CommandAlreadyExistsError(name) raise CommandAlreadyExistsError(name)
default_cooldown: int = self._ctx.config.get("default_cooldown", 5) default_cooldown: int = self._ctx.config.get("default_cooldown", 5)
@@ -632,11 +632,35 @@ class TestManager:
self, self,
manager: CommandManager, manager: CommandManager,
) -> None: ) -> None:
"""Create_command raises CommandAlreadyExistsError on conflict.""" """Create_command raises CommandAlreadyExistsError for custom conflicts."""
await manager.create_command("greet", "Hi")
with pytest.raises(CommandAlreadyExistsError) as exc_info: with pytest.raises(CommandAlreadyExistsError) as exc_info:
await manager.create_command("greet", "Info")
assert exc_info.value.name == "greet"
async def test_create_rejects_not_custom_conflict(
self,
manager: CommandManager,
) -> None:
"""Create_command raises NotCustomCommandError for non-custom conflicts."""
with pytest.raises(NotCustomCommandError) as exc_info:
await manager.create_command("about", "Info") await manager.create_command("about", "Info")
assert exc_info.value.name == "about" assert exc_info.value.name == "about"
async def test_create_rejects_inactive_conflict(
self,
manager: CommandManager,
command_dispatcher: CommandDispatcher,
module_context: ModuleContext,
) -> None:
"""Create_command raises InactiveCommandError for inactive custom conflicts."""
await manager.create_command("greet", "Hi")
module_context.commands.unregister("greet")
_register_foreign_command(command_dispatcher, "greet")
with pytest.raises(InactiveCommandError) as exc_info:
await manager.create_command("greet", "Info")
assert exc_info.value.name == "greet"
async def test_create_rollback_on_db_failure( async def test_create_rollback_on_db_failure(
self, self,
manager: CommandManager, manager: CommandManager,
@@ -1178,6 +1202,20 @@ class TestAddCommand:
"Command !greet already exists. Use '!editcommand' to modify it." "Command !greet already exists. Use '!editcommand' to modify it."
) )
async def test_not_custom_conflict(
self,
event_dispatcher: EventDispatcher,
owncast_client: RecordingOwncastClient,
) -> None:
"""!addcommand with a non-custom command name reports the right error."""
user = make_user(is_moderator=True)
event = make_chat_event(raw_body="!addcommand !about Hacked", user=user)
await event_dispatcher.dispatch(EventType.CHAT, event)
assert _send_bodies(owncast_client)[0] == (
"That command is not a custom command and cannot be modified."
)
@pytest.mark.usefixtures("module_lifecycle") @pytest.mark.usefixtures("module_lifecycle")
class TestEditCommand: class TestEditCommand:
@@ -1897,6 +1935,7 @@ class TestInactiveCommandRejection:
"raw_body", "raw_body",
[ [
pytest.param("!editcommand !greet New!", id="editcommand"), pytest.param("!editcommand !greet New!", id="editcommand"),
pytest.param("!addcommand !greet New!", id="addcommand"),
pytest.param("!commandmodonly !greet on", id="commandmodonly"), pytest.param("!commandmodonly !greet on", id="commandmodonly"),
pytest.param("!resetcommand !greet", id="resetcommand"), pytest.param("!resetcommand !greet", id="resetcommand"),
pytest.param("!commandcooldown !greet 10", id="commandcooldown"), pytest.param("!commandcooldown !greet 10", id="commandcooldown"),