import re


def replace_command(string, *, command_infos, **_):
    """Replaces links to commands."""

    def replace(match):
        alias = match.group(1).lstrip(".")
        matching_ids = [
            info['id'] for info in command_infos if alias in info['aliases']
        ]
        assert len(matching_ids) > 0, "no command with alias {}".format(alias)
        return "[..{}](#{})".format(alias, matching_ids[0].lower())

    return re.sub(r"@command\(([^)]*)\)", replace, string)